linux-hyperv.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
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,
	Kuan-Wei Chiu <visitorckw@gmail.com>
Subject: [PATCH] tools: hv: kvp: fix memory leak in realloc failure handling
Date: Sun, 24 Sep 2023 13:51:48 +0800	[thread overview]
Message-ID: <20230924055148.1074754-1-visitorckw@gmail.com> (raw)

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


             reply	other threads:[~2023-09-24  5:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-24  5:51 Kuan-Wei Chiu [this message]
2023-09-25  0:00 ` [PATCH] tools: hv: kvp: fix memory leak in realloc failure handling Kuan-Wei Chiu

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=20230924055148.1074754-1-visitorckw@gmail.com \
    --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).