public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/12229] New: dynamic_cast in templates causes core dump in gcc 3.3.1 but not in gcc 2.95.3
@ 2003-09-10  0:04 shelly at InterraSystems dot com
  2003-09-10  0:39 ` [Bug c++/12229] " bangerth at dealii dot org
  2003-09-10  1:12 ` [Bug c++/12229] dynamic_cast, private inheritance and friendship bangerth at dealii dot org
  0 siblings, 2 replies; 3+ messages in thread
From: shelly at InterraSystems dot com @ 2003-09-10  0:04 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12229

           Summary: dynamic_cast in templates causes core dump in gcc 3.3.1
                    but not in gcc 2.95.3
           Product: gcc
           Version: 3.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: shelly at InterraSystems dot com
                CC: gcc-bugs at gcc dot gnu dot org

The source code ...

#include <stdio.h>

template <int III> class ABC;
template <int III> class DEF;
template <int III> class GHI;
template <int III> class Array;

template <int III> class ABC {
public:
	int A;
public:
	ABC(int AA) { A = AA; }
	virtual ~ABC() {};
	virtual void P() = 0;
	friend class DEF<III>;
	friend class GHI<III>;
};

template <int III> class DEF : protected ABC<III> {
	int B;
public:
	DEF(int BB) : ABC<III>(BB) { B = BB; }
	~DEF() {};
	void P() {printf("DEF P %d\n", B);}
	void Q() {printf("DEF Q %d\n", B);}
	friend class Array<III>;
};

template <int III> class GHI : protected ABC<III> {
	int C;
public:
	GHI(int CC) : ABC<III>(CC) { C = CC; }
	~GHI() {};
	void P() {printf("GHI P %d\n", C);}
	void R() {printf("GHI R %d\n", C);}
	friend class Array<III>;
};

template <int III> class Array {
	ABC<III>**	pArray;
public:
	Array() {
		pArray = new (ABC<III>*)[III];
		for (int i = 0; i < III; ++i) {
			if (i < (III / 2)) {
				pArray[i] = new DEF<III>(i);
			} else {
				pArray[i] = new GHI<III>(i);
			}
		}
	}
	~Array() {
		for (int i = 0; i < III; ++i) {
			delete pArray[i];
		}
		delete [] pArray;
	}
	void P() {
		DEF<III>*	pD;
		GHI<III>*	pG;
		for (int i = 0; i < III; ++i) {
			pArray[i]->P();
			if (i < (III / 2)) {
				pD = dynamic_cast<DEF<III>*>(pArray[i]);
				pD->Q();
			} else {
				pG = dynamic_cast<GHI<III>*>(pArray[i]);
				pG->R();
			}
		}
	}
};

int main() {
	Array<10>	sArray;
	sArray.P();
	return 0;
}

... compiles fine on both SunOS and Linux with gcc 2.95.3 and executes fine too
producing

DEF P 0
DEF Q 0
DEF P 1
DEF Q 1
DEF P 2
DEF Q 2
DEF P 3
DEF Q 3
DEF P 4
DEF Q 4
GHI P 5
GHI R 5
GHI P 6
GHI R 6
GHI P 7
GHI R 7
GHI P 8
GHI R 8
GHI P 9
GHI R 9

But on both SunOS and Linux gcc 3.3.1 (and redhat linux gcc 2.96) I get ...
DEF P 0
Segmentation fault (core dumped)

---------------------------------------------------------------------------------
shelly@redhat[146] /usr/local/bin/g++ -v a.cxx
Reading specs from /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/specs
gcc version 2.95.3 20010315 (release)
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/cpp0 -lang-c++ -v -D__GNUC__=2
-D__GNUG__=2 -D__GNUC_MINOR__=95 -D__cplusplus -D__ELF__ -Dunix -D__i386__
-Dlinux -D__ELF__ -D__unix__ -D__i386__ -D__linux__ -D__unix -D__linux
-Asystem(posix) -D__EXCEPTIONS -Acpu(i386) -Amachine(i386) -Di386 -D__i386
-D__i386__ -Di686 -Dpentiumpro -D__i686 -D__i686__ -D__pentiumpro
-D__pentiumpro__ a.cxx /tmp/cc9eYrFn.ii
GNU CPP version 2.95.3 20010315 (release) (i386 Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/../../../../include/g++-3
 /usr/local/include
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/../../../../i686-pc-linux-gnu/include
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/include
 /usr/include
End of search list.
The following default directories have been omitted from the search path:
End of omitted list.
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/cc1plus /tmp/cc9eYrFn.ii -quiet
-dumpbase a.cc -version -o /tmp/ccKPAA2C.s
GNU C++ version 2.95.3 20010315 (release) (i686-pc-linux-gnu) compiled by GNU C
version 2.95.3 20010315 (release).
 /usr/local/i686-pc-linux-gnu/bin/as -V -Qy -o /tmp/cc9HB1ZV.o /tmp/ccKPAA2C.s
GNU assembler version 2.14 (i686-pc-linux-gnu) using BFD version 2.14 20030612
 /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/collect2 -m elf_i386
-dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o
/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/crtbegin.o
-L/usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3
-L/usr/local/i686-pc-linux-gnu/lib -L/usr/local/lib /tmp/cc9HB1ZV.o -lstdc++ -lm
-lgcc -lc -lgcc /usr/local/lib/gcc-lib/i686-pc-linux-gnu/2.95.3/crtend.o
/usr/lib/crtn.o

-----------------------------------------------------------------------------------

shelly@redhat[149] /usr/local/gcc3.3.1/bin/g++ -v a.cxx
Reading specs from /usr/local/gcc3.3.1/lib/gcc-lib/i686-pc-linux-gnu/3.3.1/specs
Configured with: ../gcc-3.3.1/configure --prefix=/usr/local/gcc3.3.1
Thread model: posix
gcc version 3.3.1
 /usr/local/gcc3.3.1/lib/gcc-lib/i686-pc-linux-gnu/3.3.1/cc1plus -quiet -v
-D__GNUC__=3 -D__GNUC_MINOR__=3 -D__GNUC_PATCHLEVEL__=1 -D_GNU_SOURCE a.cxx
-D__GNUG__=3 -quiet -dumpbase a.cxx -auxbase a -version -o /tmp/ccTOAW5i.s
GNU C++ version 3.3.1 (i686-pc-linux-gnu)
        compiled by GNU C version 3.3.1.
GGC heuristics: --param ggc-min-expand=98 --param ggc-min-heapsize=128549
ignoring nonexistent directory "/usr/local/gcc3.3.1/i686-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/gcc3.3.1/include/c++/3.3.1
 /usr/local/gcc3.3.1/include/c++/3.3.1/i686-pc-linux-gnu
 /usr/local/gcc3.3.1/include/c++/3.3.1/backward
 /usr/local/include
 /usr/local/gcc3.3.1/include
 /usr/local/gcc3.3.1/lib/gcc-lib/i686-pc-linux-gnu/3.3.1/include
 /usr/include
End of search list.
 as --traditional-format -V -Qy -o /tmp/ccfSJwex.o /tmp/ccTOAW5i.s
GNU assembler version 2.10.91 (i386-redhat-linux) using BFD version 2.10.91.0.2
 /usr/local/gcc3.3.1/lib/gcc-lib/i686-pc-linux-gnu/3.3.1/collect2 -m elf_i386
-dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o
/usr/local/gcc3.3.1/lib/gcc-lib/i686-pc-linux-gnu/3.3.1/crtbegin.o
-L/usr/local/gcc3.3.1/lib/gcc-lib/i686-pc-linux-gnu/3.3.1
-L/usr/local/gcc3.3.1/lib/gcc-lib/i686-pc-linux-gnu/3.3.1/../../..
/tmp/ccfSJwex.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc
/usr/local/gcc3.3.1/lib/gcc-lib/i686-pc-linux-gnu/3.3.1/crtend.o /usr/lib/crtn.o

------------------------------------------------------------------------------------

shelly@redhat[151] g++ -v a.cxx
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs
gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-81)
 /usr/lib/gcc-lib/i386-redhat-linux/2.96/cpp0 -lang-c++ -D__GNUG__=2
-D__EXCEPTIONS -v -D__GNUC__=2 -D__GNUC_MINOR__=96 -D__GNUC_PATCHLEVEL__=0
-D__ELF__ -Dunix -Dlinux -D__ELF__ -D__unix__ -D__linux__ -D__unix -D__linux
-Asystem(posix) -Acpu(i386) -Amachine(i386) -Di386 -D__i386 -D__i386__
-D__tune_i386__ a.cxx /tmp/ccVOYG9R.ii
GNU CPP version 2.96 20000731 (Red Hat Linux 7.1 2.96-81) (cpplib) (i386 Linux/ELF)
ignoring nonexistent directory "/usr/i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/g++-3
 /usr/local/include
 /usr/lib/gcc-lib/i386-redhat-linux/2.96/include
 /usr/include
End of search list.
 /usr/lib/gcc-lib/i386-redhat-linux/2.96/cc1plus /tmp/ccVOYG9R.ii -quiet
-dumpbase a.cxx -version -o /tmp/ccWN34DD.s
GNU C++ version 2.96 20000731 (Red Hat Linux 7.1 2.96-81) (i386-redhat-linux)
compiled by GNU C version 2.96 20000731 (Red Hat Linux 7.1 2.96-81).
 as -V -Qy -o /tmp/ccxNnjvy.o /tmp/ccWN34DD.s
GNU assembler version 2.10.91 (i386-redhat-linux) using BFD version 2.10.91.0.2
 /usr/lib/gcc-lib/i386-redhat-linux/2.96/collect2 -m elf_i386 -dynamic-linker
/lib/ld-linux.so.2 /usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crti.o
/usr/lib/gcc-lib/i386-redhat-linux/2.96/crtbegin.o
-L/usr/lib/gcc-lib/i386-redhat-linux/2.96
-L/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../.. /tmp/ccxNnjvy.o -lstdc++ -lm
-lgcc -lc -lgcc /usr/lib/gcc-lib/i386-redhat-linux/2.96/crtend.o
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crtn.o

-------------------------------------------------------------------------------------

Thanks a lot for the great compiler :-)

Shelly


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

* [Bug c++/12229] dynamic_cast in templates causes core dump in gcc 3.3.1 but not in gcc 2.95.3
  2003-09-10  0:04 [Bug c++/12229] New: dynamic_cast in templates causes core dump in gcc 3.3.1 but not in gcc 2.95.3 shelly at InterraSystems dot com
@ 2003-09-10  0:39 ` bangerth at dealii dot org
  2003-09-10  1:12 ` [Bug c++/12229] dynamic_cast, private inheritance and friendship bangerth at dealii dot org
  1 sibling, 0 replies; 3+ messages in thread
From: bangerth at dealii dot org @ 2003-09-10  0:39 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12229



------- Additional Comments From bangerth at dealii dot org  2003-09-10 00:39 -------
I can confirm this. I'm presently investigating, so wait for a smaller
testcase, guys.

W.


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

* [Bug c++/12229] dynamic_cast, private inheritance and friendship
  2003-09-10  0:04 [Bug c++/12229] New: dynamic_cast in templates causes core dump in gcc 3.3.1 but not in gcc 2.95.3 shelly at InterraSystems dot com
  2003-09-10  0:39 ` [Bug c++/12229] " bangerth at dealii dot org
@ 2003-09-10  1:12 ` bangerth at dealii dot org
  1 sibling, 0 replies; 3+ messages in thread
From: bangerth at dealii dot org @ 2003-09-10  1:12 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12229


bangerth at dealii dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID
            Summary|dynamic_cast in templates   |dynamic_cast, private
                   |causes core dump in gcc     |inheritance and friendship
                   |3.3.1 but not in gcc 2.95.3 |


------- Additional Comments From bangerth at dealii dot org  2003-09-10 01:12 -------
This one is actually much simpler than it looks like. Consider this
code:
-----------------------------
#include <cassert>

struct B {
    virtual ~B() {};
};

struct D : private B {
    friend class S;
};

struct S {
    static void f() {
      B *pB = new D();
      assert (dynamic_cast<D*>(pB) != 0);
    }
};

int main() {
  S::f ();
}
----------------------------
When compiled with 2.95, this code survives the assert, but it doesn't
with 3.x and icc. Now, gcc 2.95 is wrong. The point is that the
compiler must allow the attempt at the dynamic_cast since S is a friend
of D, so the base class of D is visible to S. However, the 
dynamic_cast then fails at run-time. And since this is exactly what
5.2.7/8 says, the testcase must yield the abort.


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

end of thread, other threads:[~2003-09-10  1:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-10  0:04 [Bug c++/12229] New: dynamic_cast in templates causes core dump in gcc 3.3.1 but not in gcc 2.95.3 shelly at InterraSystems dot com
2003-09-10  0:39 ` [Bug c++/12229] " bangerth at dealii dot org
2003-09-10  1:12 ` [Bug c++/12229] dynamic_cast, private inheritance and friendship bangerth at dealii dot 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).