shithub: rgbds

Download patch

ref: e78a1d5bfd2840fa94c6a4c75a4e3aabeb904b70
parent: d2f6def2eb6da8b16b90f0bba95ace24b45d255b
author: Rangi <[email protected]>
date: Fri Apr 16 12:10:46 EDT 2021

`readInterpolation` is limited by `nMaxRecursionDepth`

Fixes #837

--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -800,7 +800,7 @@
 
 /* forward declarations for peek */
 static void shiftChar(void);
-static char const *readInterpolation(void);
+static char const *readInterpolation(unsigned int depth);
 
 static int peek(void)
 {
@@ -845,7 +845,7 @@
 	} else if (c == '{' && !lexerState->disableInterpolation) {
 		/* If character is an open brace, do symbol interpolation */
 		shiftChar();
-		char const *ptr = readInterpolation();
+		char const *ptr = readInterpolation(0);
 
 		if (ptr) {
 			beginExpansion(ptr, false, ptr);
@@ -1258,8 +1258,11 @@
 
 /* Functions to read strings */
 
-static char const *readInterpolation(void)
+static char const *readInterpolation(unsigned int depth)
 {
+	if (depth >= nMaxRecursionDepth)
+		fatalerror("Recursion limit (%zu) exceeded\n", nMaxRecursionDepth);
+
 	char symName[MAXSYMLEN + 1];
 	size_t i = 0;
 	struct FormatSpec fmt = fmt_NewSpec();
@@ -1269,7 +1272,7 @@
 
 		if (c == '{') { /* Nested interpolation */
 			shiftChar();
-			char const *ptr = readInterpolation();
+			char const *ptr = readInterpolation(depth + 1);
 
 			if (ptr) {
 				beginExpansion(ptr, false, ptr);
@@ -1491,7 +1494,7 @@
 			// We'll be exiting the string scope, so re-enable expansions
 			// (Not interpolations, since they're handled by the function itself...)
 			lexerState->disableMacroArgs = false;
-			char const *ptr = readInterpolation();
+			char const *ptr = readInterpolation(0);
 
 			if (ptr)
 				while (*ptr)
@@ -1641,7 +1644,7 @@
 			// We'll be exiting the string scope, so re-enable expansions
 			// (Not interpolations, since they're handled by the function itself...)
 			lexerState->disableMacroArgs = false;
-			char const *ptr = readInterpolation();
+			char const *ptr = readInterpolation(0);
 
 			if (ptr)
 				i = appendEscapedSubstring(ptr, i);
--- /dev/null
+++ b/test/asm/interpolation-recursion.asm
@@ -1,0 +1,6 @@
+recurse EQUS "\{recurse\} "
+{recurse}
+
+; FIXME: also handle the following:
+; recurse EQUS "\{recurse\}"
+; {recurse}
--- /dev/null
+++ b/test/asm/interpolation-recursion.err
@@ -1,0 +1,67 @@
+FATAL: interpolation-recursion.asm(2):
+    Recursion limit (64) exceeded
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
+while expanding symbol "{recurse} "
--- /dev/null
+++ b/test/asm/nested-interpolation-recursion.asm
@@ -1,0 +1,3 @@
+def s equs "s"
+; 65 nested {}s, recursion limit is 64
+println "{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{s}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}"
--- /dev/null
+++ b/test/asm/nested-interpolation-recursion.err
@@ -1,0 +1,2 @@
+FATAL: nested-interpolation-recursion.asm(3):
+    Recursion limit (64) exceeded