Skip to content

Commit

Permalink
stringprintf_unittest.cc: Replace new char[n+1] with std::array
Browse files Browse the repository at this point in the history
Prefer safer alternative to naked pointers.

This is a follow-up to 1dd313c
  • Loading branch information
florin-crisan committed Jun 15, 2021
1 parent 1dd313c commit 8d1cc39
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 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 @@ -108,14 +109,13 @@ 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.c_str());
}
Expand Down

0 comments on commit 8d1cc39

Please sign in to comment.