aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2015-10-07 12:03:51 +0200
committerThomas White <taw@physics.org>2015-10-07 12:07:33 +0200
commitfaef0b78ac34d0ea2cdc98f7137b504aac94e9d5 (patch)
treede9b425076937fce67d99754fb73dd7bd3a9aa35
parentdeac92ded829656583760b80308f00d1ee8bfa53 (diff)
hdfile_get_string_value(): Rearrange control flow
This is not a nice function, and seemed to contain a weird stack corruption somewhere. It is still not a nice function, but at least no longer seems to contain a weird stack corruption.
-rw-r--r--libcrystfel/src/hdf5-file.c52
1 files changed, 41 insertions, 11 deletions
diff --git a/libcrystfel/src/hdf5-file.c b/libcrystfel/src/hdf5-file.c
index 720f6e0f..e7b6eed4 100644
--- a/libcrystfel/src/hdf5-file.c
+++ b/libcrystfel/src/hdf5-file.c
@@ -1926,7 +1926,10 @@ char *hdfile_get_string_value(struct hdfile *f, const char *name,
}
dh = H5Dopen2(f->fh, subst_name, H5P_DEFAULT);
- if ( dh < 0 ) return NULL;
+ if ( dh < 0 ) {
+ free(subst_name);
+ return NULL;
+ }
type = H5Dget_type(dh);
class = H5Tget_class(type);
@@ -1938,6 +1941,8 @@ char *hdfile_get_string_value(struct hdfile *f, const char *name,
v = H5Tis_variable_str(type);
if ( v < 0 ) {
+ H5Tclose(type);
+ free(subst_name);
return "WTF?";
}
@@ -1946,9 +1951,13 @@ char *hdfile_get_string_value(struct hdfile *f, const char *name,
r = H5Dread(dh, type, H5S_ALL, H5S_ALL,
H5P_DEFAULT, &tmp);
if ( r < 0 ) {
- tmp = NULL;
+ H5Tclose(type);
+ free(subst_name);
+ return NULL;
}
+ return tmp;
+
} else {
size = H5Tget_size(type);
@@ -1957,9 +1966,11 @@ char *hdfile_get_string_value(struct hdfile *f, const char *name,
sh = H5Screate(H5S_SCALAR);
r = H5Dread(dh, type, sh, sh, H5P_DEFAULT, tmp);
+ H5Sclose(sh);
if ( r < 0 ) {
free(tmp);
- tmp = NULL;
+ free(subst_name);
+ return NULL;
} else {
/* Two possibilities:
@@ -1968,17 +1979,20 @@ char *hdfile_get_string_value(struct hdfile *f, const char *name,
* Make sure things are done properly... */
tmp[size] = '\0';
chomp(tmp);
+ H5Dclose(dh);
+ free(subst_name);
+ return tmp;
}
- H5Sclose(sh);
-
}
-
} else {
int r;
+ H5Dclose(dh);
+ H5Tclose(type);
+
switch ( class ) {
case H5T_FLOAT :
@@ -1986,7 +2000,15 @@ char *hdfile_get_string_value(struct hdfile *f, const char *name,
H5T_NATIVE_DOUBLE);
if ( r == 0 ) {
tmp = malloc(256);
+ if ( tmp == NULL ) {
+ ERROR("Failed to allocate float\n");
+ return NULL;
+ }
snprintf(tmp, 255, "%f", buf_f);
+ return tmp;
+ } else {
+ ERROR("Failed to read value\n");
+ return NULL;
}
break;
@@ -1995,17 +2017,25 @@ char *hdfile_get_string_value(struct hdfile *f, const char *name,
H5T_NATIVE_INT);
if ( r == 0 ) {
tmp = malloc(256);
+ if ( tmp == NULL ) {
+ ERROR("Failed to allocate int buf!\n");
+ return NULL;
+ }
snprintf(tmp, 255, "%d", buf_i);
+ return tmp;
+
+ } else {
+ ERROR("Failed to read value\n");
+ return NULL;
}
break;
+
+ default :
+ ERROR("Don't know what to do!\n");
+ return NULL;
}
}
-
- H5Tclose(type);
- H5Dclose(dh);
- free(subst_name);
- return tmp;
}