Report of the static analyzer:
DEREF_OF_NULL.RET Pointer, returned from function 'elf_getarhdr' at 
readelf.c:13551, 
may be NULL and is dereferenced at readelf.c:13553.

Corrections explained:
- Added a NULL check for the pointer returned by `elf_getarhdr`.
- If the pointer is NULL, release resources with `elf_end` and skip
  the current iteration using `continue`.

Triggers found by static analyzer Svace.

Signed-off-by: Anton Moryakov <ant.v.morya...@gmail.com>
---
 src/readelf.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/readelf.c b/src/readelf.c
index 6526db07..4c14fc21 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -13549,7 +13549,11 @@ dump_archive_index (Elf *elf, const char *fname)
                          as_off, fname, elf_errmsg (-1));
 
          const Elf_Arhdr *h = elf_getarhdr (subelf);
-
+         if (h == NULL)
+       {
+               elf_end(subelf);
+               continue;
+       }
          printf (_("Archive member '%s' contains:\n"), h->ar_name);
 
          elf_end (subelf);
-- 
2.30.2

Reply via email to