Skip to content

Loop on Win32 to satisfy large random byte requests (as is done on Linux) #62364

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 2 commits into from
Dec 5, 2022

Conversation

grynspan
Copy link
Contributor

@grynspan grynspan commented Dec 2, 2022

This change resolves a crash on Windows when using SystemRandomNumberGenerator and requesting a very large number (> ULONG_MAX) of random bytes.

In practice, it's unlikely anyone will be requesting more than 4GB of random bytes, but we don't abort on other platforms and nothing about the API suggests that a large buffer size is unsupported or disallowed.

I have not written a unit test because it would be impractical to request such a large amount of random data during a test run in CI.

Resolves #62130.

@grynspan
Copy link
Contributor Author

grynspan commented Dec 2, 2022

@swift-ci please test

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this!

fatalError(0, "Fatal error: %zd exceeds ULONG_MAX\n", nbytes);
}
while (nbytes > 0) {
auto actual_nbytes = std::min(nbytes, (__swift_size_t)ULONG_MAX);
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to use static_cast as this is C++.

Suggested change
auto actual_nbytes = std::min(nbytes, (__swift_size_t)ULONG_MAX);
auto actual_nbytes = std::min(nbytes, static_cast<__swift_size_t>(ULONG_MAX));

Copy link
Contributor

Choose a reason for hiding this comment

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

@compnerd Can the #warning on line 64 be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question. AFAIK the unit tests for random number generation are not conditionalized by platform, are they?

Copy link
Contributor

Choose a reason for hiding this comment

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

swift_stdlib_random is used in validation-test/stdlib/Random.swift, on all platforms AFAIK.

The #warning was originally an #error, because swift_stdlib_random was written without access to a compiler for Windows.

@grynspan grynspan force-pushed the jgrynspan/win32-random-large-size branch from 132cd3e to 9de9205 Compare December 5, 2022 15:09
@grynspan
Copy link
Contributor Author

grynspan commented Dec 5, 2022

@swift-ci please test

@grynspan grynspan merged commit 0368538 into swiftlang:main Dec 5, 2022
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.

swift_stdlib_random() crashes on Windows for large buffer sizes
5 participants