]> git.ipfire.org Git - pakfire.git/commitdiff
archive: Don't leak fd if archive could not be opened
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 14 Dec 2024 15:36:12 +0000 (15:36 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 14 Dec 2024 15:36:12 +0000 (15:36 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/archive.c

index 489c7f63941a9a64c69ca96905f16c12e20e7b9b..c71732e9cc73ac65ae659c9b9c7e72f155aeff2a 100644 (file)
@@ -163,6 +163,10 @@ static int archive_read_file_open(struct archive* a, FILE* f) {
 
        // Fetch the underlying file descriptor
        fd = fileno(f);
+
+       // Reset so we won't accidentally close it
+       f = NULL;
+
        if (fd < 0)
                goto ERROR;
 
@@ -176,6 +180,9 @@ static int archive_read_file_open(struct archive* a, FILE* f) {
        if (!f)
                goto ERROR;
 
+       // Reset file descriptor
+       fd = -EBADF;
+
        // Start reading at the beginning
        rewind(f);
 
@@ -200,6 +207,8 @@ ERROR:
 
        if (fd >= 0)
                close(fd);
+       if (f)
+               fclose(f);
 
        return ARCHIVE_FATAL;
 }