Set dwarf2 stash pointer earlier
authorAlan Modra <amodra@gmail.com>
Sun, 8 Jan 2023 02:26:21 +0000 (12:56 +1030)
committerAlan Modra <amodra@gmail.com>
Mon, 9 Jan 2023 23:39:11 +0000 (10:09 +1030)
This fixes a memory leak in the vanishingly rare cases (found by
fuzzers of course) when something goes wrong in the save_section_vma,
htab_create_alloc or alloc_trie_leaf calls before *pinfo is written.
If *pinfo is not written, _bfd_dwarf2_cleanup_debug_info won't be able
to free that memory.

* dwarf2.c (_bfd_dwarf2_slurp_debug_info): Save stash pointer
on setting up stash.

bfd/dwarf2.c

index f689eebbd88d2d68b624a768e037c4e6276c9683..bd431805d2af622810b4a555857e5f6a56683adf 100644 (file)
@@ -5408,6 +5408,7 @@ _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
       stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
       if (! stash)
        return false;
+      *pinfo = stash;
     }
   stash->orig_bfd = abfd;
   stash->debug_sections = debug_sections;
@@ -5433,8 +5434,6 @@ _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
   if (!stash->alt.trie_root)
     return false;
 
-  *pinfo = stash;
-
   if (debug_bfd == NULL)
     debug_bfd = abfd;