From 496cfc4897ef0e7f879409e7ab68c011f526ef5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torbj=C3=B6rn=20SVENSSON?= Date: Thu, 26 Mar 2026 19:02:16 +0100 Subject: [PATCH] gcov: return failure if malloc fails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Don't assume that malloc always returns a non-null pointer. xmalloc is sometimes an alias for malloc due to this in libgcc/libgcov.h: /* work around the poisoned malloc/calloc in system.h. */ #ifndef xmalloc #define xmalloc malloc #endif libgcc/ChangeLog: * libgcov-driver-system.c (gcov_exit_open_gcda_file): Handle potential NULL value from malloc. Signed-off-by: Torbjörn SVENSSON --- libgcc/libgcov-driver-system.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libgcc/libgcov-driver-system.c b/libgcc/libgcov-driver-system.c index 7b20b29bebf..0991bb57db6 100644 --- a/libgcc/libgcov-driver-system.c +++ b/libgcc/libgcov-driver-system.c @@ -301,6 +301,11 @@ gcov_exit_open_gcda_file (struct gcov_info *gi_ptr, size_t prefix_length = gf->prefix ? strlen (gf->prefix) : 0; gf->filename = (char *) xmalloc (prefix_length + strlen (fname) + 2); + if (!gf->filename) + { + fprintf (stderr, "profiling:Failed to allocate memory for filename"); + return -1; + } *gf->filename = '\0'; if (prefix_length) strcat (gf->filename, gf->prefix);