public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: elfutils-devel@sourceware.org
Cc: David Malcolm <dmalcolm@redhat.com>, Mark Wielaard <mark@klomp.org>
Subject: [PATCH 1/7] libdwfl: Cleanup user_core resources on failure in dwfl_core_file_report.
Date: Sun, 10 May 2020 21:53:34 +0200	[thread overview]
Message-ID: <20200510195339.37191-2-mark@klomp.org> (raw)
In-Reply-To: <20200510195339.37191-1-mark@klomp.org>

GCC10 -fanalyzer noticed that we allocate, but don't always cleanup the
dwfl->user_core if it wasn't set yet on error. In theory dwfl_module_end
should take care of it, but it is cleaner and less confusing to just do
it here.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 libdwfl/ChangeLog   |  6 ++++++
 libdwfl/core-file.c | 17 ++++++++++++++++-
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index 3f9cd665..05d5bd4a 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,3 +1,9 @@
+2020-05-08  Mark Wielaard  <mark@klomp.org>
+
+	* libdwfl/core-file.c (dwfl_core_file_report): Keep track of
+	new bool cleanup_user_core and cleanup dwfl->user_core in error
+	case.
+
 2020-04-30  Mark Wielaard  <mark@klomp.org>
 
 	* find-debuginfo.c (dwfl_standard_find_debuginfo): When mod->dw
diff --git a/libdwfl/core-file.c b/libdwfl/core-file.c
index 01109f4b..a0ccc9b3 100644
--- a/libdwfl/core-file.c
+++ b/libdwfl/core-file.c
@@ -450,6 +450,7 @@ dwfl_core_file_report (Dwfl *dwfl, Elf *elf, const char *executable)
       return -1;
     }
 
+  bool cleanup_user_core = false;
   if (dwfl->user_core != NULL)
     free (dwfl->user_core->executable_for_core);
   if (executable == NULL)
@@ -461,6 +462,7 @@ dwfl_core_file_report (Dwfl *dwfl, Elf *elf, const char *executable)
     {
       if (dwfl->user_core == NULL)
 	{
+	  cleanup_user_core = true;
 	  dwfl->user_core = calloc (1, sizeof (struct Dwfl_User_Core));
 	  if (dwfl->user_core == NULL)
 	    {
@@ -472,6 +474,11 @@ dwfl_core_file_report (Dwfl *dwfl, Elf *elf, const char *executable)
       dwfl->user_core->executable_for_core = strdup (executable);
       if (dwfl->user_core->executable_for_core == NULL)
 	{
+	  if (cleanup_user_core)
+	    {
+	      free (dwfl->user_core);
+	      dwfl->user_core = NULL;
+	    }
 	  __libdwfl_seterrno (DWFL_E_NOMEM);
 	  return -1;
 	}
@@ -481,7 +488,15 @@ dwfl_core_file_report (Dwfl *dwfl, Elf *elf, const char *executable)
   GElf_Phdr notes_phdr;
   int ndx = dwfl_report_core_segments (dwfl, elf, phnum, &notes_phdr);
   if (unlikely (ndx <= 0))
-    return ndx;
+    {
+      if (cleanup_user_core)
+	{
+	  free (dwfl->user_core->executable_for_core);
+	  free (dwfl->user_core);
+	  dwfl->user_core = NULL;
+	}
+      return ndx;
+    }
 
   /* Next, we should follow the chain from DT_DEBUG.  */
 
-- 
2.20.1


  reply	other threads:[~2020-05-10 19:58 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-10 19:53 Some more GCC10 -fanalyzer inspired patches Mark Wielaard
2020-05-10 19:53 ` Mark Wielaard [this message]
2020-05-10 19:53 ` [PATCH 2/7] tests: Make sure to not call memcmp with NULL arguments Mark Wielaard
2020-05-10 19:53 ` [PATCH 3/7] libelf: Check __gelf_getehdr_rdlock call doesn't fail in elf_getdata Mark Wielaard
2020-05-10 19:53 ` [PATCH 4/7] libelf: Check for NULL shdr in elf_strptr Mark Wielaard
2020-05-10 19:53 ` [PATCH 5/7] src: Check ebl_openbackend result before using ebl handle Mark Wielaard
2020-05-10 19:53 ` [PATCH 6/7] libdwfl: Return failure from dwfl_standard_find_debuginfo for NULL module Mark Wielaard
2020-05-10 19:53 ` [PATCH 7/7] libcpu: Free unused new bitfield on error in i386_parse.y new_bitfield Mark Wielaard
2020-05-14 12:44 ` Some more GCC10 -fanalyzer inspired patches 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=20200510195339.37191-2-mark@klomp.org \
    --to=mark@klomp.org \
    --cc=dmalcolm@redhat.com \
    --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).