public inbox for linux-ext4@vger.kernel.org
 help / color / mirror / Atom feed
From: Wu Guanghao <wuguanghao3@huawei.com>
To: linux-ext4@vger.kernel.org,
	"Благодаренко Артём" <artem.blagodarenko@gmail.com>
Cc: <liuzhiqiang26@huawei.com>, <linfeilong@huawei.com>
Subject: [PATCH V2 04/12] ss_add_info_dir: fix memory leak and check whether,NULL pointer
Date: Mon, 31 May 2021 09:28:23 +0800	[thread overview]
Message-ID: <03398446-66ea-01cb-e21a-b4841f325254@huawei.com> (raw)
In-Reply-To: <00ad4a90-8a40-24c1-98d9-eb5f0da42436@huawei.com>

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..429f410e 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;
@@ -155,6 +156,10 @@ void ss_add_info_dir(int sci_idx, char *info_dir, int *code_ptr)
     info->info_dirs = dirs;
     dirs[n_dirs + 1] = (char *)NULL;
     dirs[n_dirs] = malloc((unsigned)strlen(info_dir)+1);
+    if (dirs[n_dirs] == (char *)NULL) {
+        *code_ptr = errno;
+        return;
+    }
     strcpy(dirs[n_dirs], info_dir);
     *code_ptr = 0;
 }
-- 

  parent reply	other threads:[~2021-05-31  1:28 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-31  1:23 [PATCH V2 00/12] e2fsprogs: some bugfixs and some code cleanups Wu Guanghao
2021-05-31  1:26 ` [PATCH V2 03/12] zap_sector: fix memory leak Wu Guanghao
2021-05-31  1:28 ` Wu Guanghao [this message]
2021-05-31  1:30 ` [PATCH V2 06/12] append_pathname: check the value returned by realloc to avoid segfault Wu Guanghao
2021-05-31  1:31 ` [PATCH V2 07/12] argv_parse: check return value of malloc in argv_parse() Wu Guanghao
2021-05-31  1:32 ` [PATCH V2 10/12] hashmap: change return value type of, ext2fs_hashmap_add() Wu Guanghao
2021-05-31  4:28 ` [PATCH V2 00/12] e2fsprogs: some bugfixs and some code cleanups Theodore Ts'o
     [not found]   ` <e4df8d8a-a5b2-a3cf-7e0d-c5dceb3b39f9@huawei.com>
2021-05-31  8:28     ` Wu Guanghao
2021-06-15 11:27   ` Zhiqiang Liu

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=03398446-66ea-01cb-e21a-b4841f325254@huawei.com \
    --to=wuguanghao3@huawei.com \
    --cc=artem.blagodarenko@gmail.com \
    --cc=linfeilong@huawei.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=liuzhiqiang26@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