Package: makedepf90
Version: 3.0.1-3
Severity: normal
Tags: patch
X-Debbugs-Cc: [email protected]

Three more out-of-bounds accesses in the same class as #1155574, found
under AddressSanitizer/UBSan. Unlike that one, these are reachable from
ordinary builds:

1. #if nesting deeper than 19 levels writes past skip_to_end[20]
(find_dep.y). skip_i is also never reset between files, so unclosed
conditionals accumulate across the command line.
UBSan: index out of bounds for type 'bool[20]', find_dep.y

2. modfile_name() expands into a fixed 256-byte heap buffer. With the
default options, any module in a source file whose path is longer than
~255 characters overflows it (plausible in deep CI work directories).
ASan: heap-buffer-overflow write, modfile_name.c:53/:60

3. replace_suffix() and set_path() test filename[n] before n >= 0, so a
name with no '.' or '/' reads filename[-1]. With -b DIR this happens
for every name without a directory part.
ASan: heap-buffer-overflow read, utils.c:88

Reproducers:

python3 -c "print('#ifdef X\n'*25 + '#endif\n'*25)" > nest.F90
makedepf90 nest.F90

d=$(python3 -c "print('/'.join(['d'*40]*7))"); mkdir -p $d
printf 'module m\nend module\n' > $d/m.F90
makedepf90 $d/m.F90

printf 'module m\nend module\n' > m.f90
makedepf90 -b d m.f90

The fixes are minimal: deeper nesting shares the last stack slot, the
buffer is sized from its inputs, and the operands are swapped. Dependency
output is byte-identical before and after on 182 option/test combinations
and on a 316-file Fortran model tree. Patches against 3.0.1 (apply on top
of std23.patch and the #1155574 patch; also attached as DEP-3 patches):

--- a/find_dep.y
+++ b/find_dep.y
@@ -44,7 +44,10 @@ static List *macrolist;
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];
+#define SKIP_DEPTH 20
+static bool skip_to_end[SKIP_DEPTH];
+/* Nesting deeper than SKIP_DEPTH shares the last slot instead of
overflowing. */
+#define SKIP_TO_END skip_to_end[skip_i < SKIP_DEPTH ? skip_i : SKIP_DEPTH
- 1]
static int skip_i;
static bool in_interface = false;

@@ -191,7 +194,7 @@ keyword_stmt: WORD EOSTMT {
else if (!list_find(macrolist, mac, &macrocmp))
pp_ignore = 1;
else
- skip_to_end[skip_i] = true;
+ SKIP_TO_END = true;

macro_free(mac);
}
@@ -207,7 +210,7 @@ keyword_stmt: WORD EOSTMT {
if (list_find(macrolist, mac, &macrocmp))
pp_ignore = 1;
else
- skip_to_end[skip_i] = true;
+ SKIP_TO_END = true;

macro_free(mac);
}
@@ -217,21 +220,21 @@ keyword_stmt: WORD EOSTMT {
* following #else or #elif:s (ie. no 'skip_to_end'). */
skip_i++;
if (pp_ignore) pp_ignore++;
- skip_to_end[skip_i] = false;
+ SKIP_TO_END = 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_TO_END && pp_ignore == 0) pp_ignore = 1;
}
| else other EOSTMT {
- if (pp_ignore == 1 && skip_to_end[skip_i] == false)
+ if (pp_ignore == 1 && SKIP_TO_END == false)
pp_ignore = 0;
else if (pp_ignore == 0)
pp_ignore = 1;
}
| endif other EOSTMT {
- skip_to_end[skip_i] = false;
+ SKIP_TO_END = false;
if (skip_i > 0) skip_i--;
if (pp_ignore) pp_ignore--;
}

--- a/modfile_name.c
+++ b/modfile_name.c
@@ -39,10 +39,16 @@ char *modfile_name (const char *modulename,
char *modfile;
const char *fmt;
int mi = 0, j, i;
+ size_t maxarg;

fmt = (submodulename) ? options.submodfile_fmt : options.modfile_fmt;

- modfile = (char *)xmalloc (sizeof(char)*MODFILE_NAME_LEN);
+ /* No format character expands to more than the longest name. */
+ maxarg = strlen(filename);
+ if (strlen(modulename) > maxarg) maxarg = strlen(modulename);
+ if (submodulename && strlen(submodulename) > maxarg)
+ maxarg = strlen(submodulename);
+ modfile = (char *)xmalloc (strlen(fmt) * (maxarg + 1) + 1);

for (i = 0; fmt[i]; i++) {
if (fmt[i] == '%') {

--- a/utils.c
+++ b/utils.c
@@ -52,7 +52,7 @@ char *replace_suffix(const char *filename, const char
*new_suffix)

/* Search for last '.' in filename */
fl = n = strlen(filename);
- while (filename[n] != '.' && n >= 0) n--;
+ while (n >= 0 && filename[n] != '.') n--;

if (n == -1) {
/* if there was no '.' */
@@ -85,7 +85,7 @@ char *set_path(const char *filename, const char *path,
const bool mirror)

fl = n = strlen(filename);
if( !mirror )
- while (filename[n] != '/' && n >= 0) n--;
+ while (n >= 0 && filename[n] != '/') n--;
else
n=-1;
nl = fl - n - 1;

Thanks for maintaining this.
Description: Bound the preprocessor nesting stack to prevent a global buffer overflow
 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.  Reproduced with UBSan: "index 20 out of bounds for type
 'bool[20]'" at find_dep.y, yyparse.
 .
 Levels beyond the array now share the last slot.  That changes behaviour only
 where the old code had undefined behaviour; dependency output for nesting of
 19 or fewer levels is unchanged.
Author: Ash Pera <[email protected]>
Forwarded: no
Last-Updated: 2026-11-27
--- a/find_dep.y
+++ b/find_dep.y
@@ -44,7 +44,10 @@ static List *macrolist;
 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];
+#define SKIP_DEPTH 20
+static bool skip_to_end[SKIP_DEPTH];
+/* Nesting deeper than SKIP_DEPTH shares the last slot instead of overflowing. */
+#define SKIP_TO_END skip_to_end[skip_i < SKIP_DEPTH ? skip_i : SKIP_DEPTH - 1]
 static int skip_i;
 static bool in_interface = false;
 
@@ -191,7 +194,7 @@ keyword_stmt: WORD EOSTMT {
         else if (!list_find(macrolist, mac, &macrocmp))
             pp_ignore = 1;
         else
-            skip_to_end[skip_i] = true;
+            SKIP_TO_END = true;
 
         macro_free(mac);
     }
@@ -207,7 +210,7 @@ keyword_stmt: WORD EOSTMT {
         if (list_find(macrolist, mac, &macrocmp))
             pp_ignore = 1;
         else
-            skip_to_end[skip_i] = true;
+            SKIP_TO_END = true;
 
         macro_free(mac);
     }
@@ -217,21 +220,21 @@ keyword_stmt: WORD EOSTMT {
          * following #else or #elif:s (ie. no 'skip_to_end'). */
         skip_i++;
         if (pp_ignore)  pp_ignore++;
-        skip_to_end[skip_i] = false;
+        SKIP_TO_END = 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_TO_END && pp_ignore == 0)  pp_ignore = 1;
     }
     | else other EOSTMT {
-        if (pp_ignore == 1 && skip_to_end[skip_i] == false)  
+        if (pp_ignore == 1 && SKIP_TO_END == false)  
             pp_ignore = 0;
         else if (pp_ignore == 0)
             pp_ignore = 1;
     }
     | endif other EOSTMT {
-        skip_to_end[skip_i] = false;
+        SKIP_TO_END = false;
         if (skip_i > 0)  skip_i--;
         if (pp_ignore) pp_ignore--;
     }
Description: Size the module-file name buffer from its inputs to prevent a heap overflow
 modfile_name() expands the -m/-s format into a fixed MODFILE_NAME_LEN (256)
 byte heap buffer with no bound.  A module name, submodule name or source path
 longer than about 255 characters (the default format is "%f.o", so a long
 source path is enough), or a long -m/-s format, writes past the buffer.
 Reproduced with AddressSanitizer: heap-buffer-overflow write at
 modfile_name.c, modfile_name.
 .
 The buffer is now allocated with room for every format character expanding to
 the longest of the three names, so no name is truncated.  Output is unchanged.
Author: Ash Pera <[email protected]>
Forwarded: no
Last-Updated: 2026-11-27
--- a/modfile_name.c
+++ b/modfile_name.c
@@ -39,10 +39,16 @@ char *modfile_name (const char *modulename,
     char *modfile;
     const char *fmt;
     int mi = 0, j, i;
+    size_t maxarg;
 
     fmt = (submodulename) ?  options.submodfile_fmt : options.modfile_fmt;
 
-    modfile = (char *)xmalloc (sizeof(char)*MODFILE_NAME_LEN);
+    /* No format character expands to more than the longest name. */
+    maxarg = strlen(filename);
+    if (strlen(modulename) > maxarg)  maxarg = strlen(modulename);
+    if (submodulename && strlen(submodulename) > maxarg)
+        maxarg = strlen(submodulename);
+    modfile = (char *)xmalloc (strlen(fmt) * (maxarg + 1) + 1);
 
     for (i = 0; fmt[i]; i++) {
         if (fmt[i] == '%') {
Description: Test the index before reading in replace_suffix() and set_path()
 Both functions scan backwards with "while (filename[n] != c && n >= 0)",
 which reads filename[-1] before testing n when the name has no '.' or '/'.
 With -b DIR this happens for every object and module name without a
 directory part.  Reproduced with AddressSanitizer: heap-buffer-overflow read
 one byte before the buffer at utils.c, set_path.
 .
 The operands are swapped.  Output is unchanged.
Author: Ash Pera <[email protected]>
Forwarded: no
Last-Updated: 2026-11-27
--- a/utils.c
+++ b/utils.c
@@ -52,7 +52,7 @@ char *replace_suffix(const char *filename, const char *new_suffix)
 
     /* Search for last '.' in filename */
     fl = n = strlen(filename);
-    while (filename[n] != '.' && n >= 0)  n--;
+    while (n >= 0 && filename[n] != '.')  n--;
 
     if (n == -1) {
         /* if there was no '.' */
@@ -85,7 +85,7 @@ char *set_path(const char *filename, const char *path, const bool mirror)
 
     fl = n = strlen(filename);
     if( !mirror )
-        while (filename[n] != '/' && n >= 0)  n--;
+        while (n >= 0 && filename[n] != '/')  n--;
     else
         n=-1;
     nl = fl - n - 1;

Reply via email to