[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20221208105247.vpb5aqn6hp3unjqx@lion.mk-sys.cz>
Date: Thu, 8 Dec 2022 11:52:47 +0100
From: Michal Kubecek <mkubecek@...e.cz>
To: Jesse Brandeburg <jesse.brandeburg@...el.com>
Cc: netdev@...r.kernel.org
Subject: Re: [PATCH ethtool v2 11/13] ethtool: fix missing free of memory
after failure
On Wed, Dec 07, 2022 at 05:11:20PM -0800, Jesse Brandeburg wrote:
> 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>
Acked-by: Michal Kubecek <mkubecek@...e.cz>
> ---
> 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
>
Download attachment "signature.asc" of type "application/pgp-signature" (489 bytes)
Powered by blists - more mailing lists