From: Kuan-Wei Chiu <visitorckw@gmail.com>
To: kys@microsoft.com
Cc: haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com,
linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] tools: hv: kvp: fix memory leak in realloc failure handling
Date: Mon, 25 Sep 2023 08:00:26 +0800 [thread overview]
Message-ID: <20230925000026.GA1101960@sivslab-System-Product-Name> (raw)
In-Reply-To: <20230924055148.1074754-1-visitorckw@gmail.com>
On Sun, Sep 24, 2023 at 01:51:48PM +0800, Kuan-Wei Chiu wrote:
> In the previous code, there was a memory leak issue where the
> previously allocated memory was not freed upon a failed realloc
> operation. This patch addresses the problem by releasing the old memory
> before setting the pointer to NULL in case of a realloc failure. This
> ensures that memory is properly managed and avoids potential memory
> leaks.
>
> Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
> ---
> tools/hv/hv_kvp_daemon.c | 16 +++++++++++-----
> 1 file changed, 11 insertions(+), 5 deletions(-)
>
> diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c
> index 27f5e7dfc2f7..af180278d56d 100644
> --- a/tools/hv/hv_kvp_daemon.c
> +++ b/tools/hv/hv_kvp_daemon.c
> @@ -209,11 +209,13 @@ static void kvp_update_mem_state(int pool)
> * We have more data to read.
> */
> num_blocks++;
> - record = realloc(record, alloc_unit * num_blocks);
> + struct kvp_record *record_tmp =
> + realloc(record, alloc_unit * num_blocks);
>
> - if (record == NULL) {
> + if (record_tmp == NULL) {
> syslog(LOG_ERR, "malloc failed");
> kvp_release_lock(pool);
> + free(record);
> exit(EXIT_FAILURE);
> }
> continue;
> @@ -345,11 +347,15 @@ static int kvp_key_add_or_modify(int pool, const __u8 *key, int key_size,
> */
> if (num_records == (ENTRIES_PER_BLOCK * num_blocks)) {
> /* Need to allocate a larger array for reg entries. */
> - record = realloc(record, sizeof(struct kvp_record) *
> - ENTRIES_PER_BLOCK * (num_blocks + 1));
> + struct kvp_record *record_tmp = realloc(
> + record, sizeof(struct kvp_record) * ENTRIES_PER_BLOCK *
> + (num_blocks + 1));
>
> - if (record == NULL)
> + if (record_tmp == NULL) {
> + free(record);
> return 1;
> + }
> + record = record_tmp;
> kvp_file_info[pool].num_blocks++;
>
> }
> --
> 2.25.1
>
After tracing the code more thoroughly, I have come to the realization
that the original codebase already handles memory management correctly.
It verifies the success of the realloc operation before updating the
pointer, which means there is no memory leak issue, and there is no
need to release memory explicitly.
Consequently, my proposed changes are unnecessary and could potentially
introduce problems if implemented.
Best regards,
Kuan-Wei Chiu
prev parent reply other threads:[~2023-09-25 0:00 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-24 5:51 [PATCH] tools: hv: kvp: fix memory leak in realloc failure handling Kuan-Wei Chiu
2023-09-25 0:00 ` Kuan-Wei Chiu [this message]
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=20230925000026.GA1101960@sivslab-System-Product-Name \
--to=visitorckw@gmail.com \
--cc=decui@microsoft.com \
--cc=haiyangz@microsoft.com \
--cc=kys@microsoft.com \
--cc=linux-hyperv@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=wei.liu@kernel.org \
/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).