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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/google/protobuf/stubs/stringprintf_unittest.cc
Expand Up @@ -32,6 +32,7 @@

#include <google/protobuf/stubs/stringprintf.h>

#include <array>
#include <cerrno>
#include <string>

Expand Down Expand Up @@ -91,7 +92,9 @@ TEST(StringPrintfTest, Multibyte) {
// out of memory while trying to determine destination buffer size.
// see b/4194543.

char* old_locale = setlocale(LC_CTYPE, nullptr);
char* old_locale_c = setlocale(LC_CTYPE, nullptr);
ASSERT_TRUE(old_locale_c != nullptr);
std::string old_locale = old_locale_c;
// Push locale with multibyte mode
setlocale(LC_CTYPE, "en_US.utf8");

Expand All @@ -106,24 +109,25 @@ TEST(StringPrintfTest, Multibyte) {

// Repeat with longer string, to make sure that the dynamically
// allocated path in StringAppendV is handled correctly.
int n = 2048;
char* buf = new char[n+1];
memset(buf, ' ', n-3);
memcpy(buf + n - 3, kInvalidCodePoint, 4);
value = StringPrintf("%.*s", n, buf);
const size_t n = 2048;
std::array<char, n+1> buf;
memset(&buf[0], ' ', n-3);
memcpy(&buf[0] + n - 3, kInvalidCodePoint, 4);
value = StringPrintf("%.*s", n, &buf[0]);
// See GRTEv2 vs. GRTEv3 comment above.
EXPECT_TRUE(value.empty() || value == buf);
delete[] buf;
EXPECT_TRUE(value.empty() || value == &buf[0]);

setlocale(LC_CTYPE, old_locale);
setlocale(LC_CTYPE, old_locale.c_str());
}

TEST(StringPrintfTest, NoMultibyte) {
// No multibyte handling, but the string contains funny chars.
char* old_locale = setlocale(LC_CTYPE, nullptr);
char* old_locale_c = setlocale(LC_CTYPE, nullptr);
ASSERT_TRUE(old_locale_c != nullptr);
std::string old_locale = old_locale_c;
setlocale(LC_CTYPE, "POSIX");
std::string value = StringPrintf("%.*s", 3, "\375\067s");
setlocale(LC_CTYPE, old_locale);
setlocale(LC_CTYPE, old_locale.c_str());
EXPECT_EQ("\375\067s", value);
}

Expand Down