linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Zhiqiang Liu <liuzhiqiang26@huawei.com>
To: "Благодаренко Артём" <artem.blagodarenko@gmail.com>,
	"Wu Guanghao" <wuguanghao3@huawei.com>
Cc: <linux-ext4@vger.kernel.org>, <linfeilong@huawei.com>
Subject: Re: [PATCH 10/12] hashmap: change return value type of, ext2fs_hashmap_add()
Date: Sat, 29 May 2021 17:55:27 +0800	[thread overview]
Message-ID: <42b64a9f-05ff-2c8d-e991-e4e91a26b57c@huawei.com> (raw)
In-Reply-To: <ECE8BAEA-EDA7-472D-9163-ABD0709F91F7@gmail.com>


On 2021/5/25 20:45, Благодаренко Артём wrote:
>
>> On 24 May 2021, at 14:25, Wu Guanghao <wuguanghao3@huawei.com> 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 <liuzhiqiang26@huawei.com>
>> Signed-off-by: Wu Guanghao <wuguanghao3@huawei.com>
>> ---
>> 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
>
>
> .


  reply	other threads:[~2021-05-29  9:55 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-24 11:18 [PATCH 00/12] e2fsprogs: some bugfixs and some code cleanups Wu Guanghao
2021-05-24 11:19 ` [PATCH 01/12] profile_create_node: set magic before strdup(name) to avoid memory leak Wu Guanghao
2021-05-24 11:20 ` [PATCH 02/12] tdb_transaction_recover: fix " Wu Guanghao
2021-05-24 11:20 ` [PATCH 03/12] zap_sector: " Wu Guanghao
2021-05-24 14:40   ` Благодаренко Артём
2021-05-25  2:16     ` Wu Guanghao
2021-05-24 11:21 ` [PATCH 04/12] ss_add_info_dir: fix memory leak and check whether,NULL pointer Wu Guanghao
2021-05-24 11:21 ` [PATCH 05/12] ss_create_invocation: fix memory leak and check whether NULL pointer Wu Guanghao
2021-05-24 11:23 ` [PATCH 06/12] append_pathname: check the value returned by realloc to avoid segfault Wu Guanghao
2021-05-25  9:37   ` Благодаренко Артём
2021-05-25 11:26     ` Wu Guanghao
2021-05-24 11:23 ` [PATCH 07/12] argv_parse: check return value of malloc in argv_parse() Wu Guanghao
2021-05-25  2:32   ` Wu Guanghao
2021-05-24 11:24 ` [PATCH 08/12] misc: fix potential segmentation fault problem in, scandir() Wu Guanghao
2021-05-24 11:24 ` [PATCH 09/12] lib/ss/error.c: check return value malloc in ss_name() Wu Guanghao
2021-05-24 11:25 ` [PATCH 10/12] hashmap: change return value type of, ext2fs_hashmap_add() Wu Guanghao
2021-05-25 12:45   ` Благодаренко Артём
2021-05-29  9:55     ` Zhiqiang Liu [this message]
2021-05-24 11:25 ` [PATCH 11/12] misc/lsattr: check whether path is NULL in, lsattr_dir_proc() Wu Guanghao
2021-05-24 11:26 ` [PATCH 12/12] ext2ed: fix potential NULL pointer dereference in, dupstr() Wu Guanghao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=42b64a9f-05ff-2c8d-e991-e4e91a26b57c@huawei.com \
    --to=liuzhiqiang26@huawei.com \
    --cc=artem.blagodarenko@gmail.com \
    --cc=linfeilong@huawei.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=wuguanghao3@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).