if the base class and the derived class have different alignments, g++
sometimes uses the alignment of the base class for the derived class. 

it happens if the source code has definition of a base class instance before
the declaration of the derived class, then the base alignment is used, which is
wrong.

3 files to reproduce the bug:
====================== gcc_align_bug.h ========================
#pragma pack(push, 1)
struct myset_t : public std::set<int>
{
  void myadd(int x);
};
#pragma pack(pop)

====================== gcc_align_bug.cpp ======================
#include <set>
#include <stdio.h>

// comment this line to fix the bug. apparently is causes generation of
// wrong code for myset_t:
std::set<int> my_intset;

#include "gcc_align_bug.h"

int main()
{
  printf("alignment bug demo\n");

  myset_t s;
  s.myadd(0);   // crash

  // NOTREACHED
  printf("all ok\n");
  return 0;
}

====================== myadd.cpp ======================
#include <set>
#include "gcc_align_bug.h"

//-------------------------------------------------------------------------
void myset_t::myadd(int x)
{
  // will crash here:
  insert(x);
}



Compile without any options and run:

> g++ gcc_align_bug.cpp myadd.cpp 
> ./a.out
alignment bug demo
Segmentation fault


-- 
           Summary: structure alignment for derived class is wrong in some
                    cases
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: info at hex-rays dot com
 GCC build triplet: 4.4.1 - 4.1.3 and probably other builds
  GCC host triplet: x86-linux-elf
GCC target triplet: x86-linux-elf


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

Reply via email to