diff --git a/src/google/protobuf/descriptor.cc b/src/google/protobuf/descriptor.cc index 7456d3c3d6653..7731a9405e031 100644 --- a/src/google/protobuf/descriptor.cc +++ b/src/google/protobuf/descriptor.cc @@ -343,22 +343,16 @@ class FlatAllocation { return ends_.template Get(); } - // Avoid the reinterpret_cast if the array is empty. - // Clang's Control Flow Integrity does not like the cast pointing to memory - // that is not yet initialized to be of that type. - // (from -fsanitize=cfi-unrelated-cast) template + PROTOBUF_NO_SANITIZE("cfi-unrelated-cast", "vptr") U* Begin() const { - int begin = BeginOffset(), end = EndOffset(); - if (begin == end) return nullptr; - return reinterpret_cast(data() + begin); + return reinterpret_cast(data() + BeginOffset()); } template + PROTOBUF_NO_SANITIZE("cfi-unrelated-cast", "vptr") U* End() const { - int begin = BeginOffset(), end = EndOffset(); - if (begin == end) return nullptr; - return reinterpret_cast(data() + end); + return reinterpret_cast(data() + EndOffset()); } template diff --git a/src/google/protobuf/map.h b/src/google/protobuf/map.h index 71abcb9b80564..2d3ef18139e2f 100644 --- a/src/google/protobuf/map.h +++ b/src/google/protobuf/map.h @@ -320,6 +320,7 @@ class PROTOBUF_EXPORT UntypedMapBase { UntypedMapBase& operator=(const UntypedMapBase&) = delete; template + PROTOBUF_NO_SANITIZE("cfi-unrelated-cast", "vptr") T* GetKey(NodeBase* node) const { // Debug check that `T` matches what we expect from the type info. ABSL_DCHECK_EQ(static_cast(StaticTypeKind()), @@ -332,6 +333,7 @@ class PROTOBUF_EXPORT UntypedMapBase { } template + PROTOBUF_NO_SANITIZE("cfi-unrelated-cast", "vptr") T* GetValue(NodeBase* node) const { // Debug check that `T` matches what we expect from the type info. ABSL_DCHECK_EQ(static_cast(StaticTypeKind()), diff --git a/src/google/protobuf/port_def.inc b/src/google/protobuf/port_def.inc index a12a49e9971af..8b2ebe31195d2 100644 --- a/src/google/protobuf/port_def.inc +++ b/src/google/protobuf/port_def.inc @@ -601,6 +601,16 @@ static_assert(PROTOBUF_ABSL_MIN(20230125, 3), #define PROTOBUF_PREFETCH_WITH_OFFSET(base, offset) #endif +#if !defined(_MSVC_LANG) +#if __has_attribute(no_sanitize) +#define PROTOBUF_NO_SANITIZE(...) __attribute__((no_sanitize(__VA_ARGS__))) +#else +#define PROTOBUF_NO_SANITIZE(...) +#endif // __has_attribute(no_sanitize) +#else +#define PROTOBUF_NO_SANITIZE(...) +#endif + // ThreadSafeArenaz is turned off completely in opensource builds.