Add recursion limit for INCLUDE and macros

(And REPT.)
Not exactly a *recursion* limit, more like a *stack depth* limit,
but calling it "recursion" conveys its purpose better.
The default of 64 is super overkill: even in a a project with
what I believe to be above-average levels of nesting, the
level only peaked at 6.
Keeping in mind the purpose of this is to catch infinite
recursion, which is still caught quickly (in usual cases, anyways),
this default seems sensible.
And it passes tests. What more do you need?
This commit is contained in:
ISSOtm
2019-08-30 04:42:56 +02:00
parent a21cef7190
commit 6068b565f5
3 changed files with 21 additions and 1 deletions

View File

@@ -28,6 +28,8 @@
#include "types.h"
static struct sContext *pFileStack;
static unsigned int nFileStackDepth;
unsigned int nMaxFileStackDepth;
static struct sSymbol *pCurrentMacro;
static YY_BUFFER_STATE CurrentFlexHandle;
static FILE *pCurrentFile;
@@ -51,6 +53,8 @@ uint32_t ulMacroReturnValue;
#define STAT_isMacroArg 2
#define STAT_isREPTBlock 3
/* Max context stack size */
/*
* Context push and pop
*/
@@ -58,6 +62,9 @@ static void pushcontext(void)
{
struct sContext **ppFileStack;
if (++nFileStackDepth > nMaxFileStackDepth)
fatalerror("Recursion limit (%d) exceeded", nMaxFileStackDepth);
ppFileStack = &pFileStack;
while (*ppFileStack)
ppFileStack = &((*ppFileStack)->pNext);
@@ -158,6 +165,8 @@ static int32_t popcontext(void)
fatalerror("%s: Internal error.", __func__);
}
nFileStackDepth--;
free(*ppLastFile);
*ppLastFile = NULL;
yy_switch_to_buffer(CurrentFlexHandle);
@@ -417,6 +426,7 @@ void fstk_Init(char *pFileName)
if (pCurrentFile == NULL)
err(1, "Unable to open file '%s'", pFileName);
}
nFileStackDepth = 0;
nMacroCount = 0;
nCurrentStatus = STAT_isInclude;