From: Malaya Kumar Rout <malayarout91@gmail.com>
To: malayarout91@gmail.com
Cc: linux-kernel@vger.kernel.org,
"K. Y. Srinivasan" <kys@microsoft.com>,
Haiyang Zhang <haiyangz@microsoft.com>,
Wei Liu <wei.liu@kernel.org>, Dexuan Cui <decui@microsoft.com>,
linux-hyperv@vger.kernel.org
Subject: [PATCH] tools/hv: Memory Leak on realloc
Date: Sat, 22 Mar 2025 22:16:47 +0530 [thread overview]
Message-ID: <20250322164709.500340-1-malayarout91@gmail.com> (raw)
Static analysis for hv_kvp_daemon.c with cppcheck : error:
hv_kvp_daemon.c:359:3: error: Common realloc mistake:
'record' nulled but not freed upon failure [memleakOnRealloc]
record = realloc(record, sizeof(struct kvp_record) *
If realloc() fails, record is now NULL.
If we directly assign this NULL to record, the reference to the previously allocated memory is lost.
This causes a memory leak because the old allocated memory remains but is no longer accessible.
A temporary pointer was utilized when invoking realloc() to prevent
the loss of the original allocation in the event of a failure
CC: linux-kernel@vger.kernel.org
linux-hyperv@vger.kernel.org
Signed-off-by: Malaya Kumar Rout <malayarout91@gmail.com>
---
tools/hv/hv_kvp_daemon.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c
index 04ba035d67e9..6807832209f0 100644
--- a/tools/hv/hv_kvp_daemon.c
+++ b/tools/hv/hv_kvp_daemon.c
@@ -356,11 +356,14 @@ 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));
-
- if (record == NULL)
+ struct kvp_record *temp = realloc(record, sizeof(struct kvp_record) *
+ ENTRIES_PER_BLOCK * (num_blocks + 1));
+ if (!temp) {
+ free(record);
+ record = NULL;
return 1;
+ }
+ record = temp;
kvp_file_info[pool].num_blocks++;
}
--
2.43.0
next reply other threads:[~2025-03-22 16:47 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-22 16:46 Malaya Kumar Rout [this message]
2025-04-05 6:09 ` malaya kumar rout
2025-04-07 5:37 ` Wei Liu
2025-04-07 7:14 ` malaya kumar rout
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=20250322164709.500340-1-malayarout91@gmail.com \
--to=malayarout91@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
all inboxes | Powered by JetHome®