2 Commits

Author SHA1 Message Date
CV-GPhL 0e258cc8c9 Null terminate buffer in file.c
Ensure the buffer is null terminated after allocation - otherwise correct results from strcmp below is undefined.
2026-02-04 14:52:02 +01:00
Graeme Winter bec44c8691 Do not depend on the strings being NULL terminated
Fixes #28

Instead make them NULL terminated by reading one longer into buffer
2023-08-09 12:47:45 +01:00
+10 -8
View File
@@ -631,7 +631,12 @@ herr_t det_visit_callback(hid_t root_id, const char *name,
if (mt_id < 0) {
ERROR_JUMP(-1, free_buffer, "Error creating HDF5 String datatype");
}
if (H5Tset_size(mt_id, str_size == -1 ? H5T_VARIABLE : str_size) < 0) {
// set the target string type to be one longer than the recorded string
// in keeping with the malloc'd buffer - if this is already a null
// terminated string then we will just have two nulls, if it is not
// then we won't clobber the last char in the buffer with a null in the
// H5Aread call
if (H5Tset_size(mt_id, str_size == -1 ? H5T_VARIABLE : str_size + 1) < 0) {
char message[64];
sprintf(message, "Error setting string datatype to size %d", str_size);
ERROR_JUMP(-1, close_mtype, message);
@@ -646,12 +651,9 @@ herr_t det_visit_callback(hid_t root_id, const char *name,
ERROR_JUMP(-1, close_mtype, message);
}
/* at least one file has been seen where the NX_class attribute was not null
* terminated and extraneous bytes where being read by strcmp - set the end
* byte to null
*/
if (str_size > 0)
((char *)buffer)[str_size] = '\0';
/* ensure the buffer is null terminated */
buffer[H5Tget_size(t_id)] = '\0';
/* test for NXdata or NXdetector */
{
char *nxclass = str_size > 0 ? (char *)buffer : *((char **)buffer);
@@ -805,7 +807,7 @@ int create_dataset_descriptor(struct ds_desc_t **desc,
/* determine the pixel information location */
if (H5Lexists(g_id, "x_pixel_size", H5P_DEFAULT) > 0 &&
H5Lexists(g_id, "y_pixel_size", H5P_DEFAULT)) {
H5Lexists(g_id, "y_pixel_size", H5P_DEFAULT) > 0) {
pxl_func = &get_nxs_pixel_info;
} else if (H5Lexists(g_id, "detectorSpecific", H5P_DEFAULT) > 0 &&
H5Lexists(g_id, "detectorSpecific/x_pixel_size", H5P_DEFAULT) >