memory fix

This commit is contained in:
Ian Jauslin 2023-11-03 17:41:30 -04:00
parent 16c80d2305
commit 209ba06cbf
2 changed files with 59 additions and 13 deletions

View File

@ -168,7 +168,8 @@ int dstring_to_str(dstring input, char** output){
} }
// noinit (changes the size of input if needed) // noinit (changes the size of input if needed)
char* dstring_to_str_noinit(dstring* input){ 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){ if(input->length==input->memory){
dstring_resize(input,input->length+1); dstring_resize(input,input->length+1);
} }

View File

@ -128,7 +128,13 @@ int main (
// read command line arguments // read command line arguments
ret=read_args(argc, argv, &param_str, &command, &nthreads, &savefile_str, &utfile_str, &resumefile_str); ret=read_args(argc, argv, &param_str, &command, &nthreads, &savefile_str, &utfile_str, &resumefile_str);
if(ret<0){ 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 // set default params
@ -136,38 +142,58 @@ int main (
// read params // read params
ret=read_params(param_str, &parameters, &initfile_str, &drivingfile_str); ret=read_params(param_str, &parameters, &initfile_str, &drivingfile_str);
if(ret<0){ 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 is 'resume', then read args from file
if(command==COMMAND_RESUME){ if(command==COMMAND_RESUME){
ret=args_from_file(&param_str, &command, &nthreads, &savefile_str, &utfile_str, dstring_to_str_noinit(&resumefile_str)); ret=args_from_file(&param_str, &command, &nthreads, &savefile_str, &utfile_str, dstring_to_str_noinit(&resumefile_str));
if(ret<0){ 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 // read params
ret=read_params(param_str, &parameters, &initfile_str, &drivingfile_str); ret=read_params(param_str, &parameters, &initfile_str, &drivingfile_str);
if(ret<0){ 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) // reread arguments (to allow overrides from the command line)
ret=read_args(argc, argv, &param_str, &command, &nthreads, &savefile_str, &utfile_str, &resumefile_str); read_args(argc, argv, &param_str, &command, &nthreads, &savefile_str, &utfile_str, &resumefile_str);
if(ret<0){
return(-1);
}
// reread params // reread params
ret=read_params(param_str, &parameters, &initfile_str, &drivingfile_str); read_params(param_str, &parameters, &initfile_str, &drivingfile_str);
if(ret<0){
return(-1);
}
} }
// free strings
dstring_free(resumefile_str);
// open initfile // open initfile
if(initfile_str.length!=0){ if(initfile_str.length!=0){
parameters.initfile=fopen(dstring_to_str_noinit(&initfile_str),"r"); parameters.initfile=fopen(dstring_to_str_noinit(&initfile_str),"r");
if(parameters.initfile==NULL){ if(parameters.initfile==NULL){
fprintf(stderr,"Error opening file '%s' for reading: %s\n", dstring_to_str_noinit(&initfile_str), strerror(errno)); 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); return(-1);
} }
} }
@ -176,6 +202,11 @@ int main (
parameters.drivingfile=fopen(dstring_to_str_noinit(&drivingfile_str),"r"); parameters.drivingfile=fopen(dstring_to_str_noinit(&drivingfile_str),"r");
if(parameters.drivingfile==NULL){ if(parameters.drivingfile==NULL){
fprintf(stderr,"Error opening file '%s' for reading: %s\n", dstring_to_str_noinit(&drivingfile_str), strerror(errno)); 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); return(-1);
} }
} }
@ -199,6 +230,13 @@ int main (
savefile=fopen(dstring_to_str_noinit(&savefile_str),"w"); savefile=fopen(dstring_to_str_noinit(&savefile_str),"w");
if(savefile==NULL){ if(savefile==NULL){
fprintf(stderr,"Error opening file '%s' for writing: %s\n", dstring_to_str_noinit(&savefile_str), strerror(errno)); 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); return(-1);
} }
} }
@ -208,6 +246,13 @@ int main (
utfile=fopen(dstring_to_str_noinit(&utfile_str),"w"); utfile=fopen(dstring_to_str_noinit(&utfile_str),"w");
if(utfile==NULL){ if(utfile==NULL){
fprintf(stderr,"Error opening file '%s' for writing: %s\n", dstring_to_str_noinit(&utfile_str), strerror(errno)); 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); return(-1);
} }
} }