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 05/12] ss_create_invocation: fix memory leak and check whether NULL pointer
Date: Thu, 15 Jul 2021 23:34:57 -0400 [thread overview]
Message-ID: <YPD+YbH7STaKTgxC@mit.edu> (raw)
In-Reply-To: <20210630082724.50838-6-wuguanghao3@huawei.com>
On Wed, Jun 30, 2021 at 04:27:17PM +0800, wuguanghao wrote:
> In ss_create_invocation(), it is necessary to check whether
> returned by malloc is a null pointer.
>
> Signed-off-by: Wu Guanghao <wuguanghao3@huawei.com>
> Signed-off-by: Zhiqiang Liu <liuzhiqiang26@huawei.com>
> ---
> lib/ss/invocation.c | 38 ++++++++++++++++++++++++++++++++------
> 1 file changed, 32 insertions(+), 6 deletions(-)
>
Instead of adding all of these goto targets (which is fragile if for
some reason the code gets rearranged), it would be better to make sure
everything that we might want to free is initialized, i.e.:
register ss_data *new_table = NULL;
register ss_data **table = NULL;
new_table = (ss_data *) malloc(sizeof(ss_data));
if (!new_table)
goto out;
memset(new_table, 0, sizeof(ss_data));
and then exit path can just look like this:
out:
if (new_table) {
free(new_table->prompt);
free(new_table->info_dirs);
}
free(new_table);
free(table);
*code_ptr = ENOMEM;
return 0;
... which is much cleaner, and means in the future, you don't need to
figure out which goto label you might need to jump to.
Cheers,
- Ted
P.S. And if we are making all of these changes to the function's
initializers, it might be a good time to zap the "register" keywords
for any lines we are changing, or are nearby, while we're at it.
next prev parent reply other threads:[~2021-07-16 3:35 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
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 [this message]
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=YPD+YbH7STaKTgxC@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).