From 209ba06cbf338fa6fdbb9f4fe767463ff78d86e7 Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Fri, 3 Nov 2023 17:41:30 -0400 Subject: [PATCH] memory fix --- src/dstring.c | 3 ++- src/main.c | 69 ++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/src/dstring.c b/src/dstring.c index 134968a..59552fc 100644 --- a/src/dstring.c +++ b/src/dstring.c @@ -168,7 +168,8 @@ int dstring_to_str(dstring input, char** output){ } // noinit (changes the size of input if needed) char* dstring_to_str_noinit(dstring* input){ - if(input->string[input->length-1]!='\0'){ + // check if string ends in a trailing '\0' (or if string is empty) + if(input->length==0 || input->string[input->length-1]!='\0'){ if(input->length==input->memory){ dstring_resize(input,input->length+1); } diff --git a/src/main.c b/src/main.c index 762bcd2..fe3b77a 100644 --- a/src/main.c +++ b/src/main.c @@ -128,7 +128,13 @@ int main ( // read command line arguments ret=read_args(argc, argv, ¶m_str, &command, &nthreads, &savefile_str, &utfile_str, &resumefile_str); if(ret<0){ - return(-1); + dstring_free(param_str); + dstring_free(savefile_str); + dstring_free(utfile_str); + dstring_free(initfile_str); + dstring_free(drivingfile_str); + dstring_free(resumefile_str); + return(ret); } // set default params @@ -136,38 +142,58 @@ int main ( // read params ret=read_params(param_str, ¶meters, &initfile_str, &drivingfile_str); if(ret<0){ - return(-1); + dstring_free(param_str); + dstring_free(savefile_str); + dstring_free(utfile_str); + dstring_free(initfile_str); + dstring_free(drivingfile_str); + dstring_free(resumefile_str); + return(ret); } // if command is 'resume', then read args from file if(command==COMMAND_RESUME){ ret=args_from_file(¶m_str, &command, &nthreads, &savefile_str, &utfile_str, dstring_to_str_noinit(&resumefile_str)); if(ret<0){ - return(-1); + dstring_free(param_str); + dstring_free(savefile_str); + dstring_free(utfile_str); + dstring_free(initfile_str); + dstring_free(drivingfile_str); + dstring_free(resumefile_str); + return(ret); } // read params ret=read_params(param_str, ¶meters, &initfile_str, &drivingfile_str); if(ret<0){ - return(-1); + dstring_free(param_str); + dstring_free(savefile_str); + dstring_free(utfile_str); + dstring_free(initfile_str); + dstring_free(drivingfile_str); + dstring_free(resumefile_str); + return(ret); } // reread arguments (to allow overrides from the command line) - ret=read_args(argc, argv, ¶m_str, &command, &nthreads, &savefile_str, &utfile_str, &resumefile_str); - if(ret<0){ - return(-1); - } + read_args(argc, argv, ¶m_str, &command, &nthreads, &savefile_str, &utfile_str, &resumefile_str); // reread params - ret=read_params(param_str, ¶meters, &initfile_str, &drivingfile_str); - if(ret<0){ - return(-1); - } + read_params(param_str, ¶meters, &initfile_str, &drivingfile_str); } + // free strings + dstring_free(resumefile_str); + // open initfile if(initfile_str.length!=0){ parameters.initfile=fopen(dstring_to_str_noinit(&initfile_str),"r"); if(parameters.initfile==NULL){ fprintf(stderr,"Error opening file '%s' for reading: %s\n", dstring_to_str_noinit(&initfile_str), strerror(errno)); + dstring_free(param_str); + dstring_free(savefile_str); + dstring_free(utfile_str); + dstring_free(initfile_str); + dstring_free(drivingfile_str); return(-1); } } @@ -176,6 +202,11 @@ int main ( parameters.drivingfile=fopen(dstring_to_str_noinit(&drivingfile_str),"r"); if(parameters.drivingfile==NULL){ fprintf(stderr,"Error opening file '%s' for reading: %s\n", dstring_to_str_noinit(&drivingfile_str), strerror(errno)); + dstring_free(param_str); + dstring_free(savefile_str); + dstring_free(utfile_str); + dstring_free(initfile_str); + dstring_free(drivingfile_str); return(-1); } } @@ -199,6 +230,13 @@ int main ( savefile=fopen(dstring_to_str_noinit(&savefile_str),"w"); if(savefile==NULL){ fprintf(stderr,"Error opening file '%s' for writing: %s\n", dstring_to_str_noinit(&savefile_str), strerror(errno)); + dstring_free(param_str); + dstring_free(savefile_str); + dstring_free(utfile_str); + dstring_free(initfile_str); + dstring_free(drivingfile_str); + free(g); + free(u0); return(-1); } } @@ -208,6 +246,13 @@ int main ( utfile=fopen(dstring_to_str_noinit(&utfile_str),"w"); if(utfile==NULL){ fprintf(stderr,"Error opening file '%s' for writing: %s\n", dstring_to_str_noinit(&utfile_str), strerror(errno)); + dstring_free(param_str); + dstring_free(savefile_str); + dstring_free(utfile_str); + dstring_free(initfile_str); + dstring_free(drivingfile_str); + free(g); + free(u0); return(-1); } }