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]

g++ code generation bug


Given the following code snippet:

//test19.cc-----------------------------------------------------
#include <iostream.h>

template <class T> class A
{
 public:
        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.cc;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 test19.cc;      
Reading specs from
/usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.13/specs
gcc version egcs-2.91.13 980308 (gcc-2.8.0 release)
 /usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.13/cpp
-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) test19.cc /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:
 /usr/local/include/g++
 /usr/local/include
 /usr/local/i686-pc-linux-gnulibc1/include
 /usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.13/include
 /usr/include
End of search list.
 /usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.13/cc1plus
/tmp/cca00461.ii -quiet -dumpbase test19.cc -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
linux-2.8.1.0.15
 /usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.13/ld -m
elf_i386 -dynamic-linker /lib/ld-linux.so.1 -o test19 /usr/lib/crt1.o
/usr/lib/crti.o
/usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.13/crtbegin.o
-L/usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.13
-L/usr/local/i686-pc-linux-gnulibc1/lib -L/usr/local/lib
/tmp/cca004611.o -lstdc++ -lm -lgcc -lc -lgcc
/usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.91.13/crtend.o
/usr/lib/crtn.o


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