aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2017-10-23 15:53:19 +0200
committerThomas White <taw@physics.org>2017-10-23 16:01:37 +0200
commit45038c42847476ce1aee5b09c47fb330a0108cd6 (patch)
tree3979667c80cacc3bd34d0f59fc905a0d4c0b3d0d
parent7b6fa15e761ec0c5ae9010dc21786a1a3dda7076 (diff)
Check return values from fread/fgets
-rw-r--r--libcrystfel/src/dirax.c9
-rw-r--r--libcrystfel/src/felix.c9
-rw-r--r--libcrystfel/src/mosflm.c16
-rw-r--r--libcrystfel/src/xds.c11
-rw-r--r--src/ambigator.c7
-rw-r--r--src/partial_sim.c9
-rw-r--r--src/partialator.c5
-rw-r--r--src/pattern_sim.c7
-rw-r--r--tests/integration_check.c7
-rw-r--r--tests/prof2d_check.c7
-rw-r--r--tests/ring_check.c6
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<n_threads; i++ ) {
unsigned long int seed;
-
- fread(&seed, sizeof(seed), 1, fh);
qargs.rngs[i] = gsl_rng_alloc(gsl_rng_mt19937);
- gsl_rng_set(qargs.rngs[i], seed);
+
+ if ( fread(&seed, sizeof(seed), 1, fh) == 1 ) {
+ gsl_rng_set(qargs.rngs[i], seed);
+ } else {
+ ERROR("Failed to seed RNG %i\n", i);
+ }
}
diff --git a/src/partialator.c b/src/partialator.c
index 569145e8..142ca892 100644
--- a/src/partialator.c
+++ b/src/partialator.c
@@ -1018,7 +1018,10 @@ int main(int argc, char *argv[])
ERROR("Failed to open '%s'\n", sparams_fn);
return 1;
}
- fgets(line, 1024, sparams_fh);
+ if ( fgets(line, 1024, sparams_fh) == NULL ) {
+ ERROR("Failed to read header from %s\n", sparams_fn);
+ return 1;
+ }
STATUS("Reading initial scaling factors (G,B) from '%s'\n",
sparams_fn);
free(sparams_fn);
diff --git a/src/pattern_sim.c b/src/pattern_sim.c
index fd670c48..a3fea8e3 100644
--- a/src/pattern_sim.c
+++ b/src/pattern_sim.c
@@ -799,9 +799,12 @@ int main(int argc, char *argv[])
FILE *fh;
unsigned long int seed;
fh = fopen("/dev/urandom", "r");
- fread(&seed, sizeof(seed), 1, fh);
+ if ( fread(&seed, sizeof(seed), 1, fh) == 1 ) {
+ gsl_rng_set(rng, seed);
+ } else {
+ ERROR("Failed to seed random number generator\n");
+ }
fclose(fh);
- gsl_rng_set(rng, seed);
}
powder.det = image.det;
diff --git a/tests/integration_check.c b/tests/integration_check.c
index 0713d837..65241612 100644
--- a/tests/integration_check.c
+++ b/tests/integration_check.c
@@ -65,9 +65,12 @@ int main(int argc, char *argv[])
rng = gsl_rng_alloc(gsl_rng_mt19937);
fh = fopen("/dev/urandom", "r");
- fread(&seed, sizeof(seed), 1, fh);
+ if ( fread(&seed, sizeof(seed), 1, fh) == 1 ) {
+ gsl_rng_set(rng, seed);
+ } else {
+ ERROR("Failed to seed RNG\n");
+ }
fclose(fh);
- gsl_rng_set(rng, seed);
image.beam = NULL;
image.lambda = ph_eV_to_lambda(9000.0);
diff --git a/tests/prof2d_check.c b/tests/prof2d_check.c
index 8081748c..9fe8b356 100644
--- a/tests/prof2d_check.c
+++ b/tests/prof2d_check.c
@@ -73,9 +73,12 @@ int main(int argc, char *argv[])
rng = gsl_rng_alloc(gsl_rng_mt19937);
fh = fopen("/dev/urandom", "r");
- fread(&seed, sizeof(seed), 1, fh);
+ if ( fread(&seed, sizeof(seed), 1, fh) == 1 ) {
+ gsl_rng_set(rng, seed);
+ } else {
+ ERROR("Failed to seed RNG\n");
+ }
fclose(fh);
- gsl_rng_set(rng, seed);
image.beam = NULL;
image.lambda = ph_eV_to_lambda(9000.0);
diff --git a/tests/ring_check.c b/tests/ring_check.c
index cc8a1c8e..0997447f 100644
--- a/tests/ring_check.c
+++ b/tests/ring_check.c
@@ -176,7 +176,11 @@ int main(int argc, char *argv[])
rng = gsl_rng_alloc(gsl_rng_mt19937);
fh = fopen("/dev/urandom", "r");
- fread(&seed, sizeof(seed), 1, fh);
+ if ( fread(&seed, sizeof(seed), 1, fh) == 1 ) {
+ gsl_rng_set(rng, seed);
+ } else {
+ ERROR("Failed to seed RNG\n");
+ }
fclose(fh);
gsl_rng_set(rng, seed);