linux-ext4.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Theodore Y. Ts'o" <tytso@mit.edu>
To: wuguanghao <wuguanghao3@huawei.com>
Cc: linux-ext4@vger.kernel.org, artem.blagodarenko@gmail.com,
	liuzhiqiang26@huawei.com, linfeilong@huawei.com
Subject: Re: [PATCH v2 04/12] ss_add_info_dir: fix memory leak and check whether
Date: Thu, 15 Jul 2021 23:27:44 -0400	[thread overview]
Message-ID: <YPD8sGirl9D/3CyL@mit.edu> (raw)
In-Reply-To: <20210630082724.50838-5-wuguanghao3@huawei.com>

On Wed, Jun 30, 2021 at 04:27:16PM +0800, wuguanghao wrote:
> In ss_add_info_dir(), need free info->info_dirs before return,
> otherwise it will cause memory leak. At the same time, it is necessary
> to check whether dirs[n_dirs] is a null pointer, otherwise a segmentation
> fault will occur.
> 
> Signed-off-by: Wu Guanghao <wuguanghao3@huawei.com>
> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
> Reviewed-by: Wu Bo <wubo40@huawei.com>
> ---
>  lib/ss/help.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/lib/ss/help.c b/lib/ss/help.c
> index 5204401b..6768b9b1 100644
> --- a/lib/ss/help.c
> +++ b/lib/ss/help.c
> @@ -148,6 +148,7 @@ void ss_add_info_dir(int sci_idx, char *info_dir, int *code_ptr)
>      dirs = (char **)realloc((char *)dirs,
>  			    (unsigned)(n_dirs + 2)*sizeof(char *));
>      if (dirs == (char **)NULL) {
> +	free(info->info_dirs);
>  	info->info_dirs = (char **)NULL;
>  	*code_ptr = errno;
>  	return;

Adding the free() isn't right fix.  The real problem is that this line
should be removed:

  	info->info_dirs = (char **)NULL;

The function is trying to add a string (a directory name) to list, and
the realloc() is trying to extend the list.  If the realloc fils, we
shouldn't be zapping the original list.  We should just be returning,
and leaving the original list of directories unchanged and untouched.

    	    		      	 	     - Ted

  reply	other threads:[~2021-07-16  3:27 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-30  8:27 [PATCH v2 01/12] profile_create_node: set magic before strdup(name) to avoid memory leak wuguanghao
2021-06-30  8:27 ` [PATCH v2 02/12] tdb_transaction_recover: fix " wuguanghao
2021-07-16  3:19   ` Theodore Y. Ts'o
2021-06-30  8:27 ` [PATCH v2 03/12] zap_sector: " wuguanghao
2021-07-16  3:20   ` Theodore Y. Ts'o
2021-06-30  8:27 ` [PATCH v2 04/12] ss_add_info_dir: fix memory leak and check whether wuguanghao
2021-07-16  3:27   ` Theodore Y. Ts'o [this message]
2021-07-17  3:10     ` Zhiqiang Liu
2021-06-30  8:27 ` [PATCH v2 05/12] ss_create_invocation: fix memory leak and check whether NULL pointer wuguanghao
2021-07-16  3:34   ` Theodore Y. Ts'o
2021-07-19 11:08     ` Zhiqiang Liu
2021-06-30  8:27 ` [PATCH v2 06/12] append_pathname: check the value returned by realloc wuguanghao
2021-07-16  3:39   ` Theodore Y. Ts'o
2021-06-30  8:27 ` [PATCH v2 07/12] argv_parse: check return value of malloc in argv_parse() wuguanghao
2021-07-16  3:41   ` Theodore Y. Ts'o
2021-06-30  8:27 ` [PATCH v2 08/12] misc: fix potential segmentation fault problem in scandir() wuguanghao
2021-07-16  3:44   ` Theodore Y. Ts'o
2021-06-30  8:27 ` [PATCH v2 09/12] lib/ss/error.c: check return value malloc in ss_name() wuguanghao
2021-07-16  3:46   ` Theodore Y. Ts'o
2021-06-30  8:27 ` [PATCH v2 10/12] hashmap: change return value type of ext2fs_hashmap_add() wuguanghao
2021-07-16  3:55   ` Theodore Y. Ts'o
2021-06-30  8:27 ` [PATCH v2 11/12] misc/lsattr: check whether path is NULL in lsattr_dir_proc() wuguanghao
2021-07-16  3:58   ` Theodore Y. Ts'o
2021-07-17  1:39     ` Zhiqiang Liu
2021-06-30  8:27 ` [PATCH v2 12/12] ext2ed: fix potential NULL pointer dereference in dupstr() wuguanghao
2021-07-16  3:59   ` Theodore Y. Ts'o
2021-07-16  3:18 ` [PATCH v2 01/12] profile_create_node: set magic before strdup(name) to avoid memory leak Theodore Y. Ts'o

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=YPD8sGirl9D/3CyL@mit.edu \
    --to=tytso@mit.edu \
    --cc=artem.blagodarenko@gmail.com \
    --cc=linfeilong@huawei.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=liuzhiqiang26@huawei.com \
    --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).