From 7d012aa1624ecb83925d2ff9135610a40526f370 Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Tue, 7 Jul 2026 15:08:58 -0700 Subject: [PATCH] Fail smoothly for nullptr on c-style strings in IsEmpty PiperOrigin-RevId: 944115454 Change-Id: I99284439b0c0e0acee1cb4e12f2d0b8d422c83ba --- googlemock/include/gmock/gmock-more-matchers.h | 6 ++++++ googlemock/test/gmock-matchers-comparisons_test.cc | 2 ++ 2 files changed, 8 insertions(+) diff --git a/googlemock/include/gmock/gmock-more-matchers.h b/googlemock/include/gmock/gmock-more-matchers.h index d29123a67..9d9688ef0 100644 --- a/googlemock/include/gmock/gmock-more-matchers.h +++ b/googlemock/include/gmock/gmock-more-matchers.h @@ -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 diff --git a/googlemock/test/gmock-matchers-comparisons_test.cc b/googlemock/test/gmock-matchers-comparisons_test.cc index 8a9981fcd..b800f1e8b 100644 --- a/googlemock/test/gmock-matchers-comparisons_test.cc +++ b/googlemock/test/gmock-matchers-comparisons_test.cc @@ -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