public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [COMMITTED] nm: Simplify naming of invalid sections, check shdr isn't NULL.
@ 2019-04-28 15:57 Mark Wielaard
  0 siblings, 0 replies; only message in thread
From: Mark Wielaard @ 2019-04-28 15:57 UTC (permalink / raw)
  To: elfutils-devel; +Cc: Mark Wielaard

When shdr is NULL or the sh_name index is invalid, don't try to use
it.  Just call the section "[invalid section name]". Don't try to be
too smart by creating a dynamic invalid name using alloca to simplify
memory usage in this exceptional case.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 src/ChangeLog |  5 +++++
 src/nm.c      | 14 ++++++--------
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 3786f3432..3020bd768 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,8 @@
+2019-04-28  Mark Wielaard  <mark@klomp.org>
+
+	* nm.c (show_symbols_sysv): Check gelf_getshdr doesn't return
+	NULL. Simplify naming of invalid sections, don't use alloca.
+
 2019-04-28  Mark Wielaard  <mark@klomp.org>
 
 	* elfcmp.c (main): Check shdr1 and shdr2 are not NULL.
diff --git a/src/nm.c b/src/nm.c
index ffe8ca691..da1350b4c 100644
--- a/src/nm.c
+++ b/src/nm.c
@@ -751,19 +751,17 @@ show_symbols_sysv (Ebl *ebl, GElf_Word strndx, const char *fullname,
   while ((scn = elf_nextscn (ebl->elf, scn)) != NULL)
     {
       GElf_Shdr shdr_mem;
+      GElf_Shdr *shdr;
 
       assert (elf_ndxscn (scn) == cnt);
       cnt++;
 
-      char *name = elf_strptr (ebl->elf, shstrndx,
-			       gelf_getshdr (scn, &shdr_mem)->sh_name);
+      char *name = NULL;
+      shdr = gelf_getshdr (scn, &shdr_mem);
+      if (shdr != NULL)
+	name = elf_strptr (ebl->elf, shstrndx, shdr->sh_name);
       if (unlikely (name == NULL))
-	{
-	  const size_t bufsz = sizeof "[invalid sh_name 0x12345678]";
-	  name = alloca (bufsz);
-	  snprintf (name, bufsz, "[invalid sh_name %#" PRIx32 "]",
-		    gelf_getshdr (scn, &shdr_mem)->sh_name);
-	}
+	name = "[invalid section name]";
       scnnames[elf_ndxscn (scn)] = name;
     }
 
-- 
2.20.1

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-04-28 15:57 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-28 15:57 [COMMITTED] nm: Simplify naming of invalid sections, check shdr isn't NULL Mark Wielaard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).