public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: elfutils-devel@sourceware.org
Cc: Mark Wielaard <mark@klomp.org>
Subject: [PATCH] libdw: Make sure dirarray is always properly freed in dwarf_getsrclines.
Date: Tue, 05 Jun 2018 20:33:00 -0000	[thread overview]
Message-ID: <1528230786-5399-1-git-send-email-mark@klomp.org> (raw)

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           |  6 ++++++
 libdw/dwarf_getsrclines.c | 21 ++++++++++++---------
 2 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/libdw/ChangeLog b/libdw/ChangeLog
index b9f177d..f0ce901 100644
--- a/libdw/ChangeLog
+++ b/libdw/ChangeLog
@@ -1,5 +1,11 @@
 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
 	larger to contain MAX_BUILD_ID_BYTES.
 
diff --git a/libdw/dwarf_getsrclines.c b/libdw/dwarf_getsrclines.c
index 790d4e4..0c2efaa 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++)
     {
-- 
1.8.3.1

             reply	other threads:[~2018-06-05 20:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-05 20:33 Mark Wielaard [this message]
2018-06-08 10:08 ` Mark Wielaard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1528230786-5399-1-git-send-email-mark@klomp.org \
    --to=mark@klomp.org \
    --cc=elfutils-devel@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).