linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Daniel Lezcano <daniel.lezcano@linaro.org>
To: Kuan-Wei Chiu <visitorckw@gmail.com>, rafael@kernel.org
Cc: amitk@kernel.org, rui.zhang@intel.com, linux-pm@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] tools/thermal: fix memory leak in realloc failure handling
Date: Mon, 25 Sep 2023 11:42:03 +0200	[thread overview]
Message-ID: <93c87b5c-ae8f-d27d-0342-faa884a3142e@linaro.org> (raw)
In-Reply-To: <20230924065013.1081471-1-visitorckw@gmail.com>


Hi Kuan-Wei,

On 24/09/2023 08:50, Kuan-Wei Chiu wrote:
> 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.

Thanks for reporting the issue and proposing the fix.

The description is not accurate actually, neither the fix.

What is happening is we are losing the pointer information as the 'mds' 
variable is a global variable. So the assignation will overwrite the 
current pointer if it fails. That leads to a NULL pointer dereference in 
the mainloop_del.

Looking closer to the code, it seems 'mds' is not used as the stored 
information is not accessed.

For my understanding, we can just remove the:

static struct mainloop_data **mds

and

static unsigned short nrhandler;

along with the associated code


> Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
> ---
>   tools/thermal/lib/mainloop.c | 8 ++++++--
>   1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/thermal/lib/mainloop.c b/tools/thermal/lib/mainloop.c
> index 94cbbcbd1c14..6dcc4090d47e 100644
> --- a/tools/thermal/lib/mainloop.c
> +++ b/tools/thermal/lib/mainloop.c
> @@ -62,9 +62,13 @@ int mainloop_add(int fd, mainloop_callback_t cb, void *data)
>   	struct mainloop_data *md;
>   
>   	if (fd >= nrhandler) {
> -		mds = realloc(mds, sizeof(*mds) * (fd + 1));
> -		if (!mds)
> +		struct mainloop_data **mds_tmp =
> +			realloc(mds, sizeof(*mds) * (fd + 1));
> +		if (!mds_tmp) {
> +			free(mds);
>   			return -1;
> +		}
> +		mds = mds_tmp;
>   		nrhandler = fd + 1;
>   	}
>   

-- 
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog


  reply	other threads:[~2023-09-25  9:42 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-24  6:50 [PATCH] tools/thermal: fix memory leak in realloc failure handling Kuan-Wei Chiu
2023-09-25  9:42 ` Daniel Lezcano [this message]
2023-09-26 17:37   ` [PATCH v2] tools/thermal: remove unused 'mds' and 'nrhandler' variables Kuan-Wei Chiu
2023-09-28 13:48     ` Daniel Lezcano

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=93c87b5c-ae8f-d27d-0342-faa884a3142e@linaro.org \
    --to=daniel.lezcano@linaro.org \
    --cc=amitk@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=rafael@kernel.org \
    --cc=rui.zhang@intel.com \
    --cc=visitorckw@gmail.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;
as well as URLs for NNTP newsgroup(s).