Fix range-dependent dead code in recursion depth check

This commit is contained in:
ISSOtm
2020-08-23 03:02:17 +02:00
parent b224cab3e0
commit 96cb5e10ed
2 changed files with 7 additions and 15 deletions

View File

@@ -39,7 +39,7 @@ struct sContext {
int32_t nREPTBodyLastLine; int32_t nREPTBodyLastLine;
}; };
extern unsigned int nMaxRecursionDepth; extern size_t nMaxRecursionDepth;
void fstk_AddIncludePath(char const *s); void fstk_AddIncludePath(char const *s);
/** /**
@@ -61,6 +61,6 @@ char *fstk_DumpToStr(void);
char const *fstk_GetFileName(void); char const *fstk_GetFileName(void);
uint32_t fstk_GetLine(void); uint32_t fstk_GetLine(void);
void fstk_Init(char *mainPath, uint32_t maxRecursionDepth); void fstk_Init(char *mainPath, size_t maxRecursionDepth);
#endif /* RGBDS_ASM_FSTACK_H */ #endif /* RGBDS_ASM_FSTACK_H */

View File

@@ -44,8 +44,8 @@ struct Context {
static struct Context *contextStack; static struct Context *contextStack;
static struct Context *topLevelContext; static struct Context *topLevelContext;
static unsigned int contextDepth = 0; static size_t contextDepth = 0;
unsigned int nMaxRecursionDepth; size_t nMaxRecursionDepth;
static unsigned int nbIncPaths = 0; static unsigned int nbIncPaths = 0;
static char const *includePaths[MAXINCPATHS]; static char const *includePaths[MAXINCPATHS];
@@ -180,7 +180,7 @@ bool yywrap(void)
static void newContext(uint32_t reptDepth) static void newContext(uint32_t reptDepth)
{ {
if (++contextDepth >= nMaxRecursionDepth) if (++contextDepth >= nMaxRecursionDepth)
fatalerror("Recursion limit (%u) exceeded\n", nMaxRecursionDepth); fatalerror("Recursion limit (%zu) exceeded\n", nMaxRecursionDepth);
contextStack->child = malloc(sizeof(*contextStack->child) contextStack->child = malloc(sizeof(*contextStack->child)
+ reptDepth * sizeof(contextStack->reptIters[0])); + reptDepth * sizeof(contextStack->reptIters[0]));
if (!contextStack->child) if (!contextStack->child)
@@ -360,7 +360,7 @@ uint32_t fstk_GetLine(void)
return lexer_GetLineNo(); return lexer_GetLineNo();
} }
void fstk_Init(char *mainPath, uint32_t maxRecursionDepth) void fstk_Init(char *mainPath, size_t maxRecursionDepth)
{ {
topLevelContext = malloc(sizeof(*topLevelContext)); topLevelContext = malloc(sizeof(*topLevelContext));
if (!topLevelContext) if (!topLevelContext)
@@ -381,18 +381,10 @@ void fstk_Init(char *mainPath, uint32_t maxRecursionDepth)
contextStack = topLevelContext; contextStack = topLevelContext;
#if 0
if (maxRecursionDepth if (maxRecursionDepth
> (SIZE_MAX - sizeof(*contextStack)) / sizeof(contextStack->reptIters[0])) { > (SIZE_MAX - sizeof(*contextStack)) / sizeof(contextStack->reptIters[0])) {
#else
/* If this holds, then GCC raises a warning about the `if` above being dead code */
static_assert(UINT32_MAX
<= (SIZE_MAX - sizeof(*contextStack)) / sizeof(contextStack->reptIters[0]),
"Please enable recursion depth capping");
if (0) {
#endif
error("Recursion depth may not be higher than %zu, defaulting to 64\n", error("Recursion depth may not be higher than %zu, defaulting to 64\n",
(SIZE_MAX - sizeof(*contextStack)) / sizeof(contextStack->reptIters[0])); (SIZE_MAX - sizeof(*contextStack)) / sizeof(contextStack->reptIters[0]));
nMaxRecursionDepth = 64; nMaxRecursionDepth = 64;
} else { } else {
nMaxRecursionDepth = maxRecursionDepth; nMaxRecursionDepth = maxRecursionDepth;