]> git.ipfire.org Git - thirdparty/e2fsprogs.git/commitdiff
hashmap: change return value type of ext2fs_hashmap_add()
authorZhiqiang Liu <liuzhiqiang26@huawei.com>
Wed, 28 Jul 2021 01:56:47 +0000 (09:56 +0800)
committerTheodore Ts'o <tytso@mit.edu>
Tue, 3 Aug 2021 02:25:18 +0000 (22:25 -0400)
In ext2fs_hashmap_add(), new entry is allocated by calling
malloc(). If malloc() return NULL, it will cause a
segmentation fault problem.

Here, we change return value type of ext2fs_hashmap_add()
from void to int. If allocating new entry fails, we will
return -1, and the callers should also verify the return
value of ext2fs_hashmap_add().

Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
Signed-off-by: Wu Guanghao <wuguanghao3@huawei.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
contrib/android/base_fs.c
lib/ext2fs/fileio.c
lib/ext2fs/hashmap.c
lib/ext2fs/hashmap.h

index 652317e265c5645e4965e3a458510a41bc7b62e5..d3e00d189baa2be5bad5916fe93284768a74b16d 100644 (file)
@@ -110,10 +110,16 @@ struct ext2fs_hashmap *basefs_parse(const char *file, const char *mountpoint)
        if (!entries)
                goto end;
 
-       while ((entry = basefs_readline(f, mountpoint, &err)))
-               ext2fs_hashmap_add(entries, entry, entry->path,
+       while ((entry = basefs_readline(f, mountpoint, &err))) {
+               err = ext2fs_hashmap_add(entries, entry, entry->path,
                                   strlen(entry->path));
-
+               if (err) {
+                       free_base_fs_entry(entry);
+                       fclose(f);
+                       ext2fs_hashmap_free(entries);
+                       return NULL;
+               }
+       }
        if (err) {
                fclose(f);
                ext2fs_hashmap_free(entries);
index a0b5d9714546bbb5f3fe22e5030b0bb519cb55ad..818f7f054200292f2f34753d25dbaedeca241c65 100644 (file)
@@ -475,8 +475,14 @@ errcode_t ext2fs_file_write(ext2_file_t file, const void *buf,
 
                        if (new_block) {
                                new_block->physblock = file->physblock;
-                               ext2fs_hashmap_add(fs->block_sha_map, new_block,
-                                       new_block->sha, sizeof(new_block->sha));
+                               int ret = ext2fs_hashmap_add(fs->block_sha_map,
+                                               new_block, new_block->sha,
+                                               sizeof(new_block->sha));
+                               if (ret) {
+                                       retval = EXT2_ET_NO_MEMORY;
+                                       free(new_block);
+                                       goto fail;
+                               }
                        }
 
                        if (bmap_flags & BMAP_SET) {
index ffe61ce95a3b2842cc407cb217d16a89d293eb48..697b2bcc669e6db337927b1b1388a6711fbc2109 100644 (file)
@@ -36,6 +36,9 @@ struct ext2fs_hashmap *ext2fs_hashmap_create(
 {
        struct ext2fs_hashmap *h = calloc(sizeof(struct ext2fs_hashmap) +
                                sizeof(struct ext2fs_hashmap_entry) * size, 1);
+       if (!h)
+               return NULL;
+
        h->size = size;
        h->free = free_fct;
        h->hash = hash_fct;
@@ -43,12 +46,15 @@ struct ext2fs_hashmap *ext2fs_hashmap_create(
        return h;
 }
 
-void ext2fs_hashmap_add(struct ext2fs_hashmap *h, void *data, const void *key,
-                       size_t key_len)
+int ext2fs_hashmap_add(struct ext2fs_hashmap *h,
+                      void *data, const void *key, size_t key_len)
 {
        uint32_t hash = h->hash(key, key_len) % h->size;
        struct ext2fs_hashmap_entry *e = malloc(sizeof(*e));
 
+       if (!e)
+               return -1;
+
        e->data = data;
        e->key = key;
        e->key_len = key_len;
@@ -62,6 +68,8 @@ void ext2fs_hashmap_add(struct ext2fs_hashmap *h, void *data, const void *key,
        h->first = e;
        if (!h->last)
                h->last = e;
+
+       return 0;
 }
 
 void *ext2fs_hashmap_lookup(struct ext2fs_hashmap *h, const void *key,
index dcfa74553489f5e0aa3599b5c2f9d09e2b6578bb..0c09d2bde000f8ca274c66e2f0f90b1fc623bfd7 100644 (file)
@@ -27,8 +27,8 @@ struct ext2fs_hashmap_entry {
 struct ext2fs_hashmap *ext2fs_hashmap_create(
                                uint32_t(*hash_fct)(const void*, size_t),
                                void(*free_fct)(void*), size_t size);
-void ext2fs_hashmap_add(struct ext2fs_hashmap *h, void *data, const void *key,
-                       size_t key_len);
+int ext2fs_hashmap_add(struct ext2fs_hashmap *h,
+                      void *data, const void *key,size_t key_len);
 void *ext2fs_hashmap_lookup(struct ext2fs_hashmap *h, const void *key,
                            size_t key_len);
 void *ext2fs_hashmap_iter_in_order(struct ext2fs_hashmap *h,