basic_string.h says:

  /*
   * [...]
   *  Where the _M_p points to the first character in the string, and
   *  you cast it to a pointer-to-_Rep and subtract 1 to get a
   *  pointer to the header.
   * [...]
   */

The test case `testsuite/ext/array_allocator/2.cc' illustrates the bug.  After
linking, the global variable `extern_array' is located at an odd-numbered
address (which is fine; it is a char array).

When _M_rep() is called by basic_string's destructor, a _Rep* is returned that
is unaligned for calling _Rep methods like dispose(), because an odd-numbered
address minus 1*sizeof (_Rep) is still odd-numbered:
      _Rep*
      _M_rep() const
      { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }

This method call:
      ~basic_string()
      { _M_rep()->_M_dispose(this->get_allocator()); }

induces a bus error on architectures where misalignment triggers a bus error. 
Is this a libstdc++ bug or a documentation omission?

-- 
           Summary: basic_string::_M_rep() can produce an unnaturally
                    aligned pointer to _Rep
           Product: gcc
           Version: 4.0.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: bje at gcc dot gnu dot org
                CC: gcc-bugs at gcc dot gnu dot org
  GCC host triplet: powerpc-linux
GCC target triplet: powerpc-linux


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=19495

Reply via email to