Opened 6 years ago

Closed 6 years ago

#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:

Description

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
{
  __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 && ./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.

Change History (4)

comment:1 by Tavian Barnes <tavianator@…>, 6 years ago

In https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71002#c8, a GCC dev confirms that it's ultimately Boost's bug.

comment:2 by Ion Gaztañaga, 6 years ago

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 by Tavian Barnes <tavianator@…>, 6 years ago

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

comment:4 by Ion Gaztañaga, 6 years ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.