Hi Anton,

On Thu, 2025-02-13 at 18:57 +0300, Anton Moryakov wrote:
> 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);

Again subject isn't super helpful and indentation is incorrect.
Also it is easier to switch the check around to:

+         if (h != NULL)
+           printf (_("Archive member '%s' contains:\n"), h->ar_name);

I made those changes and checked this in.

Thanks,

Mark

Reply via email to