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 #7047 Safely handle setlocale #8735

Merged
merged 2 commits into from Jul 8, 2021

Conversation

florin-crisan
Copy link
Contributor

setlocale returns a pointer to a buffer containing the current locale name.
This needs to be copied into a std::string or it will be overwritten by the next call.

Trying to call setlocale with a non-null, invalid pointer can have unpredictable results, such as #7074

[ RUN      ] StringPrintfTest.Multibyte
minkernel\crts\ucrt\src\appcrt\convert\mbstowcs.cpp(246) : Assertion failed: (pwcs == nullptr && sizeInWords == 0) || (pwcs != nullptr && sizeInWords > 0)

setlocale can also return a nullptr if it fails, but we assert against that.

@acozzette
Copy link
Member

@florin-crisan It looks like your pull request updates the googletest submodule and that is causing some tests to fail. Would you mind updating the PR to avoid changing googletest?

`setlocale` returns a pointer to a buffer containing the current locale name.
This needs to be copied into a `std::string` or it will be overwritten by the next call.

Trying to call `setlocale` with a non-null, invalid pointer can have unpredictable results, such as
```
[ RUN      ] StringPrintfTest.Multibyte
minkernel\crts\ucrt\src\appcrt\convert\mbstowcs.cpp(246) : Assertion failed: (pwcs == nullptr && sizeInWords == 0) || (pwcs != nullptr && sizeInWords > 0)
```

`setlocale` can also return a `nullptr` if it fails, but we assert against that.
Prefer safer alternative to naked pointers.

This is a follow-up to 1dd313c
@florin-crisan
Copy link
Contributor Author

@florin-crisan It looks like your pull request updates the googletest submodule and that is causing some tests to fail. Would you mind updating the PR to avoid changing googletest?

As explained on ee0e153, the whole reason for the update was that I couldn't compile with Visual C++ 2019.

But I will try it your way and let somebody else deal with the update, if it's that tricky.

@acozzette
Copy link
Member

Thanks, @florin-crisan! I think it would be OK to update googletest in another pull request but we would just need to make sure all the tests still pass.

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

Successfully merging this pull request may close these issues.

None yet

3 participants