mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
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:
@@ -51,6 +51,9 @@ uint32_t tFloatingChars[256];
|
||||
uint32_t nFloating;
|
||||
enum eLexerState lexerstate = LEX_STATE_NORMAL;
|
||||
|
||||
struct sStringExpansionPos *pCurrentStringExpansion;
|
||||
static unsigned int nNbStringExpansions;
|
||||
|
||||
/* UTF-8 byte order mark */
|
||||
static const unsigned char bom[BOM_SIZE] = { 0xEF, 0xBB, 0xBF };
|
||||
|
||||
@@ -102,6 +105,31 @@ void yyunputstr(const char *s)
|
||||
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)
|
||||
{
|
||||
pCurrentBuffer = buf;
|
||||
@@ -424,6 +452,9 @@ void lex_Init(void)
|
||||
|
||||
nLexMaxLength = 0;
|
||||
nFloating = 0;
|
||||
|
||||
pCurrentStringExpansion = NULL;
|
||||
nNbStringExpansions = 0;
|
||||
}
|
||||
|
||||
void lex_AddStrings(const struct sLexInitString *lex)
|
||||
@@ -968,12 +999,30 @@ static uint32_t yylex_MACROARGS(void)
|
||||
|
||||
int yylex(void)
|
||||
{
|
||||
int returnedChar;
|
||||
switch (lexerstate) {
|
||||
case LEX_STATE_NORMAL:
|
||||
return yylex_NORMAL();
|
||||
returnedChar = yylex_NORMAL();
|
||||
break;
|
||||
case LEX_STATE_MACROARGS:
|
||||
return yylex_MACROARGS();
|
||||
returnedChar = yylex_MACROARGS();
|
||||
break;
|
||||
default:
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user