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

@florin-crisan florin-crisan commented Jun 15, 2021

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.

@google-cla google-cla bot added the cla: yes label Jun 15, 2021
@florin-crisan florin-crisan force-pushed the bugfix/7047 branch 3 times, most recently from 0fe68a4 to 8d1cc39 Compare Jun 15, 2021
@acozzette acozzette added c++ release notes: yes Include this PR description in the next release kokoro:run labels Jun 17, 2021
@acozzette
Copy link
Member

acozzette commented Jun 28, 2021

@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 commented Jul 5, 2021

@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 acozzette merged commit 0444e07 into protocolbuffers:master Jul 8, 2021
49 of 54 checks passed
@acozzette
Copy link
Member

acozzette commented Jul 8, 2021

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
Labels
c++ cla: yes release notes: yes Include this PR description in the next release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants