Commit 6cbfdd6
preprocessor: add macroExpandDepth guard to bound MacroExpand recursion
MacroExpand can call PrescanMacroArg (to pre-expand function-like macro
arguments), which in turn calls MacroExpand for any macros found in the
argument token stream. With deeply nested macro invocations in the
source, this creates a call chain of depth proportional to the nesting
level. At 200+ levels the native C++ stack overflows.
Add macroExpandDepth to TPpContext (initialized to 0) and a RAII
DepthGuard in MacroExpand that increments it on entry and decrements on
exit. If the depth reaches maxMacroExpandDepth (200) MacroExpand emits
a preprocessor error and returns MacroExpandNotStarted, unwinding the
recursion cleanly instead of crashing.
Add test preprocessor.macro.recursion.vert: F(x)=x nested 210 times
triggers the guard 10 times with 'macro expansion depth limit exceeded'
errors; without the guard the same input causes a stack overflow.1 parent 598bd84 commit 6cbfdd6
6 files changed
Lines changed: 49 additions & 0 deletions
File tree
- Test
- baseResults
- glslang/MachineIndependent/preprocessor
- gtests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
Whitespace-only changes.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1237 | 1237 | | |
1238 | 1238 | | |
1239 | 1239 | | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
| 1249 | + | |
| 1250 | + | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
1240 | 1254 | | |
1241 | 1255 | | |
1242 | 1256 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
466 | 466 | | |
467 | 467 | | |
468 | 468 | | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
469 | 473 | | |
470 | 474 | | |
471 | 475 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| 76 | + | |
76 | 77 | | |
77 | 78 | | |
78 | 79 | | |
| |||
0 commit comments