[Gold] Don't get a section name if the offset is bad

Message ID CA+pbi+4wArufUvOdbL+ojXT4h=wA4Rqv+a-d9=_OfEeFYH6-pA@mail.gmail.com
State New
Headers show
Series
  • [Gold] Don't get a section name if the offset is bad
Related show

Commit Message

Alan Modra via Binutils Sept. 3, 2021, 8:53 p.m.
This is a fix for: https://sourceware.org/bugzilla/show_bug.cgi?id=26765

When getting an offset value that doesn't exist in the section name string
table, we shouldn't try to get the section name, rather just return from
the function.

Thanks,
Miguel Saldivar

Patch

diff --git a/elfcpp/ChangeLog b/elfcpp/ChangeLog
index 0d149351e8a..490b4fd025d 100644
--- a/elfcpp/ChangeLog
+++ b/elfcpp/ChangeLog
@@ -1,3 +1,8 @@ 
+2021-09-03  Miguel Saldivar  <saldivarcher@gmail.com>
+
+       * elfcpp_file.h (Elf_file::section_name): Do not get section name,
if the
+       offset is bad.
+
 2021-07-03  Nick Clifton  <nickc@redhat.com>

        * 2.37 release branch created.
diff --git a/elfcpp/elfcpp_file.h b/elfcpp/elfcpp_file.h
index 8d4aac8bbae..56811bf9740 100644
--- a/elfcpp/elfcpp_file.h
+++ b/elfcpp/elfcpp_file.h
@@ -532,9 +532,12 @@  Elf_file<size, big_endian,
File>::section_name(unsigned int shndx) const
     shstr_size = shstr_shdr.get_sh_size();
   }

-  if (sh_name >= shstr_size)
+  if (sh_name >= shstr_size) {
     file->error(_("bad section name offset for section %u: %u"),
                shndx, sh_name);
+    // Don't try to get the section name if the offset is bad.
+    return std::string();
+  }

   typename File::View v(file->view(shstr_off, shstr_size));