From adc9ee09cef56939c5baad9435d15c225c29abae Mon Sep 17 00:00:00 2001 From: rlar Date: Thu, 13 Sep 2012 20:07:33 +0200 Subject: [PATCH] api change for ngdirname() to fix a memory leak ngdirname did `own' the returned string. now the invoker is responsible for the returned string. note, this is contrary to the POSIX dirname() implementation, which *might* return pointers to statical allocated memory. --- src/frontend/device.c | 6 +++++- src/frontend/inp.c | 6 +++--- src/frontend/inpcom.c | 12 ++++++------ src/misc/ivars.c | 1 + src/misc/util.c | 14 ++------------ 5 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/frontend/device.c b/src/frontend/device.c index 5342122d7..4b1f3343d 100644 --- a/src/frontend/device.c +++ b/src/frontend/device.c @@ -1455,7 +1455,11 @@ static void com_alter_mod(wordlist *wl) filename = copy(eqword); } modfile = inp_pathopen(filename, readmode); - inp_readall(modfile, &modeldeck, 0, ngdirname(filename), 0); + { + char *dir_name = ngdirname(filename); + inp_readall(modfile, &modeldeck, 0, dir_name, 0); + free(dir_name); + } tfree(input); tfree(filename); /* get all lines starting with *model */ diff --git a/src/frontend/inp.c b/src/frontend/inp.c index 09a6e2a69..5f97477be 100644 --- a/src/frontend/inp.c +++ b/src/frontend/inp.c @@ -293,12 +293,12 @@ inp_spsource(FILE *fp, bool comfile, char *filename) double startTime, endTime; /* read in the deck from a file */ - char *filename_dup = (filename == NULL) ? strdup(".") : strdup(filename); + char *dir_name = ngdirname(filename ? filename : "."); startTime = seconds(); - inp_readall(fp, &deck, 0, ngdirname(filename_dup), comfile); + inp_readall(fp, &deck, 0, dir_name, comfile); endTime = seconds(); - tfree(filename_dup); + tfree(dir_name); /* if nothing came back from inp_readall, just close fp and return to caller */ if (!deck) { /* MW. We must close fp always when returning */ diff --git a/src/frontend/inpcom.c b/src/frontend/inpcom.c index 95a526092..cd07b6487 100644 --- a/src/frontend/inpcom.c +++ b/src/frontend/inpcom.c @@ -310,9 +310,9 @@ inp_readall(FILE *fp, struct line **data, int call_depth, char *dir_name, bool c library_file[num_libraries++] = strdup(y); if ( dir_name_flag == FALSE ) { - char *s_dup = strdup(y); - inp_readall(newfp, &libraries[num_libraries-1], call_depth+1, ngdirname(s_dup), FALSE); - tfree(s_dup); + char *y_dir_name = ngdirname(y); + inp_readall(newfp, &libraries[num_libraries-1], call_depth+1, y_dir_name, FALSE); + tfree(y_dir_name); } else { inp_readall(newfp, &libraries[num_libraries-1], call_depth+1, dir_name, FALSE); } @@ -387,9 +387,9 @@ inp_readall(FILE *fp, struct line **data, int call_depth, char *dir_name, bool c } if ( dir_name_flag == FALSE ) { - char *s_dup = strdup(y); - inp_readall(newfp, &newcard, call_depth+1, ngdirname(s_dup), FALSE); /* read stuff in include file into netlist */ - tfree(s_dup); + char *y_dir_name = ngdirname(y); + inp_readall(newfp, &newcard, call_depth+1, y_dir_name, FALSE); /* read stuff in include file into netlist */ + tfree(y_dir_name); } else { inp_readall(newfp, &newcard, call_depth+1, dir_name, FALSE); /* read stuff in include file into netlist */ } diff --git a/src/misc/ivars.c b/src/misc/ivars.c index 7619ab2d7..7ec26c32d 100644 --- a/src/misc/ivars.c +++ b/src/misc/ivars.c @@ -88,6 +88,7 @@ ivars(char *argv0) /* set path either to /input or, if set, to environment variable NGSPICE_INPUT_DIR */ mkvar(&Inp_Path, ngpath, "input", "NGSPICE_INPUT_DIR"); + tfree(ngpath); } #else NG_IGNORE(argv0); diff --git a/src/misc/util.c b/src/misc/util.c index ea68a29a3..917d38863 100644 --- a/src/misc/util.c +++ b/src/misc/util.c @@ -193,15 +193,10 @@ basename(const char *name) char * ngdirname(const char *name) { - static char *ret = NULL; + char *ret; const char *end = NULL; int start = 0; - if (ret) { - free(ret); - ret = NULL; - } - if(name && ((name[0] >= 'a' && name[0] <= 'z') || (name[0] >= 'A' && name[0] <= 'Z')) && name[1] == ':') start = 2; @@ -237,14 +232,9 @@ ngdirname(const char *name) char * ngdirname(const char *name) { - static char *ret = NULL; + char *ret; const char *end; - if (ret) { - free(ret); - ret = NULL; - } - end = name ? strrchr(name, '/') : NULL; if(end && end == name)