Skip to content

Commit f5dc8e2

Browse files
authored
HDF5: Fix Segfault with libSplash Files (#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 6fd3936 commit f5dc8e2

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
@@ -1424,8 +1424,9 @@ HDF5IOHandlerImpl::readAttribute(Writable* writable,
14241424
{
14251425
char* m0 = H5Tget_member_name(attr_type, 0);
14261426
char* m1 = H5Tget_member_name(attr_type, 1);
1427-
if( (strncmp("TRUE" , m0, 4) == 0) && (strncmp("FALSE", m1, 5) == 0) )
1428-
attrIsBool = true;
1427+
if( m0 != nullptr && m1 != nullptr )
1428+
if( (strncmp("TRUE" , m0, 4) == 0) && (strncmp("FALSE", m1, 5) == 0) )
1429+
attrIsBool = true;
14291430
H5free_memory(m1);
14301431
H5free_memory(m0);
14311432
}
@@ -1446,8 +1447,9 @@ HDF5IOHandlerImpl::readAttribute(Writable* writable,
14461447
{
14471448
char* m0 = H5Tget_member_name(attr_type, 0);
14481449
char* m1 = H5Tget_member_name(attr_type, 1);
1449-
if( (strncmp("r" , m0, 4) == 0) && (strncmp("i", m1, 5) == 0) )
1450-
isComplexType = true;
1450+
if( m0 != nullptr && m1 != nullptr )
1451+
if( (strncmp("r" , m0, 1) == 0) && (strncmp("i", m1, 1) == 0) )
1452+
isComplexType = true;
14511453
H5free_memory(m1);
14521454
H5free_memory(m0);
14531455
}
@@ -1463,11 +1465,13 @@ HDF5IOHandlerImpl::readAttribute(Writable* writable,
14631465
char* m0 = H5Tget_member_name(attr_type, 0);
14641466
char* m1 = H5Tget_member_name(attr_type, 1);
14651467
char* m2 = H5Tget_member_name(attr_type, 2);
1466-
if(strcmp("x", m0) != 0 || strcmp("y", m1) != 0 || strcmp("z", m2) != 0)
1468+
if( m0 == nullptr || m1 == nullptr || m2 == nullptr )
1469+
isLegacyLibSplashAttr = false; // NOLINT(bugprone-branch-clone)
1470+
else if(strcmp("x", m0) != 0 || strcmp("y", m1) != 0 || strcmp("z", m2) != 0)
14671471
isLegacyLibSplashAttr = false;
1468-
free(m2);
1469-
free(m1);
1470-
free(m0);
1472+
H5free_memory(m2);
1473+
H5free_memory(m1);
1474+
H5free_memory(m0);
14711475
}
14721476
if( isLegacyLibSplashAttr )
14731477
{
@@ -1498,7 +1502,7 @@ HDF5IOHandlerImpl::readAttribute(Writable* writable,
14981502
a = Attribute(cld);
14991503
}
15001504
else
1501-
throw unsupported_data_error("[HDF5] Unknow complex type representation");
1505+
throw unsupported_data_error("[HDF5] Unknown complex type representation");
15021506
}
15031507
else
15041508
throw unsupported_data_error("[HDF5] Compound attribute type not supported");

0 commit comments

Comments
 (0)