Fix port_test.cc in optimized builds

In optimized builds, this test currently calls `__builtin_assume()` on a false
statement, which is undefined behavior that trips up ubsan. This change fixes
that by skipping the test case when `NDEBUG` is defined.

PiperOrigin-RevId: 721617118
pull/20179/head
Adam Cozzette 4 weeks ago committed by Copybara-Service
parent fd20cfcd8c
commit 8a0c6bf079
  1. 7
      src/google/protobuf/port_test.cc

@ -25,10 +25,17 @@ int assume_var_for_test = 1;
TEST(PortTest, ProtobufAssume) {
PROTOBUF_ASSUME(assume_var_for_test == 1);
#ifdef GTEST_HAS_DEATH_TEST
#if defined(NDEBUG)
// If NDEBUG is defined, then instead of reliably crashing, the code below
// will assume a false statement. This is undefined behavior which will trip
// up sanitizers.
GTEST_SKIP() << "Can't test PROTOBUF_ASSUME()";
#else
EXPECT_DEBUG_DEATH(
PROTOBUF_ASSUME(assume_var_for_test == 2),
"port_test\\.cc:.*Assumption failed: 'assume_var_for_test == 2'");
#endif
#endif
}
TEST(PortTest, UnreachableTrapsOnDebugMode) {

Loading…
Cancel
Save