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

g++ code generation bug

Given the following code snippet:

#include <iostream.h>

template <class T> class A
        static void foo1(int);
        static void foo2(int);

void A<int>::foo1(int i)
        cout << "template specialization A<int>::foo1 " << i << endl;

template <class T> void A<T>::foo2(int i)
        cout << "template                A<T  >::foo2 " << i << endl;

int main()
        A<int>::foo1(22); //Specialized i=junk.
        A<int>::foo2(22); //non-specialized i=22 as expected.
        return 0;

~/tests>g++ -o test19;test19
template specialization A<int>::foo1 -1073743268
template                A<T  >::foo2 22

The specialized version of the static member template is compiled
incorrectly.  In particular the parameter 'i' gets scrambled. 
Investigation with gdb reveals that the specialized verion is compiled
as a (non-static) member function so that it assumes 'this' and 'i' were
pushed onto the stack, when in fact only 'i' was pushed.  Resulting in
'i'=junk.  As you can see the non-specialized template is fine
('i'=22).  This bug is rather nasty when the function arguments are
references or pointers, in which case a seg-fault is inevitable. I hope
this is useful.

System details:

~/tests>g++ -v -o test19;      
Reading specs from
gcc version egcs-2.91.13 980308 (gcc-2.8.0 release)
-lang-c++ -v -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus
-D__GNUC_MINOR__=91 -D__ELF__ -Dunix -Dlinux -D__ELF__ -D__unix__
-D__linux__ -D__unix -D__linux -Asystem(posix) -D__EXCEPTIONS -Di386
-Di686 -Asystem(unix) -Acpu(i386) -Amachine(i386) -D__i386__ -D__i686__
-Asystem(unix) -Acpu(i386) -Amachine(i386) /tmp/cca00461.ii
GNU CPP version egcs-2.91.13 980308 (gcc-2.8.0 release) (i386 Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
End of search list.
/tmp/cca00461.ii -quiet -dumpbase -version -o /tmp/cca00461.s
GNU C++ version egcs-2.91.13 980308 (gcc-2.8.0 release)
(i686-pc-linux-gnulibc1) compiled by GNU C version egcs-2.91.12 980302
(gcc-2.8.0 release).
 as -V -Qy -o /tmp/cca004611.o /tmp/cca00461.s
GNU assembler version 970731 (i586-linux), using BFD version
 /usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.13/ld -m
elf_i386 -dynamic-linker /lib/ -o test19 /usr/lib/crt1.o
-L/usr/local/i686-pc-linux-gnulibc1/lib -L/usr/local/lib
/tmp/cca004611.o -lstdc++ -lm -lgcc -lc -lgcc

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