Fail smoothly for nullptr on c-style strings in IsEmpty

PiperOrigin-RevId: 944115454
Change-Id: I99284439b0c0e0acee1cb4e12f2d0b8d422c83ba
This commit is contained in:
Mike Kruskal
2026-07-07 15:08:58 -07:00
committed by Copybara-Service
parent cd975499e8
commit 7d012aa162
2 changed files with 8 additions and 0 deletions
@@ -76,12 +76,18 @@ class [[nodiscard]] IsEmptyMatcher {
// Matches C-style strings.
bool MatchAndExplain(const char* s, MatchResultListener* listener) const {
if (s == nullptr) {
return false;
}
return MatchAndExplain(std::string(s), listener);
}
#if GTEST_HAS_STD_WSTRING
// Matches C-style wide strings.
bool MatchAndExplain(const wchar_t* s, MatchResultListener* listener) const {
if (s == nullptr) {
return false;
}
return MatchAndExplain(std::wstring(s), listener);
}
#endif // GTEST_HAS_STD_WSTRING
@@ -1092,6 +1092,7 @@ TEST(IsEmptyTest, MatchesCString) {
const char b[] = "x";
EXPECT_TRUE(m.Matches(a));
EXPECT_FALSE(m.Matches(b));
EXPECT_FALSE(m.Matches(nullptr));
}
#if GTEST_HAS_STD_WSTRING
@@ -1101,6 +1102,7 @@ TEST(IsEmptyTest, MatchesCWideString) {
const wchar_t b[] = L"x";
EXPECT_TRUE(m.Matches(a));
EXPECT_FALSE(m.Matches(b));
EXPECT_FALSE(m.Matches(nullptr));
}
#endif // GTEST_HAS_STD_WSTRING