aboutsummaryrefslogtreecommitdiff
path: root/libcrystfel
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2022-09-08 17:11:50 +0200
committerThomas White <taw@physics.org>2022-09-08 17:42:39 +0200
commit6d18e4ba3edd077aba9811d446d94f16141f57e9 (patch)
treef773d22109c01005f24fd48d9a008caa26b4a929 /libcrystfel
parente5b54070ad29b56ef4402c8d797e542169d00941 (diff)
Stream: don't open stream if headers aren't understood
This avoids e.g. crashing later if the geometry is bad. This commit also removes a problematic stanza from the geometry in test.stream, since the stream reading functions are now more fussy about this.
Diffstat (limited to 'libcrystfel')
-rw-r--r--libcrystfel/src/stream.c27
1 files changed, 17 insertions, 10 deletions
diff --git a/libcrystfel/src/stream.c b/libcrystfel/src/stream.c
index 708e681c..95345f40 100644
--- a/libcrystfel/src/stream.c
+++ b/libcrystfel/src/stream.c
@@ -937,7 +937,7 @@ char *stream_geometry_file(Stream *st)
}
-static void read_geometry_file(Stream *st)
+static int read_geometry_file(Stream *st)
{
int done = 0;
size_t len = 0;
@@ -947,7 +947,7 @@ static void read_geometry_file(Stream *st)
geom = malloc(max_geom_len);
if ( geom == NULL ) {
ERROR("Failed to allocate memory for geometry file\n");
- return;
+ return 1;
}
geom[0] = '\0';
@@ -962,7 +962,7 @@ static void read_geometry_file(Stream *st)
ERROR("Failed to read stream geometry file.\n");
stream_close(st);
free(geom);
- return;
+ return 1;
}
if ( strcmp(line, STREAM_GEOM_END_MARKER"\n") == 0 ) {
@@ -975,7 +975,7 @@ static void read_geometry_file(Stream *st)
ERROR("Stream's geometry file is too long (%li > %i).\n",
(long)len, (int)max_geom_len);
free(geom);
- return;
+ return 1;
} else {
strcat(geom, line);
}
@@ -984,10 +984,11 @@ static void read_geometry_file(Stream *st)
st->geometry_file = geom;
st->dtempl_read = data_template_new_from_string(geom);
+ return (st->dtempl_read == NULL);
}
-static void read_headers(Stream *st)
+static int read_headers(Stream *st)
{
int done = 0;
size_t len = 0;
@@ -995,7 +996,7 @@ static void read_headers(Stream *st)
st->audit_info = malloc(4096);
if ( st->audit_info == NULL ) {
ERROR("Failed to allocate memory for audit information\n");
- return;
+ return 1;
}
st->audit_info[0] = '\0';
@@ -1011,23 +1012,27 @@ static void read_headers(Stream *st)
if ( rval == NULL ) {
ERROR("Failed to read stream audit info.\n");
stream_close(st);
- return;
+ return 1;
}
if ( strcmp(line, STREAM_GEOM_START_MARKER"\n") == 0 ) {
- read_geometry_file(st);
+ if ( read_geometry_file(st) ) {
+ return 1;
+ }
done = 1;
} else {
len += strlen(line);
if ( len > 4090 ) {
ERROR("Too much audit information.\n");
- return;
+ return 1;
} else {
strcat(st->audit_info, line);
}
}
} while ( !done );
+
+ return 0;
}
@@ -1077,7 +1082,9 @@ Stream *stream_open_for_read(const char *filename)
st->ln = 1;
- read_headers(st);
+ if ( read_headers(st) ) {
+ return NULL;
+ }
return st;
}