Repository navigation
Validate the thread count and handle allocation failures in the thread pool - #15
Open
MinhyukHong wants to merge 3 commits into
Open
MinhyukHong wants to merge 3 commits into
MinhyukHong wants to merge 3 commits into
Conversation
createThreadPool() only checks that the argument is a number. The value goes straight into malloc(numThreads * sizeof(THREAD)), so a large value asks for tens of gigabytes and a mid-sized one spawns that many real threads. Reject 0 and anything above a sane upper bound.
…rencing NULL None of the four allocations in CreateThreadPool are checked. On failure the pool struct is memset through a null pointer, or the thread id array is written to in the loop. Return NULL when an allocation fails, stop the loop at the number of threads actually created so DestroyThreadPool joins only those, and report the failure from createThreadPool().
…rencing NULL None of the four allocations in CreateThreadPool are checked. On failure the pool struct is memset through a null pointer, or the thread id array is written to in the loop. Return NULL when an allocation fails, stop the loop at the number of threads actually created so DestroyThreadPool joins only those, and report the failure from createThreadPool().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
createThreadPool(n)only checks thatnis a number. The value is used directly asmalloc(n * sizeof(THREAD)), socreateThreadPool(4294967295)asks for about 34 GB; the allocation fails, the result is not checked, and the loop writes to a null pointer. A mid-sized value such as 100000 allocates successfully and then spawns that many threads.This rejects 0 and anything above 1024, and checks the four allocations in
CreateThreadPoolso a failure returns NULL rather than being dereferenced.numThreadsis set to the number of threads actually created, soDestroyThreadPooldoes not join ones that were never started.1024 is a guess at a reasonable ceiling, feel free to change it to whatever the maintainer
prefers.