Add recursion limit for string expansions

Unlike macros, REPTs and INCLUDEs, this recursion depth is independent.
This is intentional, because string expansions work very differently.

While it's easy to know when a string expansion begins, checking where it
ends is much more complicated, since the expansion's contents are simply
injected back into the lex buffer. Therefore, the depth has to be checked
after lexing took place.
Because of this, the placement of the expansion end check is somewhat
haphazard, but I think it's good. While I have no certainty, all tests
ended with all expansions properly ended, and I couldn't find any pitfalls.

Finally, `pCurrentStringExpansion` has been made global so error printing
can use it to tell the user if an error occurred inside of an expansion.
This commit is contained in:
ISSOtm
2019-08-31 15:36:58 +02:00
parent dc2c97fe0c
commit e0e8170fe6
6 changed files with 68 additions and 8 deletions

View File

@@ -35,7 +35,7 @@ struct sContext {
uint32_t nREPTBlockSize; uint32_t nREPTBlockSize;
}; };
extern unsigned int nMaxFileStackDepth; extern unsigned int nMaxRecursionDepth;
void fstk_RunInclude(char *tzFileName); void fstk_RunInclude(char *tzFileName);
void fstk_RunMacroArg(int32_t s); void fstk_RunMacroArg(int32_t s);

View File

@@ -40,6 +40,13 @@ enum eLexerState {
LEX_STATE_MACROARGS LEX_STATE_MACROARGS
}; };
struct sStringExpansionPos {
char *tzName;
char *pBuffer;
char *pBufferPos;
struct sStringExpansionPos *pParent;
};
#define INITIAL 0 #define INITIAL 0
#define macroarg 3 #define macroarg 3
@@ -62,6 +69,7 @@ void lex_FloatDeleteSecondRange(uint32_t id, uint16_t start, uint16_t end);
void lex_Init(void); void lex_Init(void);
void lex_AddStrings(const struct sLexInitString *lex); void lex_AddStrings(const struct sLexInitString *lex);
void lex_SetBuffer(char *buffer, uint32_t len); void lex_SetBuffer(char *buffer, uint32_t len);
void lex_BeginStringExpansion(const char *tzName);
int yywrap(void); int yywrap(void);
int yylex(void); int yylex(void);
void yyunput(char c); void yyunput(char c);
@@ -70,6 +78,7 @@ void yyskipbytes(uint32_t count);
void yyunputbytes(uint32_t count); void yyunputbytes(uint32_t count);
extern YY_BUFFER_STATE pCurrentBuffer; extern YY_BUFFER_STATE pCurrentBuffer;
extern struct sStringExpansionPos *pCurrentStringExpansion;
void upperstring(char *s); void upperstring(char *s);
void lowerstring(char *s); void lowerstring(char *s);

View File

@@ -29,7 +29,7 @@
static struct sContext *pFileStack; static struct sContext *pFileStack;
static unsigned int nFileStackDepth; static unsigned int nFileStackDepth;
unsigned int nMaxFileStackDepth; unsigned int nMaxRecursionDepth;
static struct sSymbol *pCurrentMacro; static struct sSymbol *pCurrentMacro;
static YY_BUFFER_STATE CurrentFlexHandle; static YY_BUFFER_STATE CurrentFlexHandle;
static FILE *pCurrentFile; static FILE *pCurrentFile;
@@ -62,8 +62,8 @@ static void pushcontext(void)
{ {
struct sContext **ppFileStack; struct sContext **ppFileStack;
if (++nFileStackDepth > nMaxFileStackDepth) if (++nFileStackDepth > nMaxRecursionDepth)
fatalerror("Recursion limit (%d) exceeded", nMaxFileStackDepth); fatalerror("Recursion limit (%d) exceeded", nMaxRecursionDepth);
ppFileStack = &pFileStack; ppFileStack = &pFileStack;
while (*ppFileStack) while (*ppFileStack)

View File

@@ -279,6 +279,8 @@ uint32_t ParseSymbol(char *src, uint32_t size)
if (!oDontExpandStrings && sym_isString(dest)) { if (!oDontExpandStrings && sym_isString(dest)) {
char *s; char *s;
lex_BeginStringExpansion(dest);
/* Feed the symbol's contents into the buffer */ /* Feed the symbol's contents into the buffer */
yyunputstr(s = sym_GetStringValue(dest)); yyunputstr(s = sym_GetStringValue(dest));

View File

@@ -51,6 +51,9 @@ uint32_t tFloatingChars[256];
uint32_t nFloating; uint32_t nFloating;
enum eLexerState lexerstate = LEX_STATE_NORMAL; enum eLexerState lexerstate = LEX_STATE_NORMAL;
struct sStringExpansionPos *pCurrentStringExpansion;
static unsigned int nNbStringExpansions;
/* UTF-8 byte order mark */ /* UTF-8 byte order mark */
static const unsigned char bom[BOM_SIZE] = { 0xEF, 0xBB, 0xBF }; static const unsigned char bom[BOM_SIZE] = { 0xEF, 0xBB, 0xBF };
@@ -102,6 +105,31 @@ void yyunputstr(const char *s)
memcpy(pLexBuffer, s, len); memcpy(pLexBuffer, s, len);
} }
/*
* Marks that a new string expansion with name `tzName` ends here
* Enforces recursion depth
*/
void lex_BeginStringExpansion(const char *tzName)
{
if (++nNbStringExpansions > nMaxRecursionDepth)
fatalerror("Recursion limit (%d) exceeded", nMaxRecursionDepth);
struct sStringExpansionPos *pNewStringExpansion =
malloc(sizeof(*pNewStringExpansion));
char *tzNewExpansionName = strdup(tzName);
if (!pNewStringExpansion || !tzNewExpansionName)
fatalerror("Could not allocate memory to expand '%s'",
tzName);
pNewStringExpansion->tzName = tzNewExpansionName;
pNewStringExpansion->pBuffer = pLexBufferRealStart;
pNewStringExpansion->pBufferPos = pLexBuffer;
pNewStringExpansion->pParent = pCurrentStringExpansion;
pCurrentStringExpansion = pNewStringExpansion;
}
void yy_switch_to_buffer(YY_BUFFER_STATE buf) void yy_switch_to_buffer(YY_BUFFER_STATE buf)
{ {
pCurrentBuffer = buf; pCurrentBuffer = buf;
@@ -424,6 +452,9 @@ void lex_Init(void)
nLexMaxLength = 0; nLexMaxLength = 0;
nFloating = 0; nFloating = 0;
pCurrentStringExpansion = NULL;
nNbStringExpansions = 0;
} }
void lex_AddStrings(const struct sLexInitString *lex) void lex_AddStrings(const struct sLexInitString *lex)
@@ -968,12 +999,30 @@ static uint32_t yylex_MACROARGS(void)
int yylex(void) int yylex(void)
{ {
int returnedChar;
switch (lexerstate) { switch (lexerstate) {
case LEX_STATE_NORMAL: case LEX_STATE_NORMAL:
return yylex_NORMAL(); returnedChar = yylex_NORMAL();
break;
case LEX_STATE_MACROARGS: case LEX_STATE_MACROARGS:
return yylex_MACROARGS(); returnedChar = yylex_MACROARGS();
break;
default: default:
fatalerror("%s: Internal error.", __func__); fatalerror("%s: Internal error.", __func__);
} }
/* Check if string expansions were fully read */
while (pCurrentStringExpansion
&& pCurrentStringExpansion->pBuffer == pLexBufferRealStart
&& pCurrentStringExpansion->pBufferPos <= pLexBuffer) {
struct sStringExpansionPos *pParent =
pCurrentStringExpansion->pParent;
free(pCurrentStringExpansion->tzName);
free(pCurrentStringExpansion);
pCurrentStringExpansion = pParent;
nNbStringExpansions--;
}
return returnedChar;
} }

View File

@@ -315,7 +315,7 @@ int main(int argc, char *argv[])
/* yydebug=1; */ /* yydebug=1; */
nMaxFileStackDepth = 64; nMaxRecursionDepth = 64;
DefaultOptions.gbgfx[0] = '0'; DefaultOptions.gbgfx[0] = '0';
DefaultOptions.gbgfx[1] = '1'; DefaultOptions.gbgfx[1] = '1';
@@ -389,7 +389,7 @@ int main(int argc, char *argv[])
break; break;
case 'r': case 'r':
nMaxFileStackDepth = strtoul(optarg, &ep, 0); nMaxRecursionDepth = strtoul(optarg, &ep, 0);
if (optarg[0] == '\0' || *ep != '\0') if (optarg[0] == '\0' || *ep != '\0')
errx(1, "Invalid argument for option 'r'"); errx(1, "Invalid argument for option 'r'");