From 936de8568dd4dd37fd04545de0156b92a052a88d Mon Sep 17 00:00:00 2001 From: Stefano Pidutti Date: Tue, 19 Aug 2025 12:08:49 +0200 Subject: [PATCH] Fix critical bug in tau_realloc causing memory leaks The current implementation of `tau_realloc` has a critical bug where it attempts to free a NULL pointer instead of the original memory when realloc fails, causing memory leaks. --- tau/tau.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tau/tau.h b/tau/tau.h index 7063209..959f272 100644 --- a/tau/tau.h +++ b/tau/tau.h @@ -319,10 +319,13 @@ static void tauClockPrintDuration(const double nanoseconds_duration) { static inline void* tau_realloc(void* const ptr, const tau_ull new_size) { void* const new_ptr = realloc(ptr, new_size); - - if(TAU_NONE(new_ptr)) - free(new_ptr); - + + if(TAU_NONE(new_ptr) && new_size > 0) { + fprintf(stderr, "FATAL: tau_realloc failed to allocate %" TAU_PRIu64 " bytes\n", + (tau_u64)new_size); + TAU_ABORT; + } + return new_ptr; } #endif // TAU_NO_TESTING