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.
This commit is contained in:
rlar 2012-09-13 20:07:33 +02:00
parent 7c9b90c9ea
commit adc9ee09ce
5 changed files with 17 additions and 22 deletions

View File

@ -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 */

View File

@ -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 */

View File

@ -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 */
}

View File

@ -88,6 +88,7 @@ ivars(char *argv0)
/* set path either to <ngspice-bin-directory>/input or,
if set, to environment variable NGSPICE_INPUT_DIR */
mkvar(&Inp_Path, ngpath, "input", "NGSPICE_INPUT_DIR");
tfree(ngpath);
}
#else
NG_IGNORE(argv0);

View File

@ -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)