#12183 closed Bugs (fixed)

GCC 6.1 thinks boost::container::string violates strict aliasing

Reported by: tavianator@… Owned by: Ion Gaztañaga
Milestone: To Be Determined Component: container
Version: Boost 1.61.0 Severity: Problem
Keywords: Cc:


The following simple test case aborts when built with -O2, but works fine with -fno-strict-aliasing.

$ cat foo.cpp
#include <boost/container/string.hpp>
#include <cstdlib>
#include <utility>

using boost::container::string;

struct foo
  foo(string str)
    : m_str{std::move(str)},
  { }

  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) {
  return 0;
$ g++ -O2 -Wall foo.cpp -o foo && ./foo
[1]    6375 abort (core dumped)  ./foo

I reported this as GCC bug 71002 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 GCC bug 14319 for example.

Attachments (0)

Change History (4)

comment:1 Changed 17 months ago by Tavian Barnes <tavianator@…>

In, a GCC dev confirms that it's ultimately Boost's bug.

comment:2 Changed 17 months ago by Ion Gaztañaga

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++.

In any case, would this "is_short" implementation avoid UB?

   bool is_short() const
      short_header hdr;
      *(unsigned char*)&hdr = *(unsigned char*)&this->members_.m_repr;
      return hdr.is_short != 0;

comment:3 Changed 17 months ago by Tavian Barnes <tavianator@…>

That looks like it avoids UB. Works under GCC 6.1 too.

comment:4 Changed 17 months ago by Ion Gaztañaga

Resolution: fixed
Status: newclosed

Modify Ticket

Change Properties
Set your email in Preferences
as closed The owner will remain Ion Gaztañaga.
The resolution will be deleted.

Add Comment

E-mail address and name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.