]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
PR32109, aborting at bfd/bfd.c:1236 in int _bfd_doprnt
authorAlan Modra <amodra@gmail.com>
Sun, 25 Aug 2024 05:50:21 +0000 (15:20 +0930)
committerAlan Modra <amodra@gmail.com>
Mon, 26 Aug 2024 00:10:55 +0000 (09:40 +0930)
Since bfd_section for .strtab isn't set, print the section index
instead.  Also, don't return NULL on this error as that results in
multiple mmap/read of the string table.  (We could return NULL if we
arranged to set sh_size zero first, but just what we do with fuzzed
object files is of no concern, and terminating the table might make a
faulty object file usable.)

PR 32109
* elf.c (bfd_elf_get_str_section): Remove outdated comment, and
tweak shstrtabsize test to suit.  Don't use string tab bfd_section
in error message, use index instead.  Don't return NULL on
unterminated string section, terminate it.
(_bfd_elf_get_dynamic_symbols): Similarly terminate string table
section.

bfd/elf.c

index bc4a2de1d3bddf1f54e3c714e33d15e263c135be..9fe031da963a0207a90e5d451464608b769d841e 100644 (file)
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -285,9 +285,7 @@ bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
       offset = i_shdrp[shindex]->sh_offset;
       shstrtabsize = i_shdrp[shindex]->sh_size;
 
-      /* Allocate and clear an extra byte at the end, to prevent crashes
-        in case the string table is not terminated.  */
-      if (shstrtabsize + 1 <= 1
+      if (shstrtabsize == 0
          || bfd_seek (abfd, offset, SEEK_SET) != 0
          || (shstrtab
              = _bfd_mmap_readonly_persistent (abfd, shstrtabsize)) == NULL)
@@ -297,14 +295,13 @@ bfd_elf_get_str_section (bfd *abfd, unsigned int shindex)
             the string table over and over.  */
          i_shdrp[shindex]->sh_size = 0;
        }
-      else if (shstrtab[shstrtabsize - 1] != '\0')
+      else if (shstrtab[shstrtabsize - 1] != 0)
        {
          /* It is an error if a string table isn't terminated.  */
          _bfd_error_handler
            /* xgettext:c-format */
-           (_("%pB(%pA): string table is corrupt"),
-            abfd, i_shdrp[shindex]->bfd_section);
-         return NULL;
+           (_("%pB: string table [%u] is corrupt"), abfd, shindex);
+         shstrtab[shstrtabsize - 1] = 0;
        }
       i_shdrp[shindex]->contents = shstrtab;
     }
@@ -1914,7 +1911,7 @@ _bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr,
       _bfd_error_handler
        /* xgettext:c-format */
        (_("%pB: DT_STRTAB table is corrupt"), abfd);
-      goto error_return;
+      strbuf[dt_strsz - 1] = 0;
     }
 
   /* Get the real symbol count from DT_HASH or DT_GNU_HASH.  Prefer