readelf: Handle NULL shdr in section_name

Message ID 20230406150408.476447-1-mark@klomp.org
State Committed
Headers
Series readelf: Handle NULL shdr in section_name |

Commit Message

Mark Wielaard April 6, 2023, 3:04 p.m. UTC
  In some error cases we want to show the section name but cannot
because the section header is corrupt or NULL. Make sure the
section_name always returns "???" in that case.

	* src/readelf.c (section_name): Check for shdr == NULL.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 src/readelf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Mark Wielaard April 14, 2023, 3:26 p.m. UTC | #1
Hi,

On Thu, 2023-04-06 at 17:04 +0200, Mark Wielaard wrote:
> In some error cases we want to show the section name but cannot
> because the section header is corrupt or NULL. Make sure the
> section_name always returns "???" in that case.
> 
> 	* src/readelf.c (section_name): Check for shdr == NULL.

Pushed,

Mark
  

Patch

diff --git a/src/readelf.c b/src/readelf.c
index 6950204e..4a1b985d 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -1606,7 +1606,7 @@  static const char *
 section_name (Ebl *ebl, GElf_Shdr *shdr)
 {
   size_t shstrndx;
-  if (elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
+  if (shdr == NULL || elf_getshdrstrndx (ebl->elf, &shstrndx) < 0)
     return "???";
   return elf_strptr (ebl->elf, shstrndx, shdr->sh_name) ?: "???";
 }