From 45038c42847476ce1aee5b09c47fb330a0108cd6 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Mon, 23 Oct 2017 15:53:19 +0200 Subject: Check return values from fread/fgets --- libcrystfel/src/dirax.c | 9 ++++++--- libcrystfel/src/felix.c | 9 ++++++--- libcrystfel/src/mosflm.c | 16 ++++++++++------ libcrystfel/src/xds.c | 11 +++++++---- src/ambigator.c | 7 +++++-- src/partial_sim.c | 9 ++++++--- src/partialator.c | 5 ++++- src/pattern_sim.c | 7 +++++-- tests/integration_check.c | 7 +++++-- tests/prof2d_check.c | 7 +++++-- tests/ring_check.c | 6 +++++- 11 files changed, 64 insertions(+), 29 deletions(-) diff --git a/libcrystfel/src/dirax.c b/libcrystfel/src/dirax.c index 488eb7db..512a61e1 100644 --- a/libcrystfel/src/dirax.c +++ b/libcrystfel/src/dirax.c @@ -660,9 +660,12 @@ const char *dirax_probe(UnitCell *cell) } fh = fdopen(pty, "r"); - fgets(line, 1024, fh); - if ( strncmp(line, "dirax", 5) == 0 ) { - ok = 1; + if ( fgets(line, 1024, fh) == NULL ) { + ERROR("Failed to probe for DirAx\n"); + } else { + if ( strncmp(line, "dirax", 5) == 0 ) { + ok = 1; + } } fclose(fh); diff --git a/libcrystfel/src/felix.c b/libcrystfel/src/felix.c index cf7a38e8..79252b2b 100644 --- a/libcrystfel/src/felix.c +++ b/libcrystfel/src/felix.c @@ -757,9 +757,12 @@ const char *felix_probe(UnitCell *cell) } fh = fdopen(pty, "r"); - fgets(line, 1024, fh); - if ( strncmp(line, "Felix", 5) == 0 ) { - ok = 1; + if ( fgets(line, 1024, fh) == NULL ) { + ERROR("Failed to probe for Felix\n"); + } else { + if ( strncmp(line, "Felix", 5) == 0 ) { + ok = 1; + } } fclose(fh); diff --git a/libcrystfel/src/mosflm.c b/libcrystfel/src/mosflm.c index 14cd9d96..7ebf6e19 100644 --- a/libcrystfel/src/mosflm.c +++ b/libcrystfel/src/mosflm.c @@ -896,12 +896,16 @@ const char *mosflm_probe(UnitCell *cell) for ( l=0; l<10; l++ ) { char *pos; - fgets(line, 1024, fh); - pos = strstr(line, "Mosflm version "); - if ( pos != NULL ) { - char *vers = pos+15; - ok = 1; - chop_word(vers); + if ( fgets(line, 1024, fh) == NULL ) { + ERROR("Failed to probe for Mosflm\n"); + } else { + pos = strstr(line, "Mosflm version "); + if ( pos != NULL ) { + char *vers = pos+15; + ok = 1; + chop_word(vers); + /* FIXME: Set capabilities based on version */ + } } } diff --git a/libcrystfel/src/xds.c b/libcrystfel/src/xds.c index bd98aec4..bfb977ea 100644 --- a/libcrystfel/src/xds.c +++ b/libcrystfel/src/xds.c @@ -679,10 +679,13 @@ const char *xds_probe(UnitCell *cell) for ( l=0; l<10; l++ ) { char *pos; - fgets(line, 1024, fh); - pos = strstr(line, "** XDS **"); - if ( pos != NULL ) { - ok = 1; + if ( fgets(line, 1024, fh) == NULL ) { + ERROR("Failed to probe for XDS\n"); + } else { + pos = strstr(line, "** XDS **"); + if ( pos != NULL ) { + ok = 1; + } } } diff --git a/src/ambigator.c b/src/ambigator.c index f7837721..3df66f3b 100644 --- a/src/ambigator.c +++ b/src/ambigator.c @@ -1254,8 +1254,11 @@ int main(int argc, char *argv[]) return 1; } - fread(&seed, sizeof(seed), 1, fh); - gsl_rng_set(rng, seed); + if ( fread(&seed, sizeof(seed), 1, fh) == 1 ) { + gsl_rng_set(rng, seed); + } else { + ERROR("Failed to seed RNG\n"); + } fclose(fh); } diff --git a/src/partial_sim.c b/src/partial_sim.c index 0482cc91..2f0a0277 100644 --- a/src/partial_sim.c +++ b/src/partial_sim.c @@ -917,10 +917,13 @@ int main(int argc, char *argv[]) for ( i=0; i