From b888d2ae472e447611b3767bab7818cd4d9095e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Mon, 16 May 2016 16:59:25 +0200 Subject: [PATCH] Fixes Trac #12183 (GCC 6.1 thinks boost::container::string violates strict aliasing) --- doc/container.qbk | 2 +- include/boost/container/string.hpp | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/doc/container.qbk b/doc/container.qbk index be6ea1f..3dda2b1 100644 --- a/doc/container.qbk +++ b/doc/container.qbk @@ -1218,10 +1218,10 @@ use [*Boost.Container]? There are several reasons for that: * Fixed bugs: * [@https://svn.boost.org/trac/boost/ticket/11170 Trac #11170: ['"Doc slip for index_of"]]. * [@https://svn.boost.org/trac/boost/ticket/12177 Trac #12177: ['"vector::priv_merge uses unqualified uintptr_t"]]. + * [@https://svn.boost.org/trac/boost/ticket/12183 Trac #12183: ['"GCC 6.1 thinks boost::container::string violates strict aliasing"]]. [endsect] - [section:release_notes_boost_1_61_00 Boost 1.61 Release] * [classref boost::container::small_vector] supports more constructors and assignments. diff --git a/include/boost/container/string.hpp b/include/boost/container/string.hpp index 33f5f66..af85853 100644 --- a/include/boost/container/string.hpp +++ b/include/boost/container/string.hpp @@ -230,7 +230,13 @@ class basic_string_base protected: bool is_short() const - { return static_cast(this->members_.m_repr.s.h.is_short != 0); } + { + //Access and copy (to avoid UB) the first byte of the union to know if the + //active representation is short or long + short_header hdr; + *(unsigned char*)&hdr = *(unsigned char*)&this->members_.m_repr; + return hdr.is_short != 0; + } void is_short(bool yes) {