fix: detect macro-redefined POSIX regex symbols and fall back to SIMPLE_RE

On some systems (e.g. Fedora 42 with GCC 15), MinGW or Cygwin headers
leaking into the include path redefine regcomp, regexec, and regfree as
macros (e.g. regcomp -> regcompA). This causes linker errors because the
A-suffixed symbols do not exist in glibc.

Add a compile-time check in gtest-port.h: after including <regex.h>, if
any of regcomp, regexec, or regfree are defined as macros, undefine
GTEST_USES_POSIX_RE and fall back to GTEST_USES_SIMPLE_RE instead.
This leverages GoogleTest's existing simple regex implementation rather
than modifying call sites.

Fixes #5001
This commit is contained in:
Manikandan K. S. 2026-07-25 21:45:25 +05:30
parent b78aa5ee64
commit eed0985f87

View File

@ -443,7 +443,17 @@ typedef struct _RTL_CRITICAL_SECTION GTEST_CRITICAL_SECTION;
#define GTEST_USES_RE2 1
#elif GTEST_HAS_POSIX_RE
#include <regex.h> // NOLINT
// If the POSIX regex functions have been redefined as macros (e.g., by
// MinGW headers leaking into the include path on non-Windows systems),
// the POSIX regex implementation cannot be used reliably. Fall back to
// the simple regex implementation instead.
#if defined(regcomp) || defined(regexec) || defined(regfree)
#undef GTEST_USES_POSIX_RE
#define GTEST_USES_SIMPLE_RE 1
#else
#define GTEST_USES_POSIX_RE 1
#endif // defined(regcomp) || defined(regexec) || defined(regfree)
#else
// Use our own simple regex implementation.
#define GTEST_USES_SIMPLE_RE 1