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++/52796] New: [4.6/4.7 C++11] Initialization of primitive object with 0-length parameter pack fails to value-initialize


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

             Bug #: 52796
           Summary: [4.6/4.7 C++11] Initialization of primitive object
                    with 0-length parameter pack fails to value-initialize
    Classification: Unclassified
           Product: gcc
           Version: 4.7.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: jyasskin@gcc.gnu.org


The following program attempts to initialize a 'char' member using a variadic
parameter pack. With length 0, the initialization doesn't happen, leaving the
original value of the memory in the field. I originally noticed this in gcc-4.6
when std::list<char>(3) failed to initialize the list elements.


$ cat test.cc
#include <memory.h>
#include <iostream>

template<typename T>
class Wrapper {
 public:
#if WORK
  Wrapper()
      : t() {
  }
#endif
  template<typename ...Args>
  Wrapper(Args&&... args)
      : t(args...) {
  }

  T t;
};

int main() {
  __attribute__(aligned(alignof(Wrapper<char>))) char
space[sizeof(Wrapper<char>)];
  memset(space, '\xab', sizeof(space));
  Wrapper<char>*w = new(space) Wrapper<char>;
  std::cout << (int)w->t << '\n';
  w->~Wrapper<char>();
  memset(space, '\xab', sizeof(space));
  w = new(space) Wrapper<char>();
  std::cout << (int)w->t << '\n';
  w->~Wrapper<char>();
  memset(space, '\xab', sizeof(space));
  w = new(space) Wrapper<char>((int)'y');
  std::cout << (int)w->t << '\n';
  w->~Wrapper<char>();
}

$ g++-4.7pre --version
g++-4.7pre (GCC) 4.7.1 20120330 (prerelease)
...
$ g++-4.7pre -Wall -std=c++11 test.cc -g3 -o test && ./test
-85
0
121


Disassembling with gdb shows:

Dump of assembler code for function Wrapper<char>::Wrapper<>():
   0x0000000000400a70 <+0>:     push   %rbp
   0x0000000000400a71 <+1>:     mov    %rsp,%rbp
   0x0000000000400a74 <+4>:     mov    %rdi,-0x8(%rbp)
=> 0x0000000000400a78 <+8>:     pop    %rbp
   0x0000000000400a79 <+9>:     retq


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