summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Dino Radakovic <dinor@google.com>2022-05-17 15:21:04 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2022-05-17 15:22:17 -0700
commite92505d8d6bf50c5d44805eb5fe73e9f56484cf4 (patch)
tree15337c58ec034ea36d84701fbda295a2fa9f7616
parent7d3b4c86928ff7b17b3b881d42355062c23f6b44 (diff)
Use NullSafeStringView for const char* args to absl::StrCat, treating null pointers as ""
Fixes #1167 PiperOrigin-RevId: 449328725 Change-Id: I813785db77b94efa49eeeff4c93449334c380935
-rw-r--r--absl/strings/str_cat.h3
-rw-r--r--absl/strings/str_cat_test.cc5
2 files changed, 7 insertions, 1 deletions
diff --git a/absl/strings/str_cat.h b/absl/strings/str_cat.h
index 4d228b09..a94bc5df 100644
--- a/absl/strings/str_cat.h
+++ b/absl/strings/str_cat.h
@@ -251,7 +251,8 @@ class AlphaNum {
const strings_internal::AlphaNumBuffer<size>& buf)
: piece_(&buf.data[0], buf.size) {}
- AlphaNum(const char* c_str) : piece_(c_str) {} // NOLINT(runtime/explicit)
+ AlphaNum(const char* c_str) // NOLINT(runtime/explicit)
+ : piece_(NullSafeStringView(c_str)) {} // NOLINT(runtime/explicit)
AlphaNum(absl::string_view pc) : piece_(pc) {} // NOLINT(runtime/explicit)
template <typename Allocator>
diff --git a/absl/strings/str_cat_test.cc b/absl/strings/str_cat_test.cc
index f3770dc0..69df2502 100644
--- a/absl/strings/str_cat_test.cc
+++ b/absl/strings/str_cat_test.cc
@@ -210,6 +210,11 @@ TEST(StrCat, CornerCases) {
EXPECT_EQ(result, "");
}
+TEST(StrCat, NullConstCharPtr) {
+ const char* null = nullptr;
+ EXPECT_EQ(absl::StrCat("mon", null, "key"), "monkey");
+}
+
// A minimal allocator that uses malloc().
template <typename T>
struct Mallocator {