From 32c3dce34bb0d3174a728ff8f9dbb51653b17e1e Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 5 Feb 2026 06:51:55 -0800 Subject: [PATCH] Display the actual error code when RE::Init() fails PiperOrigin-RevId: 865923485 Change-Id: I5530050f7ff3fe295e2bbd7ad0dd90cd0de3e2c3 --- googletest/src/gtest-port.cc | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/googletest/src/gtest-port.cc b/googletest/src/gtest-port.cc index d50d07cd..f8ecb37c 100644 --- a/googletest/src/gtest-port.cc +++ b/googletest/src/gtest-port.cc @@ -89,6 +89,7 @@ #include "gtest/gtest-message.h" #include "gtest/gtest-spi.h" +#include "gtest/gtest.h" #include "gtest/internal/gtest-internal.h" #include "gtest/internal/gtest-string.h" #include "src/gtest-internal-inl.h" @@ -729,7 +730,7 @@ void RE::Init(const char* regex) { char* const full_pattern = new char[full_regex_len]; snprintf(full_pattern, full_regex_len, "^(%s)$", regex); - is_valid_ = regcomp(&full_regex_, full_pattern, reg_flags) == 0; + int error = regcomp(&full_regex_, full_pattern, reg_flags); // We want to call regcomp(&partial_regex_, ...) even if the // previous expression returns false. Otherwise partial_regex_ may // not be properly initialized can may cause trouble when it's @@ -738,13 +739,13 @@ void RE::Init(const char* regex) { // Some implementation of POSIX regex (e.g. on at least some // versions of Cygwin) doesn't accept the empty string as a valid // regex. We change it to an equivalent form "()" to be safe. - if (is_valid_) { + if (!error) { const char* const partial_regex = (*regex == '\0') ? "()" : regex; - is_valid_ = regcomp(&partial_regex_, partial_regex, reg_flags) == 0; + error = regcomp(&partial_regex_, partial_regex, reg_flags); } - EXPECT_TRUE(is_valid_) - << "Regular expression \"" << regex - << "\" is not a valid POSIX Extended regular expression."; + is_valid_ = error == 0; + EXPECT_EQ(error, 0) << "Regular expression \"" << regex + << "\" is not a valid POSIX Extended regular expression."; delete[] full_pattern; }