shithub: rgbds

Download patch

ref: be2572edca1eb97f5ca801d4e4380e757b04f314
parent: cf2bbe6435b643660c747a3e170b18baa20e3000
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Tue Apr 20 05:37:18 EDT 2021

Track nested interpolation depth even outside string literals

Fixes #837

--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -1280,7 +1280,15 @@
 	char symName[MAXSYMLEN + 1];
 	size_t i = 0;
 	struct FormatSpec fmt = fmt_NewSpec();
+	bool disableInterpolation = lexerState->disableInterpolation;
 
+	/*
+	 * In a context where `lexerState->disableInterpolation` is true, `peek` will expand
+	 * nested interpolations itself, which can lead to stack overflow. This lets
+	 * `readInterpolation` handle its own nested expansions, increasing `depth` each time.
+	 */
+	lexerState->disableInterpolation = true;
+
 	for (;;) {
 		int c = peek();
 
@@ -1319,6 +1327,9 @@
 		i--;
 	}
 	symName[i] = '\0';
+
+	/* Don't return before `lexerState->disableInterpolation` is reset! */ 
+	lexerState->disableInterpolation = disableInterpolation;
 
 	static char buf[MAXSTRLEN + 1];
 
--- a/test/asm/nested-interpolation-recursion.asm
+++ b/test/asm/nested-interpolation-recursion.asm
@@ -1,3 +1,2 @@
-def s equs "s"
-; 65 nested {}s, recursion limit is 64
-println "{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{s}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}"
+; 128 nested {}s, recursion limit is 64
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{s}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
--- a/test/asm/nested-interpolation-recursion.err
+++ b/test/asm/nested-interpolation-recursion.err
@@ -1,2 +1,2 @@
-FATAL: nested-interpolation-recursion.asm(3):
+FATAL: nested-interpolation-recursion.asm(2):
     Recursion limit (64) exceeded