This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/68047] New: diagnose placement new with misaligned buffer


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68047

            Bug ID: 68047
           Summary: diagnose placement new with misaligned buffer
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

Related to bug 67942 and bug 36159, using C++ placement new to construct an
object in a inappropriately aligned buffer is undefined and can lead to crashes
as shown in the test case below.  When the placement new expression is being
used with a plain character buffer that isn't guaranteed to be aligned on the
same boundary as the object being constructed, GCC could and arguably should
issue a diagnostic suggesting to align the buffer as necessary.  Since
depending on the alignment, there may be decent chance that the character
buffer just happens to be aligned or the misaligned access is tolerated on the
target hardware, the warning should probably be explicitly requested (i.e., not
be included in -Wall).

This bug tracks the proposed implementation of this diagnostic.

$ cat t.cpp && g++ -Wall -Wextra -Wpedantic -O2 -g t.cpp && gdb -batch -q -ex
'r' -ex 'bt' -ex 'disas' ./a.out
typedef __typeof__ (sizeof 0) size_t;

void* operator new (size_t, void *p) { return p; }
void* operator new[] (size_t, void *p) { return p; }

typedef int v4si __attribute__ ((vector_size (16)));

struct S { v4si v; };

S __attribute__ ((weak)) foo (S *p, v4si x)
{
    p->v = x;
    return *p;
}

S __attribute__ ((weak)) f (v4si x)
{
    char a [sizeof (S) * 2];

    S *p = new (a + 1) S ();

    return foo (p, x);
}

int main  ()
{
    v4si x = { 1, 2, 3, 4 };

    f (x);
}


Program received signal SIGSEGV, Segmentation fault.
foo(S*, int __vector(4)) (p=p@entry=0x7fffffffded1, x=...) at t.cpp:12
12          p->v = x;
#0  foo(S*, int __vector(4)) (p=p@entry=0x7fffffffded1, x=...) at t.cpp:12
#1  0x0000000000400667 in f(int __vector(4)) (x=...) at t.cpp:22
#2  0x00000000004004c1 in main () at t.cpp:29
Dump of assembler code for function foo(S*, int __vector(4)):
=> 0x0000000000400640 <+0>:     movaps %xmm0,(%rdi)
   0x0000000000400643 <+3>:     retq   
End of assembler dump.
$


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]