]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
libdw: Make sure dirarray is always properly freed in dwarf_getsrclines.
authorMark Wielaard <mark@klomp.org>
Tue, 5 Jun 2018 20:27:25 +0000 (22:27 +0200)
committerMark Wielaard <mark@klomp.org>
Fri, 8 Jun 2018 10:03:14 +0000 (12:03 +0200)
If there were more than 256 directories in the table and there was
illegal DWARF before we read them all, then we might not free the
dirarray (or the wrong one). Fix by defining the dirarray early
(before the first data sanity check) and making sure it is not
(still) equal to dirstack before freeing.

Signed-off-by: Mark Wielaard <mark@klomp.org>
libdw/ChangeLog
libdw/dwarf_getsrclines.c

index b9f177dee9f391e2e7e076eed94fd7fb2932280e..f0ce901de142ede3c596f7e361d531175a9fb0d3 100644 (file)
@@ -1,3 +1,9 @@
+2018-06-05  Mark Wielaard  <mark@klomp.org>
+
+       * dwarf_getsrclines.c (read_srclines): Define dirarray early and
+       check whether or not it is equal to dirstack on exit/out before
+       cleanup.
+
 2018-06-05  Mark Wielaard  <mark@klomp.org>
 
        * dwarf_getalt.c (find_debug_altlink): id_path array should be 2
index 790d4e49beafd5edbd7bb4eff215773ecaf6f5fb..0c2efaa90d149dfa0b9633c1d4c327014e53b197 100644 (file)
@@ -182,6 +182,17 @@ read_srclines (Dwarf *dbg,
       .discriminator = 0
     };
 
+  /* The dirs normally go on the stack, but if there are too many
+     we alloc them all.  Set up stack storage early, so we can check on
+     error if we need to free them or not.  */
+  struct dirlist
+  {
+    const char *dir;
+    size_t len;
+  };
+  struct dirlist dirstack[MAX_STACK_DIRS];
+  struct dirlist *dirarray = dirstack;
+
   if (unlikely (linep + 4 > lineendp))
     {
     invalid_data:
@@ -347,14 +358,6 @@ read_srclines (Dwarf *dbg,
        goto invalid_data;
     }
 
-  struct dirlist
-  {
-    const char *dir;
-    size_t len;
-  };
-  struct dirlist dirstack[MAX_STACK_DIRS];
-  struct dirlist *dirarray = dirstack;
-
   /* Arrange the list in array form.  */
   ndirlist = ndirs;
   if (ndirlist >= MAX_STACK_DIRS)
@@ -1051,7 +1054,7 @@ read_srclines (Dwarf *dbg,
       free (state.linelist);
       state.linelist = ll;
     }
-  if (ndirlist >= MAX_STACK_DIRS)
+  if (dirarray != dirstack)
     free (dirarray);
   for (size_t i = MAX_STACK_FILES; i < nfilelist; i++)
     {