Fix memory leak in pthread init functions on failure #4486
Merged
+16
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a memory leak in the debug implementation of pthread initialization functions when pthread_mutex_init() or pthread_cond_init() fails.
Problem
In the debug implementation (used when
DEBUGLEVEL >= 1), theZSTD_pthread_mutex_init()andZSTD_pthread_cond_init()functions allocate memory usingZSTD_malloc()but fail to free it if the subsequent pthread initialization calls fail. This leads to memory leaks in error conditions.The affected functions are:
ZSTD_pthread_mutex_init()at lib/common/threading.c:146ZSTD_pthread_cond_init()at lib/common/threading.c:167Solution
The fix adds proper error handling to free the allocated memory when pthread initialization fails:
Impact
This fix is particularly important for:
Testing
make checkcompleted successfully)make poolTests && ./poolTestspassed)Changes
lib/common/threading.cto add proper cleanup on pthread initialization failureThis is a defensive programming improvement that ensures no resources are leaked even in uncommon error scenarios.