]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
crush: no need to call malloc is request is zero 36729/head
authorChangcheng Liu <changcheng.liu@aliyun.com>
Thu, 20 Aug 2020 02:10:35 +0000 (10:10 +0800)
committerChangcheng Liu <changcheng.liu@aliyun.com>
Thu, 20 Aug 2020 03:33:25 +0000 (11:33 +0800)
1. malloc(0) depends on libary's behavior:
https://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html
2. There's below frame path which call malloc(0)
   # build_simple_crush_map
   # |--> crush.add_bucket(0, 0, CRUSH_HASH_DEFAULT, root_type, 0, NULL, NULL, &rootid);
   #      |--> crush_bucket *b = crush_make_bucket(crush, alg, hash, type, size, items, weights);
   #           |--> crush_make_straw2_bucket(map, hash, type, size, items, weights);
   #                |--> bucket->h.items = malloc(sizeof(__s32)*size);
   #           |--> ceph_assert(b);
   If the library return NULL(malloc(0)), ceph_assert(b) will trigger abort

Signed-off-by: Changcheng Liu <changcheng.liu@aliyun.com>
src/crush/builder.c

index 27b1319ce5ebb4a703691b3a26553180bda8020b..25788e2907154cc47af4a302db5b0ed02dd95f11 100644 (file)
@@ -209,6 +209,10 @@ crush_make_uniform_bucket(int hash, int type, int size,
 
        bucket->h.weight = size * item_weight;
        bucket->item_weight = item_weight;
+
+       if (size == 0) {
+               return bucket;
+       }
        bucket->h.items = malloc(sizeof(__s32)*size);
 
         if (!bucket->h.items)
@@ -245,6 +249,10 @@ crush_make_list_bucket(int hash, int type, int size,
        bucket->h.type = type;
        bucket->h.size = size;
 
+       if (size == 0) {
+               return bucket;
+       }
+
        bucket->h.items = malloc(sizeof(__s32)*size);
         if (!bucket->h.items)
                 goto err;
@@ -606,6 +614,10 @@ crush_make_straw2_bucket(struct crush_map *map,
        bucket->h.type = type;
        bucket->h.size = size;
 
+       if (size == 0) {
+               return bucket;
+       }
+
         bucket->h.items = malloc(sizeof(__s32)*size);
         if (!bucket->h.items)
                 goto err;