From b4b2c91481f5f1d65d232661bfe3336605be68bd Mon Sep 17 00:00:00 2001 From: stefan schippers Date: Fri, 5 May 2023 09:29:30 +0200 Subject: [PATCH] fix yet another error in "cmd" string handling in load_sym_def(). Added realloc of freed pointers detection in track_memory.awk --- src/main.c | 2 +- src/save.c | 8 +++++--- src/track_memory.awk | 22 ++++++++++++++++------ 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/main.c b/src/main.c index efb25247..b0452b26 100644 --- a/src/main.c +++ b/src/main.c @@ -71,7 +71,6 @@ static void child_handler(int signum) int main(int argc, char **argv) { int i; - my_strdup(_ALLOC_ID_, &xschem_executable, argv[0]); signal(SIGINT, sig_handler); signal(SIGSEGV, sig_handler); signal(SIGILL, sig_handler); @@ -85,6 +84,7 @@ int main(int argc, char **argv) if(!getenv("DISPLAY") || !getenv("DISPLAY")[0]) has_x=0; #endif argc = process_options(argc, argv); + my_strdup(_ALLOC_ID_, &xschem_executable, argv[0]); if(debug_var>=1 && !has_x) fprintf(errfp, "main(): no DISPLAY set, assuming no X available\n"); /* if detach is 1 no interactive command shell is created ... diff --git a/src/save.c b/src/save.c index 378db609..3339ace0 100644 --- a/src/save.c +++ b/src/save.c @@ -3156,6 +3156,7 @@ int load_sym_def(const char *name, FILE *embed_fd) xSymbol * symbol; int symbols, sym_n_pins=0, generator; char *cmd = NULL; + char *translated_cmd = NULL; if(!name) { dbg(0, "l_s_d(): Warning: name parameter set to NULL, returning with no action\n"); @@ -3175,13 +3176,14 @@ int load_sym_def(const char *name, FILE *embed_fd) dbg(1, "l_s_d(): cmd=%s\n", cmd); generator = is_generator(cmd); if(generator) { - cmd = get_generator_command(cmd); + translated_cmd = get_generator_command(cmd); dbg(1, "l_s_d(): generator: cmd=%s\n", cmd); - if(cmd) { - lcc[level].fd = popen(cmd, "r"); /* execute ss="/path/to/xxx par1 par2 ..." and pipe in the stdout */ + if(translated_cmd) { + lcc[level].fd = popen(translated_cmd, "r"); /* execute ss="/path/to/xxx par1 par2 ..." and pipe in the stdout */ } else { lcc[level].fd = NULL; } + my_free(_ALLOC_ID_, &translated_cmd); } else if(!embed_fd) { /* regular symbol: open file */ if(!strcmp(xctx->file_version,"1.0")) { my_strncpy(sympath, abs_sym_path(name, ".sym"), S(sympath)); diff --git a/src/track_memory.awk b/src/track_memory.awk index 5a6204fb..17c6ed2f 100755 --- a/src/track_memory.awk +++ b/src/track_memory.awk @@ -52,6 +52,10 @@ BEGIN{ idx[$5] = id } else { # realloc + if(!($3 in address)) { + print "Corruption: address " $3 " was freed. Can not realloc. id=" id + print_source($3) + } total += $7 - address[$3] if(total > max) max = total delete address[$3] @@ -73,14 +77,20 @@ END{ for(i in address) { stale++ leak+= address[i] - print " address[ " i ", " idx[i] " ]= " address[i] - if(show_source) { - pipe = "egrep -n 'my_(malloc|calloc|realloc|free|mstrcat|strcat|strncat|strdup|strdup2)\\(" idx[i] ",' *.c xschem.h" - while( pipe | getline a) print " " a - close(pipe) - } + print_source(i) } print "Number of unfreed pointers = " stale # as a crosscheck 'leak' should be equal to 'total'. print "Total leaked memory = " leak } + +function print_source(add) +{ + print " address[ " add ", " idx[add] " ]= " address[add] + if(show_source) { + pipe = "egrep -n 'my_(malloc|calloc|realloc|free|mstrcat|strcat|strncat|strdup|strdup2)\\(" idx[add] \ + ",' *.c xschem.h" + while( pipe | getline a) print " " a + close(pipe) + } +}