shithub: rgbds

Download patch

ref: 96cb5e10ed87e7f072f9f98440a4139a13ba013d
parent: b224cab3e052e79ee8628ed896d4350765e1b75d
author: ISSOtm <eldredhabert0@gmail.com>
date: Sat Aug 22 23:02:17 EDT 2020

Fix range-dependent dead code in recursion depth check

--- a/include/asm/fstack.h
+++ b/include/asm/fstack.h
@@ -39,7 +39,7 @@
 	int32_t nREPTBodyLastLine;
 };
 
-extern unsigned int nMaxRecursionDepth;
+extern size_t nMaxRecursionDepth;
 
 void fstk_AddIncludePath(char const *s);
 /**
@@ -61,6 +61,6 @@
 char const *fstk_GetFileName(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 */
--- a/src/asm/fstack.c
+++ b/src/asm/fstack.c
@@ -44,8 +44,8 @@
 
 static struct Context *contextStack;
 static struct Context *topLevelContext;
-static unsigned int contextDepth = 0;
-unsigned int nMaxRecursionDepth;
+static size_t contextDepth = 0;
+size_t nMaxRecursionDepth;
 
 static unsigned int nbIncPaths = 0;
 static char const *includePaths[MAXINCPATHS];
@@ -180,7 +180,7 @@
 static void newContext(uint32_t reptDepth)
 {
 	if (++contextDepth >= nMaxRecursionDepth)
-		fatalerror("Recursion limit (%u) exceeded\n", nMaxRecursionDepth);
+		fatalerror("Recursion limit (%zu) exceeded\n", nMaxRecursionDepth);
 	contextStack->child = malloc(sizeof(*contextStack->child)
 						+ reptDepth * sizeof(contextStack->reptIters[0]));
 	if (!contextStack->child)
@@ -360,7 +360,7 @@
 	return lexer_GetLineNo();
 }
 
-void fstk_Init(char *mainPath, uint32_t maxRecursionDepth)
+void fstk_Init(char *mainPath, size_t maxRecursionDepth)
 {
 	topLevelContext = malloc(sizeof(*topLevelContext));
 	if (!topLevelContext)
@@ -381,18 +381,10 @@
 
 	contextStack = topLevelContext;
 
-#if 0
 	if (maxRecursionDepth
 			> (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",
-			(SIZE_MAX - sizeof(*contextStack)) / sizeof(contextStack->reptIters[0]));
+		      (SIZE_MAX - sizeof(*contextStack)) / sizeof(contextStack->reptIters[0]));
 		nMaxRecursionDepth = 64;
 	} else {
 		nMaxRecursionDepth = maxRecursionDepth;