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

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