Skip to content

Add a helper function for avoiding certain race conditions in Win32 API calls #476

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

Merged
merged 1 commit into from
May 3, 2025

Conversation

jakepetroules
Copy link
Collaborator

For many Win32 APIs returning strings, the expected usage is to first call the API with a nil buffer and 0 capacity to receive the buffer count, then call it again with a buffer of the right size. However, some APIs refer to external state which can change between calls (such as GetEnvironmentVariableW and GetCurrentDirectoryW). This can lead to race conditions where the buffer doesn't end up being of sufficient size to hold the result, and the call fails. To protect against this, add a helper function with a reusable algorithm that continually doubles the buffer size until it's large enough to hold the result, up to a specified maximum to prevent denial of service attacks.

@jakepetroules jakepetroules added the windows Support for the Windows platform label May 1, 2025
@jakepetroules
Copy link
Collaborator Author

@swift-ci test

@jakepetroules jakepetroules force-pushed the eng/PR-win32-safe-alloc branch from 0beeb8e to 753c8ee Compare May 1, 2025 06:33
@jakepetroules
Copy link
Collaborator Author

@swift-ci test

@jakepetroules jakepetroules force-pushed the eng/PR-win32-safe-alloc branch from 753c8ee to 185d17a Compare May 1, 2025 07:21
@jakepetroules
Copy link
Collaborator Author

@swift-ci test

@jakepetroules jakepetroules force-pushed the eng/PR-win32-safe-alloc branch from 185d17a to 7c5afad Compare May 1, 2025 17:32
@jakepetroules
Copy link
Collaborator Author

@swift-ci test

…PI calls

For many Win32 APIs returning strings, the expected usage is to first call the API with a nil buffer and 0 capacity to receive the buffer count, then call it again with a buffer of the right size. However, some APIs refer to external state which can change between calls (such as GetEnvironmentVariableW and GetCurrentDirectoryW). This can lead to race conditions where the buffer doesn't end up being of sufficient size to hold the result, and the call fails. To protect against this, add a helper function with a reusable algorithm that continually doubles the buffer size until it's large enough to hold the result, up to a specified maximum to prevent denial of service attacks.
@jakepetroules jakepetroules force-pushed the eng/PR-win32-safe-alloc branch from 7c5afad to e674698 Compare May 2, 2025 19:06
@jakepetroules
Copy link
Collaborator Author

@swift-ci test

@jakepetroules
Copy link
Collaborator Author

@swift-ci test macos

@jakepetroules jakepetroules merged commit 3be34c2 into main May 3, 2025
22 checks passed
@jakepetroules jakepetroules deleted the eng/PR-win32-safe-alloc branch May 3, 2025 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
windows Support for the Windows platform
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants