]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
open_filename: Free memory on error paths 2663/head
authorTobias Stoeckmann <tobias@stoeckmann.org>
Tue, 3 Jun 2025 15:51:28 +0000 (17:51 +0200)
committerTobias Stoeckmann <tobias@stoeckmann.org>
Thu, 5 Jun 2025 20:17:56 +0000 (22:17 +0200)
If opening a filename fails, make sure that allocated memory which is
not inserted into any remaining structure is freed.

Fixes https://github.com/libarchive/libarchive/issues/1949

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
libarchive/archive_read_open_filename.c
libarchive/archive_write_open_filename.c

index 5f5b3f1f7259f940ff2eee66e1a949c450fa7448..0fb72c9e04ac720739fe7af69c082b36a8c5469e 100644 (file)
@@ -136,8 +136,10 @@ archive_read_open_filenames(struct archive *a, const char **filenames,
                        mine->filename_type = FNT_STDIN;
                } else
                        mine->filename_type = FNT_MBS;
-               if (archive_read_append_callback_data(a, mine) != (ARCHIVE_OK))
+               if (archive_read_append_callback_data(a, mine) != (ARCHIVE_OK)) {
+                       free(mine);
                        return (ARCHIVE_FATAL);
+               }
                if (filenames == NULL)
                        break;
                filename = *(filenames++);
@@ -216,8 +218,10 @@ archive_read_open_filenames_w(struct archive *a, const wchar_t **wfilenames,
                        archive_string_free(&fn);
 #endif
                }
-               if (archive_read_append_callback_data(a, mine) != (ARCHIVE_OK))
+               if (archive_read_append_callback_data(a, mine) != (ARCHIVE_OK)) {
+                       free(mine);
                        return (ARCHIVE_FATAL);
+               }
                if (wfilenames == NULL)
                        break;
                wfilename = *(wfilenames++);
index 34209426558cf7b4207f58be2085086b1fab0355..633f7fe1794d31eca1ff9ca508c2998ac55fae62 100644 (file)
@@ -108,6 +108,7 @@ open_filename(struct archive *a, int mbs_fn, const void *filename)
        else
                r = archive_mstring_copy_wcs(&mine->filename, filename);
        if (r < 0) {
+               free(mine);
                if (errno == ENOMEM) {
                        archive_set_error(a, ENOMEM, "No memory");
                        return (ARCHIVE_FATAL);