From bf24e8b443ab0907a4996b03ef37bd4d8818a416 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 5 Feb 2026 14:14:00 -0800 Subject: [PATCH] Perform ValidateDownCast only in Abseil Hardened Extensive mode Enabling downcast validation checks has incurred about 0.5% overhead on some Google workloads. Consequently it does not seem like a good inclusion in Abseil Hardened Fast mode, though it remains enabled in Abseil Hardened Extensive mode. PiperOrigin-RevId: 866108195 Change-Id: Ic796e8bce90bb0d80ad60269a0b3857f19f2c95c --- absl/base/casts.h | 2 +- absl/base/casts_test.cc | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/absl/base/casts.h b/absl/base/casts.h index 38243d87..928f4091 100644 --- a/absl/base/casts.h +++ b/absl/base/casts.h @@ -211,7 +211,7 @@ inline void ValidateDownCast(From* f ABSL_ATTRIBUTE_UNUSED) { // Assert only if RTTI is enabled and in debug mode or hardened asserts are // enabled. #ifdef ABSL_INTERNAL_HAS_RTTI -#if !defined(NDEBUG) || (ABSL_OPTION_HARDENED == 1 || ABSL_OPTION_HARDENED == 2) +#if !defined(NDEBUG) || (ABSL_OPTION_HARDENED == 1) // Suppress erroneous nonnull comparison warning on older GCC. #if defined(__GNUC__) && !defined(__clang__) #pragma GCC diagnostic push diff --git a/absl/base/casts_test.cc b/absl/base/casts_test.cc index 772225e2..dda3ae6e 100644 --- a/absl/base/casts_test.cc +++ b/absl/base/casts_test.cc @@ -92,8 +92,7 @@ TEST(DownCastTest, Pointer) { // Tests a bad downcast. We have to disguise the badness just enough // that the compiler doesn't warn about it at compile time. BaseForDownCast* base2 = new BaseForDownCast(); -#if GTEST_HAS_DEATH_TEST && (!defined(NDEBUG) || (ABSL_OPTION_HARDENED == 1 || \ - ABSL_OPTION_HARDENED == 2)) +#if GTEST_HAS_DEATH_TEST && (!defined(NDEBUG) || (ABSL_OPTION_HARDENED == 1)) EXPECT_DEATH(static_cast(absl::down_cast(base2)), ".*down cast from .*BaseForDownCast.* to " ".*DerivedForDownCast.* failed.*"); @@ -126,8 +125,7 @@ TEST(DownCastTest, Reference) { // Tests a bad downcast. We have to disguise the badness just enough // that the compiler doesn't warn about it at compile time. BaseForDownCast& base2 = *new BaseForDownCast(); -#if GTEST_HAS_DEATH_TEST && (!defined(NDEBUG) || (ABSL_OPTION_HARDENED == 1 || \ - ABSL_OPTION_HARDENED == 2)) +#if GTEST_HAS_DEATH_TEST && (!defined(NDEBUG) || (ABSL_OPTION_HARDENED == 1)) EXPECT_DEATH(static_cast(absl::down_cast(base2)), ".*down cast from .*BaseForDownCast.* to " ".*DerivedForDownCast.* failed.*"); @@ -140,8 +138,7 @@ TEST(DownCastTest, ErrorMessage) { BaseForDownCast& base = derived; (void)base; -#if GTEST_HAS_DEATH_TEST && (!defined(NDEBUG) || (ABSL_OPTION_HARDENED == 1 || \ - ABSL_OPTION_HARDENED == 2)) +#if GTEST_HAS_DEATH_TEST && (!defined(NDEBUG) || (ABSL_OPTION_HARDENED == 1)) EXPECT_DEATH(static_cast(absl::down_cast(base)), ".*down cast from .*DerivedForDownCast.* to " ".*Derived2ForDownCast.* failed.*");