Commit 50ce5201 by Abseil Team Committed by Derek Mauro

Googletest export

Launder buffer before reference In GCC, directly casting the Buffer reference to another type results in strict-aliasing violation errors. This launders the reference using an intermediate pointer prior to creating the new reference. PiperOrigin-RevId: 350809323
parent c13c27a5
...@@ -421,7 +421,11 @@ class MatcherBase : private MatcherDescriberInterface { ...@@ -421,7 +421,11 @@ class MatcherBase : private MatcherDescriberInterface {
template <typename M, bool = IsInlined<M>()> template <typename M, bool = IsInlined<M>()>
struct ValuePolicy { struct ValuePolicy {
static const M& Get(const MatcherBase& m) { static const M& Get(const MatcherBase& m) {
return reinterpret_cast<const M&>(m.buffer_); // When inlined along with Init, need to be explicit to avoid violating
// strict aliasing rules.
const M *ptr = static_cast<const M*>(
static_cast<const void*>(&m.buffer_));
return *ptr;
} }
static void Init(MatcherBase& m, M impl) { static void Init(MatcherBase& m, M impl) {
::new (static_cast<void*>(&m.buffer_)) M(impl); ::new (static_cast<void*>(&m.buffer_)) M(impl);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment