summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Wielaard <[email protected]>2018-06-05 22:27:25 +0200
committerMark Wielaard <[email protected]>2018-06-08 12:03:14 +0200
commit7009db70ca21d2b26cabd1c9fa1a50c35b6342b1 (patch)
tree87a21dfc259c0ab4990e1355d13d3332c7cd1894
parent50478b80cede080891996cf080581ca2a0611ce8 (diff)
libdw: Make sure dirarray is always properly freed in dwarf_getsrclines.
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 <[email protected]>
-rw-r--r--libdw/ChangeLog6
-rw-r--r--libdw/dwarf_getsrclines.c21
2 files changed, 18 insertions, 9 deletions
diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index b9f177de..f0ce901d 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,5 +1,11 @@
2018-06-05 Mark Wielaard <[email protected]>
+ * 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 <[email protected]>
+
* dwarf_getalt.c (find_debug_altlink): id_path array should be 2
larger to contain MAX_BUILD_ID_BYTES.
diff --git a/libdw/dwarf_getsrclines.c b/libdw/dwarf_getsrclines.c
index 790d4e49..0c2efaa9 100644
--- a/libdw/dwarf_getsrclines.c
+++ b/libdw/dwarf_getsrclines.c
@@ -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++)
{