Display the actual error code when RE::Init() fails

PiperOrigin-RevId: 865923485
Change-Id: I5530050f7ff3fe295e2bbd7ad0dd90cd0de3e2c3
This commit is contained in:
Abseil Team 2026-02-05 06:51:55 -08:00 committed by Copybara-Service
parent 872d386a87
commit 32c3dce34b

View File

@ -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;
}