8000 Validate the thread count and handle allocation failures in the thread pool by MinhyukHong · Pull Request #15 · inh3/nPool · GitHub
[go: up one dir, main page]

Skip to content

Validate the thread count and handle allocation failures in the thread pool - #15

Open
MinhyukHong wants to merge 3 commits into
inh3:masterfrom
MinhyukHong:fix/thread-pool-allocation
Open

MinhyukHong wants to merge 3 commits into
inh3:masterfrom
MinhyukHong:fix/thread-pool-allocation

Conversation

@MinhyukHong
@MinhyukHong MinhyukHong commented Oct 12, 2026 •
Copy link
Copy Markdown

createThreadPool(n) only checks that n is a number. The value is used directly as malloc(n * sizeof(THREAD)), so createThreadPool(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 CreateThreadPool so a failure returns NULL rather than being dereferenced. numThreads is set to the number of threads actually created, so DestroyThreadPool does 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.

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().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0