Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rare corner case in ntspawn.c #1284

Merged
merged 2 commits into from
Sep 11, 2024
Merged

Fix rare corner case in ntspawn.c #1284

merged 2 commits into from
Sep 11, 2024

Conversation

jeromew
Copy link
Contributor

@jeromew jeromew commented Sep 8, 2024

There could be cases when libc/proc/describefds.c calls calloc(handlecount, ..) with handlecount = 0
to initialize what will later become opt_lpExplicitHandleList. But calloc(0, ..) does not always return null
according to https://learn.microsoft.com/ka-ge/cpp/c-runtime-library/reference/calloc?view=msvc-140

In the Microsoft implementation, if number or size is zero, calloc returns a pointer to an allocated block
of non-zero size. An attempt to read or write through the returned pointer leads to undefined behavior.

If the chosen conditional is a->opt_lpExplicitHandleList then UpdateProcThreadAttribute is called and
produces a Windows error. The modification avoids this problem.

There could be cases when libc/proc/describefds.c calls calloc(handlecount, ..) with handlecount = 0 to initialize 
what will later become opt_lpExplicitHandleList.
but calloc(0, ..) does not always return null so if the chosen conditional is a->opt_lpExplicitHandleList then UpdateProcThreadAttribute produces a Windows error.
The modification avoids this problem.
@github-actions github-actions bot added the libc label Sep 8, 2024
Copy link
Owner

@jart jart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good eye. LGTM. Please confirm the edit I made to your PR is correct, then I'll merge.

@jeromew
Copy link
Contributor Author

jeromew commented Sep 9, 2024

LGTM. I should have spotted this. Thanks for the review

@jart jart merged commit 51c0f44 into jart:master Sep 11, 2024
6 checks passed
@jart
Copy link
Owner

jart commented Sep 11, 2024

Commit message edited to follow project style.

  • first line should be 50 characters or fewer
  • paragraphs must be justified to exactly 72 characters without inserting extra spaces

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants