Repository navigation
Fix memory leak in pthread init functions on failure - #4486
Conversation
When pthread_mutex_init() or pthread_cond_init() fails in the debug implementation (DEBUGLEVEL >= 1), the previously allocated memory was not freed, causing a memory leak. This fix ensures that allocated memory is properly freed when pthread initialization functions fail, preventing resource leaks in error conditions. The issue affects: - ZSTD_pthread_mutex_init() at lib/common/threading.c:146 - ZSTD_pthread_cond_init() at lib/common/threading.c:167 This is particularly important for long-running applications or scenarios with resource constraints where pthread initialization might fail due to system limits.
|
Hi @rlefko! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
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.