linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] tools/thermal: fix memory leak in realloc failure handling
@ 2023-09-24  6:50 Kuan-Wei Chiu
  2023-09-25  9:42 ` Daniel Lezcano
  0 siblings, 1 reply; 4+ messages in thread
From: Kuan-Wei Chiu @ 2023-09-24  6:50 UTC (permalink / raw)
  To: rafael
  Cc: daniel.lezcano, amitk, rui.zhang, linux-pm, linux-kernel,
	Kuan-Wei Chiu

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.

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;
 	}
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] tools/thermal: fix memory leak in realloc failure handling
  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
  2023-09-26 17:37   ` [PATCH v2] tools/thermal: remove unused 'mds' and 'nrhandler' variables Kuan-Wei Chiu
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel Lezcano @ 2023-09-25  9:42 UTC (permalink / raw)
  To: Kuan-Wei Chiu, rafael; +Cc: amitk, rui.zhang, linux-pm, linux-kernel


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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] tools/thermal: remove unused 'mds' and 'nrhandler' variables
  2023-09-25  9:42 ` Daniel Lezcano
@ 2023-09-26 17:37   ` Kuan-Wei Chiu
  2023-09-28 13:48     ` Daniel Lezcano
  0 siblings, 1 reply; 4+ messages in thread
From: Kuan-Wei Chiu @ 2023-09-26 17:37 UTC (permalink / raw)
  To: daniel.lezcano, rafael
  Cc: amitk, rui.zhang, linux-pm, linux-kernel, Kuan-Wei Chiu

In the previous code, the 'mds' and 'nrhandler' variables were not
utilized in the codebase. Additionally, there was a potential NULL
pointer dereference and memory leak due to improper handling of memory
reallocation failure.

This patch removes the unused 'mds' and 'nrhandler' variables along with
the associated code, addressing the unused variable issue, NULL pointer
dereference issue and the memory leak issue.

Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
Changes since v1:
 * Remove the unused 'mds' and 'nrhandler' variables along with the
   associated code.

 tools/thermal/lib/mainloop.c | 16 ----------------
 1 file changed, 16 deletions(-)

diff --git a/tools/thermal/lib/mainloop.c b/tools/thermal/lib/mainloop.c
index 94cbbcbd1c14..bf4c1b730d7b 100644
--- a/tools/thermal/lib/mainloop.c
+++ b/tools/thermal/lib/mainloop.c
@@ -9,7 +9,6 @@
 #include "log.h"
 
 static int epfd = -1;
-static unsigned short nrhandler;
 static sig_atomic_t exit_mainloop;
 
 struct mainloop_data {
@@ -18,8 +17,6 @@ struct mainloop_data {
 	int fd;
 };
 
-static struct mainloop_data **mds;
-
 #define MAX_EVENTS 10
 
 int mainloop(unsigned int timeout)
@@ -61,13 +58,6 @@ 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)
-			return -1;
-		nrhandler = fd + 1;
-	}
-
 	md = malloc(sizeof(*md));
 	if (!md)
 		return -1;
@@ -76,7 +66,6 @@ int mainloop_add(int fd, mainloop_callback_t cb, void *data)
 	md->cb = cb;
 	md->fd = fd;
 
-	mds[fd] = md;
 	ev.data.ptr = md;
 
 	if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) < 0) {
@@ -89,14 +78,9 @@ int mainloop_add(int fd, mainloop_callback_t cb, void *data)
 
 int mainloop_del(int fd)
 {
-	if (fd >= nrhandler)
-		return -1;
-
 	if (epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL) < 0)
 		return -1;
 
-	free(mds[fd]);
-
 	return 0;
 }
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] tools/thermal: remove unused 'mds' and 'nrhandler' variables
  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
  0 siblings, 0 replies; 4+ messages in thread
From: Daniel Lezcano @ 2023-09-28 13:48 UTC (permalink / raw)
  To: Kuan-Wei Chiu, rafael; +Cc: amitk, rui.zhang, linux-pm, linux-kernel

On 26/09/2023 19:37, Kuan-Wei Chiu wrote:
> In the previous code, the 'mds' and 'nrhandler' variables were not
> utilized in the codebase. Additionally, there was a potential NULL
> pointer dereference and memory leak due to improper handling of memory
> reallocation failure.
> 
> This patch removes the unused 'mds' and 'nrhandler' variables along with
> the associated code, addressing the unused variable issue, NULL pointer
> dereference issue and the memory leak issue.
> 
> Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
> ---

Applied, thanks


-- 
<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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-09-28 13:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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).