Skip to content

Commit 50ce520

Browse files
Abseil Teamderekmauro
authored andcommitted
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
1 parent c13c27a commit 50ce520

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

googletest/include/gtest/gtest-matchers.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,11 @@ class MatcherBase : private MatcherDescriberInterface {
421421
template <typename M, bool = IsInlined<M>()>
422422
struct ValuePolicy {
423423
static const M& Get(const MatcherBase& m) {
424-
return reinterpret_cast<const M&>(m.buffer_);
424+
// When inlined along with Init, need to be explicit to avoid violating
425+
// strict aliasing rules.
426+
const M *ptr = static_cast<const M*>(
427+
static_cast<const void*>(&m.buffer_));
428+
return *ptr;
425429
}
426430
static void Init(MatcherBase& m, M impl) {
427431
::new (static_cast<void*>(&m.buffer_)) M(impl);

0 commit comments

Comments
 (0)