]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
objdump: Handle elf_getarhdr returning NULL in handle_ar
authorAnton Moryakov <ant.v.moryakov@gmail.com>
Tue, 4 Feb 2025 21:50:23 +0000 (00:50 +0300)
committerMark Wielaard <mark@klomp.org>
Fri, 7 Feb 2025 22:02:06 +0000 (23:02 +0100)
Report of the static analyzer:
Pointer, returned from function 'elf_getarhdr' at objdump.c:314,
may be NULL and is dereferenced at objdump.c:317. (CWE-476, CWE-690)

Corrections explained:
When processing archive elements, the code could dereference a NULL
pointer if 'elf_getarhdr' returns NULL. This patch adds a check to
ensure 'arhdr' is not NULL before using it. The fix ensures that the
function safely handles cases where 'elf_getarhdr' fails, avoiding
potential crashes.

Triggers found by static analyzer Svace.

Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
src/objdump.c

index 1b38da23266d3f628342d16ce735ab878727234c..1f72eab8d4693c2c7843b6179b2112dc00cdd15a 100644 (file)
@@ -313,7 +313,8 @@ handle_ar (int fd, Elf *elf, const char *prefix, const char *fname,
       Elf_Arhdr *arhdr = elf_getarhdr (subelf);
 
       /* Skip over the index entries.  */
-      if (strcmp (arhdr->ar_name, "/") != 0
+      if (arhdr != NULL
+         && strcmp (arhdr->ar_name, "/") != 0
          && strcmp (arhdr->ar_name, "//") != 0)
        {
          if (elf_kind (subelf) == ELF_K_ELF)