From: Changcheng Liu Date: Thu, 20 Aug 2020 02:10:35 +0000 (+0800) Subject: crush: no need to call malloc is request is zero X-Git-Tag: v16.1.0~1291^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=4071dde70ce26c6058b72b3556ba5b8404579bb6;p=ceph.git crush: no need to call malloc is request is zero 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 --- diff --git a/src/crush/builder.c b/src/crush/builder.c index 27b1319ce5e..25788e29071 100644 --- a/src/crush/builder.c +++ b/src/crush/builder.c @@ -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;