Hi Mark,
On Tue, Nov 24, 2026 at 6:06 PM Mark Wielaard <[email protected]> wrote:
>
> If the parent Elf is not of ELF_K_AR kind then elf_getaroff would
> assert. Just check and return an error like elf_getarhdr and
> elf_getarsym do. Also remove unused assert.h include.
>
> * libelf/elf_getarhdr.c: Remove unused assert.h include.
> * libelf/elf_getaroff.c: Likewise and remove assert, move
> parent check down and set libelf errno.
>
> Signed-off-by: Mark Wielaard <[email protected]>
LGTM.
Aaron
> ---
> libelf/elf_getarhdr.c | 1 -
> libelf/elf_getaroff.c | 11 +++++++----
> 2 files changed, 7 insertions(+), 5 deletions(-)
>
> diff --git a/libelf/elf_getarhdr.c b/libelf/elf_getarhdr.c
> index dcd5718ed77c..7c6cc3d4d9cc 100644
> --- a/libelf/elf_getarhdr.c
> +++ b/libelf/elf_getarhdr.c
> @@ -31,7 +31,6 @@
> # include <config.h>
> #endif
>
> -#include <assert.h>
> #include <libelf.h>
> #include <stddef.h>
>
> diff --git a/libelf/elf_getaroff.c b/libelf/elf_getaroff.c
> index 5737b3508494..18361b264f64 100644
> --- a/libelf/elf_getaroff.c
> +++ b/libelf/elf_getaroff.c
> @@ -31,7 +31,6 @@
> # include <config.h>
> #endif
>
> -#include <assert.h>
> #include <libelf.h>
> #include <stddef.h>
>
> @@ -42,12 +41,16 @@ int64_t
> elf_getaroff (Elf *elf)
> {
> /* Be gratious, the specs demand it. */
> - if (elf == NULL || elf->parent == NULL)
> + if (elf == NULL)
> return -1;
>
> - /* We can be sure the parent is an archive. */
> Elf *parent = elf->parent;
> - assert (parent->kind == ELF_K_AR);
> + /* Calling this function is not ok for any file type but archives. */
> + if (parent == NULL || parent->kind != ELF_K_AR)
> + {
> + __libelf_seterrno (ELF_E_INVALID_OP);
> + return -1;
> + }
>
> return elf->start_offset - sizeof (struct ar_hdr) - parent->start_offset;
> }
> --
> 2.56.1
>