public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/52796] New: [4.6/4.7 C++11] Initialization of primitive object with 0-length parameter pack fails to value-initialize
@ 2012-03-30 21:01 jyasskin at gcc dot gnu.org
  2012-03-30 21:22 ` [Bug c++/52796] " jyasskin at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: jyasskin at gcc dot gnu.org @ 2012-03-30 21:01 UTC (permalink / raw)
  To: gcc-bugs

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2012-05-23 21:35 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-30 21:01 [Bug c++/52796] New: [4.6/4.7 C++11] Initialization of primitive object with 0-length parameter pack fails to value-initialize jyasskin at gcc dot gnu.org
2012-03-30 21:22 ` [Bug c++/52796] " jyasskin at gcc dot gnu.org
2012-03-30 21:54 ` [Bug c++/52796] [4.6/4.7/4.8 " jyasskin at gcc dot gnu.org
2012-04-03 14:45 ` [Bug c++/52796] [C++11] " jason at gcc dot gnu.org
2012-04-03 22:51 ` jason at gcc dot gnu.org
2012-04-03 23:38 ` jason at gcc dot gnu.org
2012-04-03 23:38 ` jason at gcc dot gnu.org
2012-04-04  1:39 ` jason at gcc dot gnu.org
2012-04-05 15:07 ` bkoz at gcc dot gnu.org
2012-05-23 21:43 ` michaelh at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).