Boost C++ Libraries: Ticket #12183: GCC 6.1 thinks boost::container::string violates strict aliasing https://svn.boost.org/trac10/ticket/12183 <p> The following simple test case aborts when built with -O2, but works fine with -fno-strict-aliasing. </p> <pre class="wiki">$ cat foo.cpp #include &lt;boost/container/string.hpp&gt; #include &lt;cstdlib&gt; #include &lt;utility&gt; using boost::container::string; struct foo { __attribute__((noinline)) foo(string str) : m_str{std::move(str)}, m_len{m_str.length()} { } string m_str; std::size_t m_len; }; int main() { foo f{"the quick brown fox jumps over the lazy dog"}; if (f.m_len == 0) { std::abort(); } return 0; } $ g++ -O2 -Wall foo.cpp -o foo &amp;&amp; ./foo [1] 6375 abort (core dumped) ./foo </pre><p> I reported this as <a class="ext-link" href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71002"><span class="icon">​</span>GCC bug 71002</a> but I'm not sure that the code is actually correct. It seems to at least access a non-active union member (the is_short bitfield). There may be other aliasing issues with the union -- in some cases it seems GCC wants you to access the members through the union directly, not through a pointer to a union member; see <a class="ext-link" href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14319"><span class="icon">​</span>GCC bug 14319</a> for example. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/12183 Trac 1.4.3 Tavian Barnes <tavianator@…> Tue, 10 May 2016 13:17:20 GMT <link>https://svn.boost.org/trac10/ticket/12183#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12183#comment:1</guid> <description> <p> In <a class="ext-link" href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71002#c8"><span class="icon">​</span>https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71002#c8</a>, a GCC dev confirms that it's ultimately Boost's bug. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Fri, 13 May 2016 20:54:21 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12183#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12183#comment:2</guid> <description> <p> The access to the non-active union member was intentional as all compilers support it (as the C standard guarantees implementation-defined behaviour, C++ compilers agree to support this as an extension). The same trick is used in libc++. </p> <p> In any case, would this "is_short" implementation avoid UB? </p> <pre class="wiki"> bool is_short() const { short_header hdr; *(unsigned char*)&amp;hdr = *(unsigned char*)&amp;this-&gt;members_.m_repr; return hdr.is_short != 0; } </pre> </description> <category>Ticket</category> </item> <item> <author>Tavian Barnes <tavianator@…></author> <pubDate>Mon, 16 May 2016 14:47:17 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/12183#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/12183#comment:3</guid> <description> <p> That looks like it avoids UB. Works under GCC 6.1 too. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Mon, 16 May 2016 15:01:51 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/12183#comment:4 https://svn.boost.org/trac10/ticket/12183#comment:4 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Thanks. Fixed in [develop b888d2a]: </p> <p> <a class="ext-link" href="https://github.com/boostorg/container/commit/b888d2ae472e447611b3767bab7818cd4d9095e9"><span class="icon">​</span>https://github.com/boostorg/container/commit/b888d2ae472e447611b3767bab7818cd4d9095e9</a> </p> Ticket