lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Wed,  7 Dec 2022 17:11:20 -0800
From:   Jesse Brandeburg <jesse.brandeburg@...el.com>
To:     mkubecek@...e.cz
Cc:     netdev@...r.kernel.org,
        Jesse Brandeburg <jesse.brandeburg@...el.com>
Subject: [PATCH ethtool v2 11/13] ethtool: fix missing free of memory after failure

cppcheck warns:
test-common.c:106:2: error: Common realloc mistake: 'block' nulled but not freed upon failure [memleakOnRealloc]
 block = realloc(block, sizeof(*block) + size);
 ^

Fix the issue by storing a local copy of the old pointer and using that
to free the original if the realloc fails, as the manual for realloc()
suggests.

Signed-off-by: Jesse Brandeburg <jesse.brandeburg@...el.com>
---
 test-common.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/test-common.c b/test-common.c
index e4dac3298577..b472027140f6 100644
--- a/test-common.c
+++ b/test-common.c
@@ -97,15 +97,18 @@ void test_free(void *ptr)
 
 void *test_realloc(void *ptr, size_t size)
 {
-	struct list_head *block = NULL;
+	struct list_head *block = NULL, *oldblock;
 
 	if (ptr) {
 		block = (struct list_head *)ptr - 1;
 		list_del(block);
 	}
-	block = realloc(block, sizeof(*block) + size);
-	if (!block)
+	oldblock = block;
+	block = realloc(oldblock, sizeof(*oldblock) + size);
+	if (!block) {
+		free(oldblock);
 		return NULL;
+	}
 	list_add(block, &malloc_list);
 	return block + 1;
 }
-- 
2.31.1

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ