From 8900d7c496afb0fb0e0a737d001a46828bc446dd Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Mon, 18 Dec 2023 12:37:32 -0800 Subject: Mutex: Fix Condition pointer-to-member cast to respect const qualifier Previously, `absl::Condition` incorrectly used the same (non-`const`) pointer-to-method type when wrapping both `const` and non-`const` methods. Unfortunately, this is undefined behavior according to `[expr.reinterpret.cast]` in the C++ standard: > The effect of calling a function through a pointer to a function type that is > not the same as the type used in the definition of the function is undefined. This fixes the UB. PiperOrigin-RevId: 591981682 Change-Id: Iaca955346699417232383d3a1800ea9b82ea5761 --- absl/synchronization/mutex_test.cc | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'absl/synchronization/mutex_test.cc') diff --git a/absl/synchronization/mutex_test.cc b/absl/synchronization/mutex_test.cc index 35b43334..a8d75827 100644 --- a/absl/synchronization/mutex_test.cc +++ b/absl/synchronization/mutex_test.cc @@ -977,6 +977,15 @@ TEST(Mutex, FunctionPointerConditionWithDerivedToBaseConversion) { const Derived *>::value)); } +struct Constable { + bool WotsAllThisThen() const { return true; } +}; + +TEST(Mutex, FunctionPointerConditionWithConstMethod) { + const Constable chapman; + EXPECT_TRUE(absl::Condition(&chapman, &Constable::WotsAllThisThen).Eval()); +} + struct True { template bool operator()(Args...) const { -- cgit v1.2.3