]> git.ipfire.org Git - thirdparty/e2fsprogs.git/commitdiff
libext2fs: fix error paths after alocation failures
authorTheodore Ts'o <tytso@mit.edu>
Thu, 22 May 2025 00:16:08 +0000 (20:16 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Thu, 22 May 2025 00:16:08 +0000 (20:16 -0400)
Addresses-Coverity-Bug: 1647960
Addresses-Coverity-Bug: 1647961
Fixes: a5a6bcfb4820 ("libext2fs: make unix_io cache size configurable")
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
lib/ext2fs/unix_io.c

index dbe748d9c4358346c2d59c4056c353e2990f656b..6acce40530764db05a4f649bec86e325511973f1 100644 (file)
@@ -789,6 +789,7 @@ static errcode_t grow_cache(io_channel channel,
                retval = EXT2_ET_NO_MEMORY;
                goto unlock;
        }
+       data->cache = new_cache;
 
        for (i = data->cache_size, cache = new_cache + data->cache_size;
             i < new_size;
@@ -798,11 +799,16 @@ static errcode_t grow_cache(io_channel channel,
                cache->dirty = 0;
                cache->in_use = 0;
                retval = io_channel_alloc_buf(channel, 0, &cache->buf);
-               if (retval)
+               if (retval) {
+                       while (i > data->cache_size) {
+                               cache--; i--;
+                               if (cache->buf)
+                                       ext2fs_free_mem(&cache->buf);
+                       }
                        goto unlock;
+               }
        }
 
-       data->cache = new_cache;
        data->cache_size = new_size;
        data->cache_hash_shift = highbit32(data->cache_size);
 
@@ -1038,9 +1044,10 @@ cleanup:
        if (data) {
                if (data->dev >= 0)
                        close(data->dev);
-               free_cache(data);
-               if (data->cache)
+               if (data->cache) {
+                       free_cache(data);
                        free(data->cache);
+               }
                ext2fs_free_mem(&data);
        }
        if (io) {