From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22447 invoked by alias); 28 Nov 2012 10:04:10 -0000 Received: (qmail 22412 invoked by uid 48); 28 Nov 2012 10:03:55 -0000 From: "gnobal at hotmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/55513] New: Incorrect snprintf folding when building with -std=c++0x Date: Wed, 28 Nov 2012 10:04:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: gnobal at hotmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2012-11/txt/msg02694.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55513 Bug #: 55513 Summary: Incorrect snprintf folding when building with -std=c++0x Classification: Unclassified Product: gcc Version: 4.7.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned@gcc.gnu.org ReportedBy: gnobal@hotmail.com Using the following program: #include #include int main() { char str[10]; const int strLength = snprintf(str, sizeof(str), "Hello"); std::cout << strLength << ':' << str << std::endl; return 0; } When built with the following command: g++ test_snprintf.cc -O0 -g3 -std=c++0x The output of running it is: 5: instead of: 5: Hello The program will work fine if either of the following changes are made 1. -std=c++0x is removed from the build command line. 2. The const is removed from the definition of the variable strLength Here's a disassembly of main() up until the call to ostream::operator<<, when built with -std=c++0x: 0x0000000000400a6c <+0>: push %rbp 0x0000000000400a6d <+1>: mov %rsp,%rbp 0x0000000000400a70 <+4>: sub $0x30,%rsp 0x0000000000400a74 <+8>: mov %fs:0x28,%rax 0x0000000000400a7d <+17>: mov %rax,-0x8(%rbp) 0x0000000000400a81 <+21>: xor %eax,%eax 0x0000000000400a83 <+23>: movl $0x5,-0x24(%rbp) 0x0000000000400a8a <+30>: mov $0x5,%esi 0x0000000000400a8f <+35>: mov $0x6010a0,%edi 0x0000000000400a94 <+40>: callq 0x4008a0 <_ZNSolsEi@plt> 0x0000000000400a99 <+45>: mov $0x3a,%esi 0x0000000000400a9e <+50>: mov %rax,%rdi 0x0000000000400aa1 <+53>: callq 0x4008e0 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c@plt> Here's a disassembly of main() up until the call to ostream::operator<<, when built without -std=c++0x _or_ when the const is removed (same result): 0x0000000000400a6c <+0>: push %rbp 0x0000000000400a6d <+1>: mov %rsp,%rbp 0x0000000000400a70 <+4>: sub $0x30,%rsp 0x0000000000400a74 <+8>: mov %fs:0x28,%rax 0x0000000000400a7d <+17>: mov %rax,-0x8(%rbp) 0x0000000000400a81 <+21>: xor %eax,%eax 0x0000000000400a83 <+23>: lea -0x20(%rbp),%rax 0x0000000000400a87 <+27>: movl $0x6c6c6548,(%rax) 0x0000000000400a8d <+33>: movw $0x6f,0x4(%rax) 0x0000000000400a93 <+39>: movl $0x5,-0x24(%rbp) 0x0000000000400a9a <+46>: mov -0x24(%rbp),%eax 0x0000000000400a9d <+49>: mov %eax,%esi 0x0000000000400a9f <+51>: mov $0x6010a0,%edi 0x0000000000400aa4 <+56>: callq 0x4008a0 <_ZNSolsEi@plt> 0x0000000000400aa9 <+61>: mov $0x3a,%esi 0x0000000000400aae <+66>: mov %rax,%rdi 0x0000000000400ab1 <+69>: callq 0x4008e0 <_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_c@plt> OS Version: Ubuntu 12.10 uname -a output: Linux desktop 3.5.0-18-generic #29-Ubuntu SMP Fri Oct 19 10:26:51 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux gcc --version output: gcc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2