]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
libdwfl: Cleanup user_core resources on failure in dwfl_core_file_report.
authorMark Wielaard <mark@klomp.org>
Fri, 8 May 2020 21:35:12 +0000 (23:35 +0200)
committerMark Wielaard <mark@klomp.org>
Thu, 14 May 2020 12:30:57 +0000 (14:30 +0200)
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
libdwfl/core-file.c

index 3f9cd6659b8ff4ca6cb9ba5f3ea5e1aabf5f19a5..05d5bd4ad6a911dfa27a18ba7fba079ae6b8c77a 100644 (file)
@@ -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
index 01109f4bd675a42b4d755bdc5139567f5c567236..a0ccc9b3daf6564634ba1592d36a92ba0eeab2a1 100644 (file)
@@ -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.  */