From ce471d70682b035232714e3c9df3899adcfba957 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Sun, 1 Apr 2018 18:30:10 -0600 Subject: [PATCH] dtcalc/ds_xlib.c: coverity CID 175093; wrong sizeof/bad free --- cde/programs/dtcalc/ds_xlib.c | 35 +++++++++++++++++++++++++---------- cde/programs/dtcalc/motif.h | 2 +- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/cde/programs/dtcalc/ds_xlib.c b/cde/programs/dtcalc/ds_xlib.c index a9489804..1136c91b 100644 --- a/cde/programs/dtcalc/ds_xlib.c +++ b/cde/programs/dtcalc/ds_xlib.c @@ -35,6 +35,8 @@ #include #include #include +#include +#include #include #include #include @@ -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); } diff --git a/cde/programs/dtcalc/motif.h b/cde/programs/dtcalc/motif.h index f51e45b5..c242bdf9 100644 --- a/cde/programs/dtcalc/motif.h +++ b/cde/programs/dtcalc/motif.h @@ -177,4 +177,4 @@ XVars X ; #define HOME_RESTORE 1 #define CURRENT_RESTORE 2 -#define MAX_PATH 1024 +#define MAX_PATH PATH_MAX -- 2.25.1