Hi Aaron,

On Thu, Jan 30, 2025 at 09:35:51PM -0500, Aaron Merey wrote:
> process_archive may be called with an fd argument of -1, which
> libelf interprets as "no file opened".  However when closing
> the fd process_archive does not check whether the fd is valid
> and may attempt to close an fd of -1.

Nice find. Less syscalls (that do nothing/just error) is always
better. I was puzzled for a moment how this could happen. But there is
a comment in offline.c already that explains:

  /* It is ok to pass fd == -1 here, because libelf uses it as a value for
     "no file opened" and supports working with files without fd, thanks to
     the existence of the elf_memory function.  */

Could you apply this patch before enabling valgrind --track-fds-yes?

Thanks,

Mark

> Signed-off-by: Aaron Merey <ame...@redhat.com>
> ---
>  libdwfl/offline.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/libdwfl/offline.c b/libdwfl/offline.c
> index 24e9e180..dc099d2b 100644
> --- a/libdwfl/offline.c
> +++ b/libdwfl/offline.c
> @@ -271,7 +271,8 @@ process_archive (Dwfl *dwfl, const char *name, const char 
> *file_name, int fd,
>       zero, that module will close FD.  If no modules survived the predicate,
>       we are all done with the file right here.  */
>    if (mod != NULL            /* If no modules, caller will clean up.  */
> -      && elf_end (archive) == 0)
> +      && elf_end (archive) == 0
> +      && fd >= 0)
>      close (fd);
>  
>    return mod;
> -- 
> 2.48.1
> 

Reply via email to