From 8eff9e336692fc95961e096564f1044c600b881d Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 10 Sep 2026 09:46:43 -0700 Subject: [PATCH] Fix a null initializer for pthread_t in GTest's Mutex. Static (linker-initialized) global mutex has a pthread_t member, which is currently initialized with 0. This is incorrect, since pthread_t is an opaque type, which can be a struct in some libc implementations (e.g. in LLVM-libc). Using POSIX-specified PTHREAD_NULL is a better option, however it may not be available on older platforms. So, use PTHREAD_NULL if it's available, but fallback to the original behavior for compatibility. PiperOrigin-RevId: 979239391 Change-Id: I79a292868f85f62bb1962b3ac956ad40eb145907 --- googletest/include/gtest/internal/gtest-port.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index 0947aee1..3b2947b8 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -1755,14 +1755,16 @@ class [[nodiscard]] MutexBase { #define GTEST_DECLARE_STATIC_MUTEX_(mutex) \ extern ::testing::internal::MutexBase mutex +#if defined(PTHREAD_NULL) +#define GTEST_INTERNAL_PTHREAD_NULL PTHREAD_NULL +#else +#define GTEST_INTERNAL_PTHREAD_NULL (pthread_t{}) +#endif + // Defines and statically (i.e. at link time) initializes a static mutex. -// The initialization list here does not explicitly initialize each field, -// instead relying on default initialization for the unspecified fields. In -// particular, the owner_ field (a pthread_t) is not explicitly initialized. -// This allows initialization to work whether pthread_t is a scalar or struct. -// The flag -Wmissing-field-initializers must not be specified for this to work. -#define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ - ::testing::internal::MutexBase mutex = {PTHREAD_MUTEX_INITIALIZER, false, 0} +#define GTEST_DEFINE_STATIC_MUTEX_(mutex) \ + ::testing::internal::MutexBase mutex = {PTHREAD_MUTEX_INITIALIZER, false, \ + GTEST_INTERNAL_PTHREAD_NULL} // The Mutex class can only be used for mutexes created at runtime. It // shares its API with MutexBase otherwise.