Skip to content

Commit bec44c8

Browse files
committed
Do not depend on the strings being NULL terminated
Fixes #28 Instead make them NULL terminated by reading one longer into buffer
1 parent 5d0b7bd commit bec44c8

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/file.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,12 @@ herr_t det_visit_callback(hid_t root_id, const char *name,
631631
if (mt_id < 0) {
632632
ERROR_JUMP(-1, free_buffer, "Error creating HDF5 String datatype");
633633
}
634-
if (H5Tset_size(mt_id, str_size == -1 ? H5T_VARIABLE : str_size) < 0) {
634+
// set the target string type to be one longer than the recorded string
635+
// in keeping with the malloc'd buffer - if this is already a null
636+
// terminated string then we will just have two nulls, if it is not
637+
// then we won't clobber the last char in the buffer with a null in the
638+
// H5Aread call
639+
if (H5Tset_size(mt_id, str_size == -1 ? H5T_VARIABLE : str_size + 1) < 0) {
635640
char message[64];
636641
sprintf(message, "Error setting string datatype to size %d", str_size);
637642
ERROR_JUMP(-1, close_mtype, message);
@@ -650,6 +655,7 @@ herr_t det_visit_callback(hid_t root_id, const char *name,
650655
* terminated and extraneous bytes where being read by strcmp - set the end
651656
* byte to null
652657
*/
658+
653659
if (str_size > 0)
654660
((char *)buffer)[str_size] = '\0';
655661
/* test for NXdata or NXdetector */
@@ -805,7 +811,7 @@ int create_dataset_descriptor(struct ds_desc_t **desc,
805811

806812
/* determine the pixel information location */
807813
if (H5Lexists(g_id, "x_pixel_size", H5P_DEFAULT) > 0 &&
808-
H5Lexists(g_id, "y_pixel_size", H5P_DEFAULT)) {
814+
H5Lexists(g_id, "y_pixel_size", H5P_DEFAULT) > 0) {
809815
pxl_func = &get_nxs_pixel_info;
810816
} else if (H5Lexists(g_id, "detectorSpecific", H5P_DEFAULT) > 0 &&
811817
H5Lexists(g_id, "detectorSpecific/x_pixel_size", H5P_DEFAULT) >

0 commit comments

Comments
 (0)