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 --- libcrystfel/src/indexers/asdf.c | 12 ++++++++++-- libcrystfel/src/indexers/felix.c | 2 ++ libcrystfel/src/indexers/fromfile.c | 5 ++++- libcrystfel/src/indexers/mosflm.c | 2 ++ libcrystfel/src/indexers/taketwo.c | 5 +++++ 5 files changed, 23 insertions(+), 3 deletions(-) (limited to 'libcrystfel/src/indexers') diff --git a/libcrystfel/src/indexers/asdf.c b/libcrystfel/src/indexers/asdf.c index a8943688..7030a3e7 100644 --- a/libcrystfel/src/indexers/asdf.c +++ b/libcrystfel/src/indexers/asdf.c @@ -180,11 +180,18 @@ static struct asdf_cell *asdf_cell_new(int n) if (c->reflections == NULL) return NULL; c->indices = malloc(sizeof(double *) * n); - if (c->indices == NULL) return NULL; + if (c->indices == NULL) { + free(c->reflections); + return NULL; + } for ( i = 0; i < n; i++ ) { c->indices[i] = malloc(sizeof(double) * 3); - if (c->indices[i] == NULL) return NULL; + if (c->indices[i] == NULL) { + free(c->reflections); + free(c->indices); + return NULL; + } } c->n = 0; @@ -973,6 +980,7 @@ static int index_refls(gsl_vector **reflections, int N_reflections, struct tvector *tvectors = malloc(N_triplets * sizeof(struct tvector)); if (tvectors == NULL) { ERROR("Failed to allocate tvectors in index_refls!\n"); + free(fits); return 0; } diff --git a/libcrystfel/src/indexers/felix.c b/libcrystfel/src/indexers/felix.c index db9cccf8..b7d2a8a3 100644 --- a/libcrystfel/src/indexers/felix.c +++ b/libcrystfel/src/indexers/felix.c @@ -143,6 +143,7 @@ static int read_felix(struct felix_private *gp, struct image *image, /* Read and discard first line */ if ( fgets( line, 1024, fh ) == NULL ) { ERROR("Failed to read *.felix file.\n"); + fclose(fh); return 0; } @@ -639,6 +640,7 @@ void *felix_prepare(IndexingMethod *indm, UnitCell *cell, if ( gp->spacegroup == 0 ) { ERROR("Couldn't determine representative space group for your cell.\n"); ERROR("Try again with a more conventional cell.\n"); + free(gp); return NULL; } diff --git a/libcrystfel/src/indexers/fromfile.c b/libcrystfel/src/indexers/fromfile.c index f0372489..d7dfbd09 100644 --- a/libcrystfel/src/indexers/fromfile.c +++ b/libcrystfel/src/indexers/fromfile.c @@ -211,7 +211,10 @@ void *fromfile_prepare(IndexingMethod *indm, struct fromfile_options *opts) } dp = malloc(sizeof(struct fromfile_private)); - if ( dp == NULL ) return NULL; + if ( dp == NULL ) { + fclose(fh); + return NULL; + } dp->sol_hash = NULL; diff --git a/libcrystfel/src/indexers/mosflm.c b/libcrystfel/src/indexers/mosflm.c index c56f3ae6..129e6859 100644 --- a/libcrystfel/src/indexers/mosflm.c +++ b/libcrystfel/src/indexers/mosflm.c @@ -244,6 +244,7 @@ static int read_newmat(struct mosflm_data *mosflm, const char *filename, n += fscanf(fh, "%f %f %f\n", &asz, &bsz, &csz); if ( n != 9 ) { STATUS("Fewer than 9 parameters found in NEWMAT file.\n"); + fclose(fh); return 1; } @@ -371,6 +372,7 @@ static void write_img(struct image *image, const char *filename) fh = fopen(filename, "w"); if ( !fh ) { ERROR("Couldn't open temporary file '%s'\n", filename); + free(intimage); return; } diff --git a/libcrystfel/src/indexers/taketwo.c b/libcrystfel/src/indexers/taketwo.c index 60bb6fed..cd4d87a6 100644 --- a/libcrystfel/src/indexers/taketwo.c +++ b/libcrystfel/src/indexers/taketwo.c @@ -1769,6 +1769,10 @@ static int match_obs_to_cell_vecs(struct TheoryVec *cell_vecs, int cell_vec_coun match_array = &(obs_vecs[i].matches); match_count = &(obs_vecs[i].match_num); + if ( for_sort == NULL ) { + return 0; + } + /* Sort in order to get most agreeable matches first */ qsort(for_sort, count, sizeof(struct sortme), sort_theory_distances); *match_array = malloc(count*sizeof(struct TheoryVec)); @@ -1831,6 +1835,7 @@ static int gen_observed_vecs(struct rvec *rlps, int rlp_count, spot_vec.match_num = 0; spot_vec.her_rlp = &rlps[i]; spot_vec.his_rlp = &rlps[j]; + spot_vec.in_network = 0; cell->obs_vecs[count - 1] = spot_vec; } -- cgit v1.2.3