dtcalc/ds_xlib.c: coverity CID 175093; wrong sizeof/bad free
authorJon Trulson <jon@radscan.com>
Mon, 2 Apr 2018 00:30:10 +0000 (18:30 -0600)
committerJon Trulson <jon@radscan.com>
Mon, 2 Apr 2018 00:30:10 +0000 (18:30 -0600)
cde/programs/dtcalc/ds_xlib.c
cde/programs/dtcalc/motif.h

index a9489804487f9b9dbd09a2e59c4ebfe2d9119efa..1136c91bb2810fccf573910fc4fe9243bd8c57f7 100644 (file)
@@ -35,6 +35,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <stdint.h>
+#include <stdbool.h>
 #include <ctype.h>
 #include <sys/param.h>
 #include <sys/stat.h>
@@ -254,15 +256,24 @@ int
 ds_save_resources(XrmDatabase rDB, char *filename)
 {
   char *home;
-  struct stat statbuf ;
+  struct stat statbuf;
+  bool needsFree = false;
 
-  if(filename == NULL)
+  if (filename == NULL)
   {
     if ((filename = getenv("DTCALCDEF")) == NULL)
       {
-        home = getenv("HOME") ;
-        filename = (char*) calloc(1, strlen(home) + 18) ;
-        snprintf(filename, sizeof(filename), "%s/.dtcalcdef", home) ;
+          size_t fileLen = strlen(home) + 18;
+          home = getenv("HOME");
+          if ( (filename = calloc(1, fileLen)) != NULL )
+          {
+              needsFree = true;
+              snprintf(filename, fileLen, "%s/.dtcalcdef", home);
+          }
+          else
+          {
+              return 1;
+          }
       }
   }
 
@@ -270,14 +281,18 @@ ds_save_resources(XrmDatabase rDB, char *filename)
 
   if (stat(filename, &statbuf) != -1 && access(filename, W_OK) != 0)
     { 
-      free(filename) ;
-      return(1) ;
+        if (needsFree)
+            free(filename);
+
+        return(1);
     }
 
 /* If file does not exist this call will create it. */
 
-  XrmPutFileDatabase(rDB, filename) ;
-  free(filename) ;
-  return(0) ;
+  XrmPutFileDatabase(rDB, filename);
+  if (needsFree)
+      free(filename);
+
+  return(0);
 }
 
index f51e45b5316f8a86542921e1b5fa1f549202b6d2..c242bdf9a78c57129fff2bb7e521b76ae46fcd75 100644 (file)
@@ -177,4 +177,4 @@ XVars X ;
 #define HOME_RESTORE    1
 #define CURRENT_RESTORE    2
 
-#define MAX_PATH 1024
+#define MAX_PATH PATH_MAX