]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
libdwfl/offline.c: Avoid closing invalid fd
authorAaron Merey <amerey@redhat.com>
Fri, 31 Jan 2025 01:16:11 +0000 (20:16 -0500)
committerAaron Merey <amerey@redhat.com>
Fri, 7 Feb 2025 22:13:51 +0000 (17:13 -0500)
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.

Signed-off-by: Aaron Merey <amerey@redhat.com>
libdwfl/offline.c

index 24e9e1803b72b22eb528c7de26f8ce208328200a..dc099d2b3a3457754c1aa0fe4dffa4a3cdfa8ead 100644 (file)
@@ -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;