aboutsummaryrefslogtreecommitdiff
path: root/libcrystfel/src
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2021-07-21 12:11:42 +0200
committerThomas White <taw@physics.org>2021-07-21 15:34:33 +0200
commit760f17f56b7f79f67a9029cbe7cc55a0acccd3b9 (patch)
tree608a86b923abe5564c7d1624f54fdf9b90b9a6ee /libcrystfel/src
parent6d94d4115c254d344bbb927596a7141ef39fd298 (diff)
Make sure that memory gets freed on realloc failure
Diffstat (limited to 'libcrystfel/src')
-rw-r--r--libcrystfel/src/fom.c4
-rw-r--r--libcrystfel/src/image-cbf.c2
-rw-r--r--libcrystfel/src/indexers/taketwo.c2
-rw-r--r--libcrystfel/src/integration.c2
-rw-r--r--libcrystfel/src/spectrum.c10
-rw-r--r--libcrystfel/src/utils.c12
-rw-r--r--libcrystfel/src/utils.h1
7 files changed, 25 insertions, 8 deletions
diff --git a/libcrystfel/src/fom.c b/libcrystfel/src/fom.c
index 2f74e945..9ecb082d 100644
--- a/libcrystfel/src/fom.c
+++ b/libcrystfel/src/fom.c
@@ -731,8 +731,8 @@ static int wilson_scale(RefList *list1, RefList *list2, UnitCell *cell)
if ( n == max_n ) {
max_n *= 2;
- x = realloc(x, max_n*sizeof(double));
- y = realloc(y, max_n*sizeof(double));
+ x = srealloc(x, max_n*sizeof(double));
+ y = srealloc(y, max_n*sizeof(double));
if ( (x==NULL) || (y==NULL) ) {
ERROR("Failed to allocate memory for scaling.\n");
return 1;
diff --git a/libcrystfel/src/image-cbf.c b/libcrystfel/src/image-cbf.c
index 1e5659f4..8a97cc6d 100644
--- a/libcrystfel/src/image-cbf.c
+++ b/libcrystfel/src/image-cbf.c
@@ -307,7 +307,7 @@ static float *read_cbf_data(const char *filename, int gz, int *w, int *h)
if ( len_read == bufinc ) {
bufsz += bufinc;
- buf = realloc(buf, bufsz);
+ buf = srealloc(buf, bufsz);
if ( buf == NULL ) return NULL;
}
diff --git a/libcrystfel/src/indexers/taketwo.c b/libcrystfel/src/indexers/taketwo.c
index cd4d87a6..72aa0bad 100644
--- a/libcrystfel/src/indexers/taketwo.c
+++ b/libcrystfel/src/indexers/taketwo.c
@@ -1751,7 +1751,7 @@ static int match_obs_to_cell_vecs(struct TheoryVec *cell_vecs, int cell_vec_coun
/* we have a match, add to array! */
size_t new_size = (count+1)*sizeof(struct sortme);
- for_sort = realloc(for_sort, new_size);
+ for_sort = srealloc(for_sort, new_size);
if ( for_sort == NULL ) return 0;
diff --git a/libcrystfel/src/integration.c b/libcrystfel/src/integration.c
index 326e3183..2375388f 100644
--- a/libcrystfel/src/integration.c
+++ b/libcrystfel/src/integration.c
@@ -1588,7 +1588,7 @@ static double estimate_resolution(Crystal *cr, struct image *image)
acc[n_acc++] = res;
if ( n_acc == max_acc ) {
max_acc += 1024;
- acc = realloc(acc, max_acc*sizeof(double));
+ acc = srealloc(acc, max_acc*sizeof(double));
if ( acc == NULL ) {
ERROR("Allocation failed during"
" estimate_resolution!\n");
diff --git a/libcrystfel/src/spectrum.c b/libcrystfel/src/spectrum.c
index bf38a73a..91bc48c9 100644
--- a/libcrystfel/src/spectrum.c
+++ b/libcrystfel/src/spectrum.c
@@ -390,9 +390,13 @@ static int read_esrf_spectrum(FILE *fh, Spectrum *s)
if ( n_bins == max_bins ) {
max_bins += 64;
- k = realloc(k, max_bins*sizeof(double));
- samp = realloc(samp, max_bins*sizeof(double));
- if ( (k==NULL) || (samp==NULL) ) return 1;
+ k = srealloc(k, max_bins*sizeof(double));
+ samp = srealloc(samp, max_bins*sizeof(double));
+ if ( (k==NULL) || (samp==NULL) ) {
+ free(k);
+ free(samp);
+ return 1;
+ }
}
k[n_bins] = ph_eV_to_k(energy*1000.0);
diff --git a/libcrystfel/src/utils.c b/libcrystfel/src/utils.c
index 4aecfc37..f787f82f 100644
--- a/libcrystfel/src/utils.c
+++ b/libcrystfel/src/utils.c
@@ -856,6 +856,18 @@ int compare_double(const void *av, const void *bv)
}
+void *srealloc(void *arr, size_t new_size)
+{
+ void *new_arr = realloc(arr, new_size);
+ if ( new_arr == NULL ) {
+ free(arr);
+ return NULL;
+ } else {
+ return new_arr;
+ }
+}
+
+
/* -------------------------- libcrystfel features ------------------------ */
int crystfel_has_peakfinder9()
diff --git a/libcrystfel/src/utils.h b/libcrystfel/src/utils.h
index 458307e8..9683039e 100644
--- a/libcrystfel/src/utils.h
+++ b/libcrystfel/src/utils.h
@@ -81,6 +81,7 @@ extern size_t notrail(char *s);
extern int convert_int(const char *str, int *pval);
extern int convert_float(const char *str, double *pval);
extern void chomp(char *s);
+extern void *srealloc(void *arr, size_t new_size);
#define CLEAR_BIT(val, bit) (((val) | (bit)) ^ (bit))