]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
CAB reader: fix memory leak on repeated calls to archive_read_support_format_cab 2895/head
authorGrzegorz Antoniak <ga@anadoxin.org>
Sat, 7 Mar 2026 10:30:06 +0000 (11:30 +0100)
committerGrzegorz Antoniak <ga@anadoxin.org>
Sat, 7 Mar 2026 10:40:06 +0000 (11:40 +0100)
`archive_read_support_format_cab` allocates a fresh context structure on
each call before registering the CAB format with libarchive. On the
second call, however, the registration step reports the format is
already registered, so the function frees the newly allocated context
structure — it is not needed, since the one from the first call is
already in use.

The context structure contains a `ws` field of type `archive_wstring`.
During initialization, `archive_wstring_ensure` is called on that field,
which performs its own heap allocation.

The cleanup path described above frees the context structure without
also releasing the memory owned by the `ws` field, causing a leak.

Fixed by calling `archive_wstring_free` on the `ws` field before freeing
the context structure.

Fixes #1980.

libarchive/archive_read_support_format_cab.c

index 63755ef9e579fc6e113825a9b2db41fea0b15872..c4a67a72681ce98a522ec7923980f2102a9ab8a8 100644 (file)
@@ -383,8 +383,10 @@ archive_read_support_format_cab(struct archive *_a)
            NULL,
            NULL);
 
-       if (r != ARCHIVE_OK)
+       if (r != ARCHIVE_OK) {
+               archive_wstring_free(&cab->ws);
                free(cab);
+       }
        return (ARCHIVE_OK);
 }