Skip to content

Commit c56ca00

Browse files
committed
Add error checking to TS functions
1 parent 337bdcf commit c56ca00

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

src/H5system.c

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,15 +1475,27 @@ typedef struct HDqsort_fallback_context_t {
14751475
} HDqsort_fallback_context_t;
14761476

14771477
#ifdef H5_HAVE_THREADSAFE
1478-
/* Thread-local storage approach for thread-safe builds */
1478+
/* Thread-local storage approach for thread-safe builds
1479+
*
1480+
* Note: The functions below use assertions instead of the HDF5 error stack because
1481+
* qsort and its variants return void, preventing propagation of errors.
1482+
*/
14791483
static H5TS_key_t HDqsort_fallback_key;
14801484
static H5TS_once_t HDqsort_fallback_key_once = H5TS_ONCE_INITIALIZER;
14811485

14821486
static void
14831487
HDqsort_fallback_key_init(void)
14841488
{
1489+
herr_t ret = SUCCEED;
1490+
14851491
/* Create the thread-local storage key (no destructor needed) */
1486-
H5TS_key_create(&HDqsort_fallback_key, NULL);
1492+
ret = H5TS_key_create(&HDqsort_fallback_key, NULL);
1493+
1494+
/* Assert that initialization succeeded - cannot propagate errors from here */
1495+
if (H5_UNLIKELY(ret < 0)) {
1496+
assert(false && "Failed to create TLS key for qsort fallback");
1497+
(void)0; /* Ensure non-empty body even when asserts are disabled */
1498+
}
14871499
}
14881500

14891501
static int
@@ -1505,20 +1517,33 @@ HDqsort_fallback(void *base, size_t nel, size_t size, int (*compar)(const void *
15051517
void *arg)
15061518
{
15071519
HDqsort_fallback_context_t ctx;
1520+
herr_t ret;
15081521

15091522
/* Ensure the TLS key is initialized */
1510-
H5TS_once(&HDqsort_fallback_key_once, HDqsort_fallback_key_init);
1523+
ret = H5TS_once(&HDqsort_fallback_key_once, HDqsort_fallback_key_init);
1524+
if (H5_UNLIKELY(ret < 0)) {
1525+
assert(false && "Failed to initialize TLS key for qsort fallback");
1526+
(void)0; /* Ensure non-empty body even when asserts are disabled */
1527+
}
15111528

15121529
ctx.gnu_compar = compar;
15131530
ctx.gnu_arg = arg;
15141531

15151532
/* Store context in thread-local storage */
1516-
H5TS_key_set_value(HDqsort_fallback_key, &ctx);
1533+
ret = H5TS_key_set_value(HDqsort_fallback_key, &ctx);
1534+
if (H5_UNLIKELY(ret < 0)) {
1535+
assert(false && "Failed to set TLS value for qsort fallback");
1536+
(void)0; /* Ensure non-empty body even when asserts are disabled */
1537+
}
15171538

15181539
qsort(base, nel, size, HDqsort_fallback_wrapper);
15191540

15201541
/* Clear the thread-local storage */
1521-
H5TS_key_set_value(HDqsort_fallback_key, NULL);
1542+
ret = H5TS_key_set_value(HDqsort_fallback_key, NULL);
1543+
if (H5_UNLIKELY(ret < 0)) {
1544+
assert(false && "Failed to clear TLS value for qsort fallback");
1545+
(void)0; /* Ensure non-empty body even when asserts are disabled */
1546+
}
15221547
}
15231548

15241549
#else

0 commit comments

Comments
 (0)