diff options
author | Abseil Team <absl-team@google.com> | 2021-01-08 15:00:47 -0500 |
---|---|---|
committer | Derek Mauro <dmauro@google.com> | 2021-01-13 20:59:28 -0500 |
commit | 50ce52016139a4346a94df71249c14c5d286e000 (patch) | |
tree | 2b42ba0312dae2de43cc2619c3a4eea2cbcc4664 /googletest/include/gtest | |
parent | c13c27a513ecd1cbf5700a45fe590e85e8ae6770 (diff) | |
download | googletest-50ce52016139a4346a94df71249c14c5d286e000.tar.gz googletest-50ce52016139a4346a94df71249c14c5d286e000.zip |
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
Diffstat (limited to 'googletest/include/gtest')
-rw-r--r-- | googletest/include/gtest/gtest-matchers.h | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/googletest/include/gtest/gtest-matchers.h b/googletest/include/gtest/gtest-matchers.h index 6b1bb6af..1bb3140d 100644 --- a/googletest/include/gtest/gtest-matchers.h +++ b/googletest/include/gtest/gtest-matchers.h @@ -421,7 +421,11 @@ class MatcherBase : private MatcherDescriberInterface { template <typename M, bool = IsInlined<M>()> struct ValuePolicy { 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) { ::new (static_cast<void*>(&m.buffer_)) M(impl); |