Skip to content

Use snprintf instead of _snprintf on Windows #373

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
Jun 29, 2018
Merged

Use snprintf instead of _snprintf on Windows #373

merged 1 commit into from
Jun 29, 2018

Conversation

adierking
Copy link
Contributor

_snprintf does not guarantee null termination if the size argument is too small.
Starting with VS2015, the UCRT provides a C99-compliant snprintf. Use it, and
also fix a related issue where _dispatch_client_assert_fail was passing a
too-small size.

_snprintf does not guarantee null termination if the size argument is too small.
Starting with VS2015, the UCRT provides a C99-compliant snprintf. Use it, and
also fix a related issue where _dispatch_client_assert_fail was passing a
too-small size.
@adierking
Copy link
Contributor Author

cc @MadCoder @compnerd

@@ -928,7 +928,7 @@ _dispatch_ktrace_impl(uint32_t code, uint64_t a, uint64_t b,
dispatch_assert(_length != -1); \
_msg = (char *)malloc((unsigned)_length + 1); \
dispatch_assert(_msg); \
_snprintf(_msg, (unsigned)_length, "%s" fmt, DISPATCH_ASSERTION_FAILED_MESSAGE, ##__VA_ARGS__); \
snprintf(_msg, (unsigned)_length + 1, "%s" fmt, DISPATCH_ASSERTION_FAILED_MESSAGE, ##__VA_ARGS__); \
Copy link
Member

Choose a reason for hiding this comment

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

Bleh; too bad that we still have the _scprintf here. Can we use snprintf with a 0 size instead to get the desired length? Then we could just use this version globally rather than having a windows only path. The difference between the asprintf version and this would be the extra calls, but it is on the abort path anyways, so I don't think that it matters too much.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I agree this is kinda nasty - is it fine if I make that change as a follow-up?

asprintf is also used in several places in test code :(

@MadCoder
Copy link
Contributor

@swift-ci please test

@adierking
Copy link
Contributor Author

ping @MadCoder

@MadCoder MadCoder merged commit e764f34 into swiftlang:master Jun 29, 2018
@adierking adierking deleted the snprintf branch December 2, 2018 00:33
ktopley-apple pushed a commit that referenced this pull request Dec 6, 2018
Use snprintf instead of _snprintf on Windows

Signed-off-by: Kim Topley <ktopley@apple.com>
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.

4 participants