From 6d94d4115c254d344bbb927596a7141ef39fd298 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Wed, 21 Jul 2021 11:51:56 +0200 Subject: Add missing cleanup on error paths --- src/ambigator.c | 18 +++++++++++++++--- src/cl-utils.c | 10 ++++++++-- src/crystfelindexingopts.c | 5 ++++- src/gui_backend_slurm.c | 5 ++++- src/gui_index.c | 5 ++++- src/gui_merge.c | 10 ++++++++-- src/gui_project.c | 1 + src/im-sandbox.c | 1 + src/im-zmq.c | 11 +++++++++-- src/partial_sim.c | 3 +++ src/post-refinement.c | 1 + src/scaling.c | 1 + src/whirligig.c | 5 ++++- 13 files changed, 63 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/ambigator.c b/src/ambigator.c index 298002a7..1657bfb6 100644 --- a/src/ambigator.c +++ b/src/ambigator.c @@ -196,7 +196,7 @@ static struct flist *asymm_and_merge(RefList *in, const SymOpList *sym, || (f->s_reidx == NULL) || (f->i_reidx == NULL) || (f->group_reidx == NULL) || (f->group == NULL) ) { ERROR("Failed to allocate flist\n"); - return NULL; + goto out; } f->n = 0; @@ -218,7 +218,7 @@ static struct flist *asymm_and_merge(RefList *in, const SymOpList *sym, if ( amb != NULL ) { RefList *reidx = reflist_new(); - if ( reidx == NULL ) return NULL; + if ( reidx == NULL ) goto out; for ( refl = first_refl(asym, &iter); refl != NULL; @@ -236,7 +236,8 @@ static struct flist *asymm_and_merge(RefList *in, const SymOpList *sym, cr = add_refl(reidx, hra, kra, lra); if ( cr == NULL ) { ERROR("Failed to add reflection\n"); - return NULL; + reflist_free(reidx); + goto out; } copy_data(cr, refl); } @@ -260,6 +261,16 @@ static struct flist *asymm_and_merge(RefList *in, const SymOpList *sym, reflist_free(asym); return f; + +out: + free(f->s); + free(f->s_reidx); + free(f->i); + free(f->i_reidx); + free(f->group); + free(f->group_reidx); + free(f); + return NULL; } @@ -747,6 +758,7 @@ static void write_reindexed_stream(const char *infile, const char *outfile, ofh = fopen(outfile, "w"); if ( ofh == NULL ) { ERROR("Failed to open '%s'\n", outfile); + fclose(fh); return; } diff --git a/src/cl-utils.c b/src/cl-utils.c index ec449435..c2c1bf1d 100644 --- a/src/cl-utils.c +++ b/src/cl-utils.c @@ -283,7 +283,10 @@ cl_program load_program_from_string(const char *source_in, size_t len, char *source2; source2 = malloc(strlen(source)+strlen(insert_stuff)+1); - if ( source2 == NULL ) return 0; + if ( source2 == NULL ) { + free(source); + return 0; + } il = insert_pos - source; memcpy(source2, source, il); @@ -336,7 +339,10 @@ cl_program load_program(const char *filename, cl_context ctx, return 0; } source = malloc(16384); - if ( source == NULL ) return 0; + if ( source == NULL ) { + fclose(fh); + return 0; + } len = fread(source, 1, 16383, fh); fclose(fh); diff --git a/src/crystfelindexingopts.c b/src/crystfelindexingopts.c index 8c7c5b5d..b5fd1acb 100644 --- a/src/crystfelindexingopts.c +++ b/src/crystfelindexingopts.c @@ -1067,7 +1067,10 @@ char **crystfel_indexing_opts_get_metadata_to_copy(CrystFELIndexingOpts *opts, r = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(opts->copy_metadata_store), &iter); - if ( r == FALSE ) return NULL; + if ( r == FALSE ) { + free(arr); + return NULL; + } i = 0; do { diff --git a/src/gui_backend_slurm.c b/src/gui_backend_slurm.c index d07675be..801921ea 100644 --- a/src/gui_backend_slurm.c +++ b/src/gui_backend_slurm.c @@ -270,7 +270,10 @@ static char **create_env(int *psize, char *path_add) } env[0] = malloc(path_len); - if ( env[0] == NULL ) return NULL; + if ( env[0] == NULL ) { + free(env); + return NULL; + } strcpy(env[0], base_path); if ( crystfel_path != NULL ) { diff --git a/src/gui_index.c b/src/gui_index.c index 55a758f0..a9037003 100644 --- a/src/gui_index.c +++ b/src/gui_index.c @@ -830,7 +830,10 @@ static char **indexamajig_command_line(const char *geom_filename, if ( args == NULL ) return NULL; indexamajig_path = get_crystfel_exe("indexamajig"); - if ( indexamajig_path == NULL ) return NULL; + if ( indexamajig_path == NULL ) { + free(args); + return NULL; + } /* The basics */ add_arg(args, n_args++, indexamajig_path); diff --git a/src/gui_merge.c b/src/gui_merge.c index b911fa5a..ca3c3a46 100644 --- a/src/gui_merge.c +++ b/src/gui_merge.c @@ -364,7 +364,10 @@ static int write_partialator_script(const char *filename, fprintf(fh, "#!/bin/sh\n"); exe_path = get_crystfel_exe("partialator"); - if ( exe_path == NULL ) return 1; + if ( exe_path == NULL ) { + fclose(fh); + return 1; + } fprintf(fh, "%s \\\n", exe_path); for ( i=0; in_streams; i++ ) { @@ -468,7 +471,10 @@ static int write_process_hkl_script(const char *filename, fprintf(fh, "#!/bin/sh\n"); exe_path = get_crystfel_exe("process_hkl"); - if ( exe_path == NULL ) return 1; + if ( exe_path == NULL ) { + fclose(fh); + return 1; + } add_process_hkl(fh, exe_path, input, params, out_hkl, stdout_filename, stderr_filename, "", ""); diff --git a/src/gui_project.c b/src/gui_project.c index 854b93a5..f72af893 100644 --- a/src/gui_project.c +++ b/src/gui_project.c @@ -876,6 +876,7 @@ int load_project(struct crystfelproject *proj) if ( default_project(proj) ) { ERROR("Failed to make default project when loading.\n"); + fclose(fh); return 1; } diff --git a/src/im-sandbox.c b/src/im-sandbox.c index 4d40c461..62483044 100644 --- a/src/im-sandbox.c +++ b/src/im-sandbox.c @@ -529,6 +529,7 @@ static void add_pipe(struct sandbox *sb, int fd) fhs_new = realloc(sb->fhs, (sb->n_read+1)*sizeof(FILE *)); if ( fhs_new == NULL ) { ERROR("Failed to allocate memory for new FH.\n"); + free(fds_new); return; } diff --git a/src/im-zmq.c b/src/im-zmq.c index c2d386cf..4273b600 100644 --- a/src/im-zmq.c +++ b/src/im-zmq.c @@ -68,7 +68,10 @@ struct im_zmq *im_zmq_connect(const char *zmq_address, if ( z == NULL ) return NULL; z->ctx = zmq_ctx_new(); - if ( z->ctx == NULL ) return NULL; + if ( z->ctx == NULL ) { + free(z); + return NULL; + } if ( zmq_request == NULL ) { STATUS("Connecting ZMQ subscriber to '%s'\n", zmq_address); @@ -77,10 +80,14 @@ struct im_zmq *im_zmq_connect(const char *zmq_address, STATUS("Connecting ZMQ requester to '%s'\n", zmq_address); z->socket = zmq_socket(z->ctx, ZMQ_REQ); } - if ( z->socket == NULL ) return NULL; + if ( z->socket == NULL ) { + free(z); + return NULL; + } if ( zmq_connect(z->socket, zmq_address) == -1 ) { ERROR("ZMQ connection failed: %s\n", zmq_strerror(errno)); + free(z); return NULL; } diff --git a/src/partial_sim.c b/src/partial_sim.c index 63851b9c..efba8ebd 100644 --- a/src/partial_sim.c +++ b/src/partial_sim.c @@ -349,11 +349,13 @@ static void *create_job(void *vqargs) STREAM_REFLECTIONS); if ( image == NULL ) { ERROR("Failed to read template chunk!\n"); + free(wargs); return NULL; } if ( image->n_crystals != 1 ) { ERROR("Template stream must have exactly one crystal " "per frame.\n"); + free(wargs); return NULL; } @@ -947,6 +949,7 @@ int main(int argc, char *argv[]) if ( fh == NULL ) { ERROR("Failed to open /dev/urandom. Try again without" " --really-random.\n"); + free(qargs.rngs); return 1; } diff --git a/src/post-refinement.c b/src/post-refinement.c index d4aeecef..27a8cd6a 100644 --- a/src/post-refinement.c +++ b/src/post-refinement.c @@ -821,6 +821,7 @@ void refine_all(Crystal **crystals, int n_crystals, task_defaults.sym = sym; task_defaults.amb = amb; task_defaults.scaleflags = scaleflags; + task_defaults.serial = 0; qargs.task_defaults = task_defaults; qargs.n_started = 0; diff --git a/src/scaling.c b/src/scaling.c index 3be8712b..cd4c5d92 100644 --- a/src/scaling.c +++ b/src/scaling.c @@ -130,6 +130,7 @@ void scale_all(Crystal **crystals, int n_crystals, int nthreads, int scaleflags) task_defaults.crystal = NULL; task_defaults.flags = scaleflags; + task_defaults.full = NULL; /* (not used) */ qargs.task_defaults = task_defaults; qargs.n_crystals = n_crystals; diff --git a/src/whirligig.c b/src/whirligig.c index d964557f..546301ba 100644 --- a/src/whirligig.c +++ b/src/whirligig.c @@ -167,7 +167,10 @@ static void process_series(struct image *images, signed int *ser, } p = calloc(len, sizeof(RefList *)); - if ( p == NULL ) return; + if ( p == NULL ) { + fclose(fh); + return; + } fprintf(fh, "%i frames in series\n\n", len); fprintf(fh, " # Serial Filename EventID Crystal\n"); -- cgit v1.2.3