Track nested interpolation depth even outside string literals

Fixes #837
This commit is contained in:
Rangi
2021-04-20 09:37:18 -04:00
parent cf2bbe6435
commit be2572edca
3 changed files with 14 additions and 4 deletions

View File

@@ -1280,6 +1280,14 @@ static char const *readInterpolation(unsigned int depth)
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();
@@ -1320,6 +1328,9 @@ static char const *readInterpolation(unsigned int depth)
}
symName[i] = '\0';
/* Don't return before `lexerState->disableInterpolation` is reset! */
lexerState->disableInterpolation = disableInterpolation;
static char buf[MAXSTRLEN + 1];
struct Symbol const *sym = sym_FindScopedSymbol(symName);