The #if nesting fix in my report is wrong; please use the attached
replacement.
Sharing the last skip_to_end slot loses per-level state, so at a
nesting depth of 20 or more an inner #elif or #else takes the wrong
branch, and makedepf90 emits a dependency from code that is not
compiled. Reproducer (depth 27):
{ for i in $(seq 25); do echo '#if 1'; done
printf 'module u\n#ifdef A\n#if 1\n#endif\n#elif 1\n use
wrongmod\n#endif\nend module u\n'
for i in $(seq 25); do echo '#endif'; done; } > nest.F90
printf 'module wrongmod\nend module wrongmod\n' > wrongmod.f90
makedepf90 -DA nest.F90 wrongmod.f90
Correct output is "nest.o : nest.F90"; my earlier patch also lists
wrongmod.o.
The attached patch grows the stack on demand with xrealloc. Its output
matches unpatched 3.0.1 on this reproducer at depths 19, 20, 21 and 42,
and on 432 dependency runs over a source corpus. At 300 levels an
unpatched build crashes (#ifdef) or never exits (#if 1); the patched
one finishes, clean under ASan/UBSan.
The long-path and -b patches are unchanged.
Thanks,
Ash
Description: Grow the preprocessor nesting stack instead of overflowing it
skip_to_end[] has 20 slots, but every #if/#ifdef/#ifndef (and $if/$ifdef/
$ifndef) increments skip_i unconditionally and then indexes skip_to_end[skip_i]
(find_dep.y). Nesting deeper than 19 levels writes past the array. skip_i is
also not reset between files, so unclosed conditionals accumulate across a
command line. At 300 levels an unpatched build crashes, or for "#if 1" never
exits.
Reproduced with UBSan: "index 20 out of bounds for type
'bool[20]'" at find_dep.y, yyparse.
.
The stack now grows on demand, so every nesting level keeps its own slot and
dependency output is the same as it was for nesting within the old limit.
Author: Ash Pera <[email protected]>
Bug-Debian: https://bugs.debian.org/1155613
Forwarded: https://bugs.debian.org/1155613
Last-Updated: 2026-11-28
--- a/find_dep.y
+++ b/find_dep.y
@@ -44,13 +44,27 @@
static char *filestack[INCLUDE_RECURSION_LIMIT+1];
static int filestack_i;
static int pp_ignore; /* In 'false'-branch of a pre-processor 'if' */
-static bool skip_to_end[20];
+static bool *skip_to_end; /* One per #if nesting level; grown on demand. */
+static int skip_cap;
static int skip_i;
static bool in_interface = false;
int yyerror(const char *s);
static int modcmp(const void *m1, const void *m2);
+
+/* The skip_to_end slot of the current nesting level, growing the stack as
+ * needed: a fixed size either overflows or merges levels. */
+static bool *skip_slot(void)
+{
+ if (skip_i >= skip_cap) {
+ int n = skip_i + 32;
+ skip_to_end = xrealloc(skip_to_end, n * sizeof *skip_to_end);
+ memset(skip_to_end + skip_cap, 0, (n - skip_cap) * sizeof *skip_to_end);
+ skip_cap = n;
+ }
+ return &skip_to_end[skip_i];
+}
SourceFmt get_format(const char *filename);
void handle_include(const char *incfile);
@@ -191,7 +205,7 @@
else if (!list_find(macrolist, mac, ¯ocmp))
pp_ignore = 1;
else
- skip_to_end[skip_i] = true;
+ *skip_slot() = true;
macro_free(mac);
}
@@ -207,7 +221,7 @@
if (list_find(macrolist, mac, ¯ocmp))
pp_ignore = 1;
else
- skip_to_end[skip_i] = true;
+ *skip_slot() = true;
macro_free(mac);
}
@@ -217,21 +231,21 @@
* following #else or #elif:s (ie. no 'skip_to_end'). */
skip_i++;
if (pp_ignore) pp_ignore++;
- skip_to_end[skip_i] = false;
+ *skip_slot() = false;
}
| elif other EOSTMT {
/* Allways taken unless an #ifdef or #ifndef-branch has been taken
* allready. */
- if (skip_to_end[skip_i] && pp_ignore == 0) pp_ignore = 1;
+ if (*skip_slot() && pp_ignore == 0) pp_ignore = 1;
}
| else other EOSTMT {
- if (pp_ignore == 1 && skip_to_end[skip_i] == false)
+ if (pp_ignore == 1 && *skip_slot() == false)
pp_ignore = 0;
else if (pp_ignore == 0)
pp_ignore = 1;
}
| endif other EOSTMT {
- skip_to_end[skip_i] = false;
+ *skip_slot() = false;
if (skip_i > 0) skip_i--;
if (pp_ignore) pp_ignore--;
}