This is the mail archive of the gcc@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]

Undefined constant is crashing streams - g++ bug?


Hello,

I think I have found a bug in G++ . Please submit it to the bug tracker (I do not want to open an account there) if you think it is a bug - I am not sure about it.

While I worked with "search+replace" I accidently had following in my source code:

const char* DUMMY = DUMMY;

It is amaazing that this code actually does compile. And no warning is output at all.

The usage of this mysterious constant "DUMMY" causes odd behavior, e.g. if it is written to a stream, the stream becomes "broken" and nothing can be written to it anymore:

cout << "Hello world" << endl;
cout << DUMMY;
cout << "You cannot read this. I am broken..." << endl;

I also had in mind that the program might have been crashed/terminated, but in fact, it is still running. I verified it with a printf() at the end and it showed me that the program is still running and just the 'cout' stream is gone.

Here are some small reproduceable codes:

1. broken.c:
============

#include <stdio.h>
#include <iostream>
int main(void) {
        const char* DUMMY = DUMMY;
        std::cout << DUMMY;
        return 0;
}

working.c:
============

#include <stdio.h>
#include <iostream>
int main(void) {
        const char* DUMMY = "x";
        std::cout << DUMMY;
        return 0;
}

The diff result of broken.asm and working.asm is:
=====================================================

--- broken.asm  2012-04-29 08:41:25.000000000 +0200
+++ working.asm 2012-04-29 08:41:26.000000000 +0200
@@ -1 +1 @@
-       .file   "broken.c"
+       .file   "working.c"
@@ -3,0 +4,3 @@
+       .section        .rodata
+.LC0:
+       .string "x"
@@ -16,0 +20 @@
+       movq    $.LC0, -8(%rbp)

A part of working.asm:
======================

.LC0:
.string "x"
.text
main:
.LFB957:
.cfi_startproc
.cfi_personality 0x3,__gxx_personality_v0
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
subq $16, %rsp
movq $.LC0, -8(%rbp) // <----- this is missing at broken.asm!
movq -8(%rbp), %rax
movq %rax, %rsi
movl $_ZSt4cout, %edi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
movl $0, %eax
leave
ret
.cfi_endproc


A part of broken.asm:
======================

main:
.LFB957:
        .cfi_startproc
        .cfi_personality 0x3,__gxx_personality_v0
        pushq   %rbp
        .cfi_def_cfa_offset 16
        movq    %rsp, %rbp
        .cfi_offset 6, -16
        .cfi_def_cfa_register 6
        subq    $16, %rsp
        movq    -8(%rbp), %rax
        movq    %rax, %rsi
        movl    $_ZSt4cout, %edi
        call    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
        movl    $0, %eax
        leave
        ret
        .cfi_endproc

My 'g++ -v' output:
====================

Es werden eingebaute Spezifikationen verwendet.
Ziel: x86_64-linux-gnu
Konfiguriert mit: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread-Modell: posix
gcc-Version 4.4.5 (Debian 4.4.5-8)

(Note: It is the latest version I can get. Since it is a production system I cannot install newer unstable versions and I do not have a Linux box at home.)


Best regards
Daniel Marschall


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