diff liboctave/wrappers/uniconv-wrappers.c @ 26475:9baba1815f1c stable

uniconv-wrappers.c: Fix static analyzer detected issues (bug #55347). * uniconv-wrappers.c (u8_from_wchar, u8_to_wchar): Check for valid pointer from malloc before proceeding.
author Rik <rik@octave.org>
date Sun, 06 Jan 2019 21:32:21 -0800
parents 00f796120a6d
children b442ec6dda5c
line wrap: on
line diff
--- a/liboctave/wrappers/uniconv-wrappers.c	Sun Jan 06 17:58:39 2019 -0800
+++ b/liboctave/wrappers/uniconv-wrappers.c	Sun Jan 06 21:32:21 2019 -0800
@@ -76,9 +76,14 @@
 
   // result might not be 0 terminated
   char *retval = malloc (length + 1);
-  memcpy (retval, mbchar, length);
-  free ((void *) mbchar);
-  retval[length] = 0; // 0 terminate string
+  if (retval)
+    {
+      memcpy (retval, mbchar, length);
+      free ((void *) mbchar);
+      retval[length] = 0; // 0 terminate string
+    }
+  else
+    free ((void *) mbchar);
 
   return retval;
 }
@@ -98,9 +103,15 @@
                                      src, srclen, NULL, NULL, &length);
   // result might not be 0 terminated
   wchar_t *retval = malloc (length + 1 * sizeof (wchar_t));
-  memcpy (retval, wchar, length);
-  free ((void *) wchar);
-  retval[length / sizeof (wchar_t)] = 0; // 0 terminate string
+  if (retval)
+    {
+      memcpy (retval, wchar, length);
+      free ((void *) wchar);
+      retval[length / sizeof (wchar_t)] = 0; // 0 terminate string
+    }
+
+  else
+    free ((void *) wchar);
 
   return retval;
 }