Compiling a testdir with CC="gcc -fanalyzer" produced this warning:

../../gllib/immutable.c:333:3: warning: use of NULL 'wp' where non-null 
expected [CWE-476] [-Wanalyzer-null-argument]


2021-04-30  Bruno Haible  <[email protected]>

        immutable: Fix crash when immmalloc() fails.
        Found by GCC 11 '-fanalyzer'.
        * lib/immutable.c (immstrdup): Don't crash when immmalloc() fails.

diff --git a/lib/immutable.c b/lib/immutable.c
index 35f7397..e4f3825 100644
--- a/lib/immutable.c
+++ b/lib/immutable.c
@@ -330,6 +330,8 @@ immstrdup (const char *string)
 {
   size_t size = strlen (string) + 1;
   void *wp = immmalloc (size);
+  if (wp == NULL)
+    return NULL;
   memcpy (wp, string, size);
   return (const char *) immfreeze (wp);
 }


Reply via email to