asan: readelf: memory leaks
authorAlan Modra <amodra@gmail.com>
Sat, 14 Mar 2020 00:41:28 +0000 (11:11 +1030)
committerAlan Modra <amodra@gmail.com>
Sat, 14 Mar 2020 06:54:19 +0000 (17:24 +1030)
* readelf.c (dump_section_as_strings): Free memory on error exit.
(dump_section_as_bytes, process_notes_at): Likewise.
(get_build_id): Free enote.

binutils/ChangeLog
binutils/readelf.c

index d88ec4dacb2778d69def10a5971d880bfd099285..9a64b0de2458e722abbc172e5ec1dea6cbeec51d 100644 (file)
@@ -1,3 +1,9 @@
+2020-03-14  Alan Modra  <amodra@gmail.com>
+
+       * readelf.c (dump_section_as_strings): Free memory on error exit.
+       (dump_section_as_bytes, process_notes_at): Likewise.
+       (get_build_id): Free enote.
+
 2020-03-13  Kamil Rytarowski  <n54@gmx.com>
 
        * readelf.c (get_netbsd_elfcore_note_type): Add support for
index f36883cb645a736ecdf7a17ec572380e5e51aef2..b106e8c816f7daa50ca502a91edf6bb31cbbb64b 100644 (file)
@@ -13682,7 +13682,7 @@ dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
            {
              warn (_("section '%s' has unsupported compress type: %d\n"),
                    printable_section_name (filedata, section), chdr.ch_type);
-             return FALSE;
+             goto error_out;
            }
          uncompressed_size = chdr.ch_size;
          start += compression_header_size;
@@ -13714,7 +13714,7 @@ dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
            {
              error (_("Unable to decompress section %s\n"),
                     printable_section_name (filedata, section));
-             return FALSE;
+             goto error_out;
            }
        }
       else
@@ -13850,6 +13850,10 @@ dump_section_as_strings (Elf_Internal_Shdr * section, Filedata * filedata)
 
   putchar ('\n');
   return TRUE;
+
+error_out:
+  free (real_start);
+  return FALSE;
 }
 
 static bfd_boolean
@@ -13889,7 +13893,7 @@ dump_section_as_bytes (Elf_Internal_Shdr *  section,
            {
              warn (_("section '%s' has unsupported compress type: %d\n"),
                    printable_section_name (filedata, section), chdr.ch_type);
-             return FALSE;
+             goto error_out;
            }
          uncompressed_size = chdr.ch_size;
          start += compression_header_size;
@@ -13924,7 +13928,7 @@ dump_section_as_bytes (Elf_Internal_Shdr *  section,
              error (_("Unable to decompress section %s\n"),
                     printable_section_name (filedata, section));
              /* FIXME: Print the section anyway ?  */
-             return FALSE;
+             goto error_out;
            }
        }
       else
@@ -13934,7 +13938,7 @@ dump_section_as_bytes (Elf_Internal_Shdr *  section,
   if (relocate)
     {
       if (! apply_relocations (filedata, section, start, section_size, NULL, NULL))
-       return FALSE;
+       goto error_out;
     }
   else
     {
@@ -14004,6 +14008,10 @@ dump_section_as_bytes (Elf_Internal_Shdr *  section,
 
   putchar ('\n');
   return TRUE;
+
+ error_out:
+  free (real_start);
+  return FALSE;
 }
 
 static ctf_sect_t *
@@ -14317,7 +14325,10 @@ get_build_id (void * data)
       if (align < 4)
         align = 4;
       else if (align != 4 && align != 8)
-        continue;
+       {
+         free (enote);
+         continue;
+       }
 
       end = (char *) enote + length;
       data_remaining = end - (char *) enote;
@@ -14330,6 +14341,7 @@ get_build_id (void * data)
              warn (_("\
 malformed note encountered in section %s whilst scanning for build-id note\n"),
                    printable_section_name (filedata, shdr));
+             free (enote);
               continue;
             }
           data_remaining -= min_notesz;
@@ -14356,6 +14368,7 @@ malformed note encountered in section %s whilst scanning for build-id note\n"),
              warn (_("\
 malformed note encountered in section %s whilst scanning for build-id note\n"),
                    printable_section_name (filedata, shdr));
+             free (enote);
               continue;
             }
           data_remaining -= min_notesz;
@@ -14380,6 +14393,7 @@ malformed note encountered in section %s whilst scanning for build-id note\n"),
          warn (_("\
 malformed note encountered in section %s whilst scanning for build-id note\n"),
                printable_section_name (filedata, shdr));
+         free (enote);
           continue;
         }
 
@@ -14394,14 +14408,19 @@ malformed note encountered in section %s whilst scanning for build-id note\n"),
 
           build_id = malloc (inote.descsz * 2 + 1);
           if (build_id == NULL)
-           return NULL;
+           {
+             free (enote);
+             return NULL;
+           }
 
           for (j = 0; j < inote.descsz; ++j)
             sprintf (build_id + (j * 2), "%02x", inote.descdata[j] & 0xff);
           build_id[inote.descsz * 2] = '\0';
+         free (enote);
 
           return (unsigned char *) build_id;
         }
+      free (enote);
     }
 
   return NULL;
@@ -19425,7 +19444,10 @@ process_notes_at (Filedata *           filedata,
       if (pnotes)
        {
          if (! apply_relocations (filedata, section, (unsigned char *) pnotes, length, NULL, NULL))
-           return FALSE;
+           {
+             free (pnotes);
+             return FALSE;
+           }
        }
     }
   else
This page took 0.032859 seconds and 4 git commands to generate.