Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Better sanity checking on scratch buffer array.
Don't wrap the scratch buffer array. Reset it to index #0 on each token, and
 consider it an error if it overflows. This should make it about equally as
 functional, but alert us if we might have silently overwritten a string
 that's still in use. This can happen now that we're dumping out several
 lines and scratch variables in the arb1 profile to emulate newer shader
 models.

--HG--
branch : trunk
  • Loading branch information
icculus committed May 30, 2008
1 parent fbf648f commit f3282ff
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions mojoshader.c
Expand Up @@ -467,21 +467,31 @@ static inline int shader_is_vertex(const Context *ctx)
} // shader_is_vertex


// Bit arrays (for storing large arrays of booleans...
static inline int isfail(const Context *ctx)
{
return (ctx->failstr != NULL);
} // isfail


static inline char *get_scratch_buffer(Context *ctx)
{
if ((ctx->scratchidx >= SCRATCH_BUFFERS) && !isfail(ctx))
{
// can't call fail() here, since it calls back into here.
const char *errstr = "BUG: overflowed scratch buffers";
char *failstr = (char *) Malloc(ctx, strlen(errstr) + 1);
if (failstr != NULL)
{
strcpy(failstr, errstr);
ctx->failstr = failstr;
} // if
} // if

ctx->scratchidx = (ctx->scratchidx + 1) % SCRATCH_BUFFERS;
return ctx->scratch[ctx->scratchidx];
} // get_scratch_buffer


static inline int isfail(const Context *ctx)
{
return (ctx->failstr != NULL);
} // isfail


static int failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static int failf(Context *ctx, const char *fmt, ...)
{
Expand Down Expand Up @@ -6420,6 +6430,9 @@ const MOJOSHADER_parseData *MOJOSHADER_parse(const char *profile,
// parse out the rest of the tokens after the version token...
while ( (rc > 0) && (!isfail(ctx)) )
{
// reset for every token, and consider an error if it ever overflows!
ctx->scratchidx = 0;

ctx->tokens += rc;
ctx->tokencount -= rc;
rc = parse_token(ctx);
Expand Down

0 comments on commit f3282ff

Please sign in to comment.