Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1091318pxj; Sat, 29 May 2021 02:57:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwfXDMNNL+QEs05VNyRjVYBH0nMZYDWGHLH0BQWY0jWDfS3YUIf+ijxuJtuxzARQWFDQRuY X-Received: by 2002:a05:6e02:605:: with SMTP id t5mr10372783ils.1.1622282232734; Sat, 29 May 2021 02:57:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622282232; cv=none; d=google.com; s=arc-20160816; b=LbY8boMDkKSm193q8ljeAZ9cL5RlI97zG6Z43lZ36zXa+1A7Zdc7nWsmdxs+B80q8E QkGjZqnaTqXgida4czJcG8nd5S0mmTx83iVYvOSHuTVc2iYW9pWasgHikjaYTsLhZKQ8 j8OP4uooGSdO+ps54WrBpYb8YXDfSUvn5JU7moN7r8db96bNkc26W9qAQvqo1GMjaWfK SxK3gwQnhbVc7lWY9Hnww5CVU55C7oye3njmSv85lKutt9wqiwevgOWq56cT6E37xV/w 14Mi50fvaBbwpM8+rI3ZyQB+cHfhpJEzlFJjkyA67lrTQTz+6eMIqInrZQ3omG4alC9x CO/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=ADlF2Gq3g2rLPmBRmsNDq1LlbiUcSC4b87sXaKHdFT4=; b=vz9dlY+aGErcg+imPXgsOm33fCD8df7MAmcMiolFL2m1BzzPsTAVodjzSuWEhV/nKT tIRiCrAiwTsmr68Rlimg8SKYcZpt4YWSeruAdOX3az3K1EspcfT/YhTZFZaRGdCLsDex d4hurFiEjG7FX3+hd7ujzyd59nPFkBdODJ2bANYDPdbS/wcrZQ2RD6Ey8UlhMTcvA402 7lrJRjw1C5sK3A2X2pwCq9WEt1RZkBxKpbET6XDOwOwFVCFrWa657Llw+npj/3PIWqjN Ltv3Eef+vYmjxdDC4JhXWg/qbYtOaQdGXv9Bi8ZvG/ZhqeaGwoZXf8nTQTtTPtTSXFH3 GDIQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s8si9154954ilt.116.2021.05.29.02.56.50; Sat, 29 May 2021 02:57:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229597AbhE2J5H (ORCPT + 99 others); Sat, 29 May 2021 05:57:07 -0400 Received: from szxga01-in.huawei.com ([45.249.212.187]:2096 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229559AbhE2J5G (ORCPT ); Sat, 29 May 2021 05:57:06 -0400 Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4FscHt59FJzWphg for ; Sat, 29 May 2021 17:50:50 +0800 (CST) Received: from dggema765-chm.china.huawei.com (10.1.198.207) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256) id 15.1.2176.2; Sat, 29 May 2021 17:55:28 +0800 Received: from [127.0.0.1] (10.174.177.249) by dggema765-chm.china.huawei.com (10.1.198.207) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2176.2; Sat, 29 May 2021 17:55:27 +0800 Subject: Re: [PATCH 10/12] hashmap: change return value type of, ext2fs_hashmap_add() To: =?UTF-8?B?0JHQu9Cw0LPQvtC00LDRgNC10L3QutC+INCQ0YDRgtGR0Lw=?= , Wu Guanghao CC: , References: <266bc52e-e279-ce84-0e1f-1405b9bc6174@huawei.com> <13880683-11b1-395a-05d0-f9076cca1672@huawei.com> From: Zhiqiang Liu Message-ID: <42b64a9f-05ff-2c8d-e991-e4e91a26b57c@huawei.com> Date: Sat, 29 May 2021 17:55:27 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Content-Language: en-US X-Originating-IP: [10.174.177.249] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggema765-chm.china.huawei.com (10.1.198.207) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org On 2021/5/25 20:45, Благодаренко Артём wrote: > >> On 24 May 2021, at 14:25, Wu Guanghao wrote: >> >> 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 >> Signed-off-by: Wu Guanghao >> --- >> contrib/android/base_fs.c | 12 +++++++++--- >> lib/ext2fs/fileio.c | 11 +++++++++-- >> lib/ext2fs/hashmap.c | 12 ++++++++++-- >> lib/ext2fs/hashmap.h | 4 ++-- >> 4 files changed, 30 insertions(+), 9 deletions(-) >> >> diff --git a/contrib/android/base_fs.c b/contrib/android/base_fs.c >> index 652317e2..d3e00d18 100644 >> --- a/contrib/android/base_fs.c >> +++ b/contrib/android/base_fs.c >> @@ -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); >> diff --git a/lib/ext2fs/fileio.c b/lib/ext2fs/fileio.c >> index a0b5d971..b86951d9 100644 >> --- a/lib/ext2fs/fileio.c >> +++ b/lib/ext2fs/fileio.c >> @@ -475,8 +475,15 @@ 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); >> + new_block = NULL; > There is no need to set new_block to NULL here.. new_block is a local variable and the function returns after "fail" label. > Same for blocks above which also jump to the “fail” label. Thanks for your suggestion. we will send the v2 patches. >> + goto fail; >> + } >> } >> >> if (bmap_flags & BMAP_SET) { >> diff --git a/lib/ext2fs/hashmap.c b/lib/ext2fs/hashmap.c >> index ffe61ce9..7278edaf 100644 >> --- a/lib/ext2fs/hashmap.c >> +++ b/lib/ext2fs/hashmap.c >> @@ -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, >> diff --git a/lib/ext2fs/hashmap.h b/lib/ext2fs/hashmap.h >> index dcfa7455..0c09d2bd 100644 >> --- a/lib/ext2fs/hashmap.h >> +++ b/lib/ext2fs/hashmap.h >> @@ -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, >> -- > Best regards, > Artem Blagodarenko > > > .