Igt-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH i-g-t] tools/intel_gpu_top: prevent losing original pointer on realloc failure
@ 2025-12-15 14:45 Sebastian Brzezinka
  2025-12-15 22:04 ` ✗ i915.CI.BAT: failure for " Patchwork
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: Sebastian Brzezinka @ 2025-12-15 14:45 UTC (permalink / raw)
  To: igt-dev; +Cc: kamil.konieczny, Sebastian Brzezinka

Previously, realloc was called directly on engines after incrementing
num_engines. If realloc failed, it would return NULL and overwrite the
original pointer, causing a memory leak and leaving engines inaccessible.
This patch uses a temporary pointer for realloc and only updates engines
after a successful allocation, ensuring the original pointer is preserved
on failure and preventing data loss.

Signed-off-by: Sebastian Brzezinka <sebastian.brzezinka@intel.com>
---
 tools/intel_gpu_top.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/tools/intel_gpu_top.c b/tools/intel_gpu_top.c
index 1bd88ba7c..1d0d11585 100644
--- a/tools/intel_gpu_top.c
+++ b/tools/intel_gpu_top.c
@@ -378,6 +378,7 @@ static struct engines *discover_engines(char *device)
 	while ((dent = readdir(d)) != NULL) {
 		const char *endswith = "-busy";
 		const unsigned int endlen = strlen(endswith);
+		struct engines *engines_tmp;
 		struct engine *engine =
 				engine_ptr(engines, engines->num_engines);
 		char buf[256];
@@ -444,14 +445,15 @@ static struct engines *discover_engines(char *device)
 			break;
 		}
 
-		engines->num_engines++;
-		engines = realloc(engines, sizeof(struct engines) +
-				  engines->num_engines * sizeof(struct engine));
-		if (!engines) {
+		engines_tmp = realloc(engines, sizeof(struct engines) +
+				  (engines->num_engines + 1) * sizeof(struct engine));
+		if (!engines_tmp) {
 			ret = errno;
 			break;
 		}
 
+		engines = engines_tmp;
+		engines->num_engines++;
 		ret = 0;
 	}
 
-- 
2.51.2


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

end of thread, other threads:[~2025-12-17 10:07 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-15 14:45 [PATCH i-g-t] tools/intel_gpu_top: prevent losing original pointer on realloc failure Sebastian Brzezinka
2025-12-15 22:04 ` ✗ i915.CI.BAT: failure for " Patchwork
2025-12-17  6:33   ` Sebastian Brzezinka
2025-12-15 22:28 ` ✗ Xe.CI.BAT: " Patchwork
2025-12-16  7:18 ` [PATCH i-g-t] " Krzysztof Karas
2025-12-16 17:29   ` Kamil Konieczny
2025-12-16  8:16 ` ✗ Xe.CI.Full: failure for " Patchwork
2025-12-16 19:02 ` [PATCH i-g-t] " Tvrtko Ursulin
2025-12-17  7:05   ` Sebastian Brzezinka
2025-12-17 10:07     ` Tvrtko Ursulin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox