Skip to content

Commit a545921

Browse files
committed
HDF5: Fix Segfault with libSplash Files (openPMD#962)
* HDF5: Fix Segfault with libSplash Files Fix a segfault (on some systems) with legacy libSplash HDF5 files. From the docs: ``` Name: H5Tget_member_name ... The HDF5 Library allocates a buffer to receive the name of the field. The caller must subsequently free the buffer with H5free_memory. ``` Refs.: - https://support.hdfgroup.org/HDF5/doc/RM/RM_H5T.html#Datatype-GetMemberName - https://support.hdfgroup.org/HDF5/doc/RM/RM_H5.html#Library-FreeMemory * avoid undefined behavior in str(n)cmp Passing potential `nullptr`s is undefined behavior. * Clang-Tidy: Intentional Branch Clone
1 parent ff862b5 commit a545921

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/IO/HDF5/HDF5IOHandler.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,8 +1414,9 @@ HDF5IOHandlerImpl::readAttribute(Writable* writable,
14141414
{
14151415
char* m0 = H5Tget_member_name(attr_type, 0);
14161416
char* m1 = H5Tget_member_name(attr_type, 1);
1417-
if( (strncmp("TRUE" , m0, 4) == 0) && (strncmp("FALSE", m1, 5) == 0) )
1418-
attrIsBool = true;
1417+
if( m0 != nullptr && m1 != nullptr )
1418+
if( (strncmp("TRUE" , m0, 4) == 0) && (strncmp("FALSE", m1, 5) == 0) )
1419+
attrIsBool = true;
14191420
H5free_memory(m1);
14201421
H5free_memory(m0);
14211422
}
@@ -1436,8 +1437,9 @@ HDF5IOHandlerImpl::readAttribute(Writable* writable,
14361437
{
14371438
char* m0 = H5Tget_member_name(attr_type, 0);
14381439
char* m1 = H5Tget_member_name(attr_type, 1);
1439-
if( (strncmp("r" , m0, 4) == 0) && (strncmp("i", m1, 5) == 0) )
1440-
isComplexType = true;
1440+
if( m0 != nullptr && m1 != nullptr )
1441+
if( (strncmp("r" , m0, 1) == 0) && (strncmp("i", m1, 1) == 0) )
1442+
isComplexType = true;
14411443
H5free_memory(m1);
14421444
H5free_memory(m0);
14431445
}
@@ -1453,11 +1455,13 @@ HDF5IOHandlerImpl::readAttribute(Writable* writable,
14531455
char* m0 = H5Tget_member_name(attr_type, 0);
14541456
char* m1 = H5Tget_member_name(attr_type, 1);
14551457
char* m2 = H5Tget_member_name(attr_type, 2);
1456-
if(strcmp("x", m0) != 0 || strcmp("y", m1) != 0 || strcmp("z", m2) != 0)
1458+
if( m0 == nullptr || m1 == nullptr || m2 == nullptr )
1459+
isLegacyLibSplashAttr = false; // NOLINT(bugprone-branch-clone)
1460+
else if(strcmp("x", m0) != 0 || strcmp("y", m1) != 0 || strcmp("z", m2) != 0)
14571461
isLegacyLibSplashAttr = false;
1458-
free(m2);
1459-
free(m1);
1460-
free(m0);
1462+
H5free_memory(m2);
1463+
H5free_memory(m1);
1464+
H5free_memory(m0);
14611465
}
14621466
if( isLegacyLibSplashAttr )
14631467
{
@@ -1488,7 +1492,7 @@ HDF5IOHandlerImpl::readAttribute(Writable* writable,
14881492
a = Attribute(cld);
14891493
}
14901494
else
1491-
throw unsupported_data_error("[HDF5] Unknow complex type representation");
1495+
throw unsupported_data_error("[HDF5] Unknown complex type representation");
14921496
}
14931497
else
14941498
throw unsupported_data_error("[HDF5] Compound attribute type not supported");

0 commit comments

Comments
 (0)