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: [COMMITTED] libdwfl: Don't call realloc with zero in cu.c addraranges.
Date: Sun, 28 Apr 2019 11:26:00 -0000	[thread overview]
Message-ID: <20190428112618.21719-1-mark@klomp.org> (raw)

Calling realloc when naranges is zero will result is trying to free
aranges. If realloc does free aranges it returns NULL, which means
aranges is still assigned. This is likely not a problem, because in
most cases aranges will be NULL already. But if it was not and
naranges does turn out to be zero after reduction (which would be
invalid DWARF) we are left with a dangling pointer.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 libdwfl/ChangeLog | 4 ++++
 libdwfl/cu.c      | 7 +++++--
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index 1da888f6f..3e19d9bd1 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,3 +1,7 @@
+2019-04-28  Mark Wielaard  <mark@klomp.org>
+
+	* cu.c (addrarange): Only call realloc when naranges is not zero.
+
 2019-03-27  Mark Wielaard  <mark@klomp.org>
 
 	* dwfl_segment_report_module.c (dwfl_segment_report_module): Check
diff --git a/libdwfl/cu.c b/libdwfl/cu.c
index 94bfad8df..4de66248b 100644
--- a/libdwfl/cu.c
+++ b/libdwfl/cu.c
@@ -83,8 +83,11 @@ addrarange (Dwfl_Module *mod, Dwarf_Addr addr, struct dwfl_arange **arange)
 
       /* Store the final array, which is probably much smaller than before.  */
       mod->naranges = naranges;
-      mod->aranges = (realloc (aranges, naranges * sizeof aranges[0])
-		      ?: aranges);
+      if (naranges > 0)
+        mod->aranges = (realloc (aranges, naranges * sizeof aranges[0])
+			?: aranges);
+      else if (aranges != NULL)
+	free (aranges);
       mod->lazycu += naranges;
     }
 
-- 
2.20.1

                 reply	other threads:[~2019-04-28 11:26 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20190428112618.21719-1-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).