mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Martin Kepplinger <martink@posteo.de>
To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com
Cc: devel@linuxdriverproject.org, linux-kernel@vger.kernel.org,
	Martin Kepplinger <martink@posteo.de>
Subject: [PATCH] tools: hv: hv_kvp_daemon: fix usage of realloc()
Date: Wed, 13 Sep 2017 20:41:02 +0200	[thread overview]
Message-ID: <20170913184102.25330-1-martink@posteo.de> (raw)

realloc() returns NULL in case it fails. Since we don't save the
pointer in question elsewhere, we leak memory by assigning NULL
to the original memory in the heap.

realloc() doesn't free memory in case of failure, so let's do
it manually.

Signed-off-by: Martin Kepplinger <martink@posteo.de>
---
 tools/hv/hv_kvp_daemon.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c
index eaa3bec273c8..0b3b18d0a6e3 100644
--- a/tools/hv/hv_kvp_daemon.c
+++ b/tools/hv/hv_kvp_daemon.c
@@ -231,6 +231,7 @@ static int kvp_file_init(void)
 	size_t records_read;
 	char *fname;
 	struct kvp_record *record;
+	struct kvp_record *record_tmp;
 	struct kvp_record *readp;
 	int num_blocks;
 	int i;
@@ -284,12 +285,15 @@ static int kvp_file_init(void)
 				 * We have more data to read.
 				 */
 				num_blocks++;
-				record = realloc(record, alloc_unit *
+				record_tmp = realloc(record, alloc_unit *
 						num_blocks);
-				if (record == NULL) {
+				if (!record_tmp) {
+					free(record);
 					fclose(filep);
 					close(fd);
 					return 1;
+				} else {
+					record = record_tmp;
 				}
 				continue;
 			}
@@ -355,6 +359,7 @@ static int kvp_key_add_or_modify(int pool, const __u8 *key, int key_size,
 	int i;
 	int num_records;
 	struct kvp_record *record;
+	struct kvp_record *record_tmp;
 	int num_blocks;
 
 	if ((key_size > HV_KVP_EXCHANGE_MAX_KEY_SIZE) ||
@@ -387,11 +392,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) *
+		record_tmp = realloc(record, sizeof(struct kvp_record) *
 			 ENTRIES_PER_BLOCK * (num_blocks + 1));
 
-		if (record == NULL)
+		if (!record_tmp) {
+			free(record);
 			return 1;
+		} else {
+			record = record_tmp;
+		}
 		kvp_file_info[pool].num_blocks++;
 
 	}
-- 
2.11.0

                 reply	other threads:[~2017-09-13 18:41 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20170913184102.25330-1-martink@posteo.de \
    --to=martink@posteo.de \
    --cc=devel@linuxdriverproject.org \
    --cc=haiyangz@microsoft.com \
    --cc=kys@microsoft.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sthemmin@microsoft.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

all inboxes | Powered by JetHome®