Fix string expansion reporting

More expansions were allowed than the limit specified, and reporting code
did not account for the extra one that caused overflow
This commit is contained in:
ISSOtm
2020-07-31 09:49:51 +02:00
parent 149db9a022
commit 38bda7e1bb

View File

@@ -501,14 +501,10 @@ static void reallocCaptureBuf(void)
static struct Expansion *getExpansionAtDistance(size_t *distance) static struct Expansion *getExpansionAtDistance(size_t *distance)
{ {
unsigned int depth = 0;
struct Expansion *expansion = NULL; /* Top level has no "previous" level */ struct Expansion *expansion = NULL; /* Top level has no "previous" level */
#define LOOKUP_PRE_NEST(exp) #define LOOKUP_PRE_NEST(exp)
#define LOOKUP_POST_NEST(exp) do { \ #define LOOKUP_POST_NEST(exp)
if (depth++ > nMaxRecursionDepth) \
fatalerror("Recursion limit (%u) exceeded", nMaxRecursionDepth); \
} while (0)
lookupExpansion(expansion, *distance); lookupExpansion(expansion, *distance);
#undef LOOKUP_PRE_NEST #undef LOOKUP_PRE_NEST
#undef LOOKUP_POST_NEST #undef LOOKUP_POST_NEST
@@ -522,9 +518,13 @@ static void beginExpansion(size_t distance, uint8_t skip,
distance += lexerState->expansionOfs; /* Distance argument is relative to read offset! */ distance += lexerState->expansionOfs; /* Distance argument is relative to read offset! */
/* Increase the total length of all parents, and return the topmost one */ /* Increase the total length of all parents, and return the topmost one */
struct Expansion *parent = NULL; struct Expansion *parent = NULL;
unsigned int depth = 0;
#define LOOKUP_PRE_NEST(exp) (exp)->totalLen += size #define LOOKUP_PRE_NEST(exp) (exp)->totalLen += size
#define LOOKUP_POST_NEST(exp) #define LOOKUP_POST_NEST(exp) do { \
if (++depth >= nMaxRecursionDepth) \
fatalerror("Recursion limit (%u) exceeded", nMaxRecursionDepth); \
} while (0)
lookupExpansion(parent, distance); lookupExpansion(parent, distance);
#undef LOOKUP_PRE_NEST #undef LOOKUP_PRE_NEST
#undef LOOKUP_POST_NEST #undef LOOKUP_POST_NEST
@@ -784,7 +784,7 @@ void lexer_DumpStringExpansions(void)
{ {
if (!lexerState) if (!lexerState)
return; return;
struct Expansion *stack[nMaxRecursionDepth]; struct Expansion *stack[nMaxRecursionDepth + 1];
unsigned int depth = 0; unsigned int depth = 0;
size_t distance = lexerState->expansionOfs; size_t distance = lexerState->expansionOfs;