Bug in exception handling with -O
Peter Schmid
schmid@ltoi.iap.physik.tu-darmstadt.de
Tue Nov 11 04:20:00 GMT 1997
This program works without optimization but crashes with -O.
I tried it on a up to date linux-system (libc.so.5.4.40 and
binutils-2.8.0.1.15) and an older (libc.so.5.4.4 and binutils-2.7.0.3)
both give the same output.
cat faq-268.C
//This is the source code from FAQ-268, found in chapter 21 of "C++ FAQs."
//Copyright (C) 1994, Addison-Wesley Publishers, Inc.; All rights reserved.
//
//The book, "C++ FAQs" is by Marshall P. Cline and Greg A. Lomow,
//Copyright (C) 1994, Addison-Wesley Publishers, Inc.; All rights reserved.
//
//This code is presented for its instructional value. It has been tested with
//care, but it is not guaranteed for any particular purpose. Neither the
//publisher nor the authors offer any warranties or representations, nor do
//they accept any liabilities with respect to this code.
#include <iostream.h>
void fred() throw(const char*)
{ cout << "fred() throwing\n"; throw "barney"; }
class X {
public:
~X() throw();
};
X::~X() throw()
{
try {
fred();
}
catch (const char* msg) {
cout << "handling fred()'s exception: " << msg << '\n';
}
}
main()
{
try {
X x;
cout << "main() throwing\n";
throw "main";
}
catch (const char* msg) {
cout << "handling main()'s exception: " << msg << '\n';
}
}
g++ -o faq-268 faq-268.C
./faq-268
main() throwing
fred() throwing
handling fred()'s exception: barney
handling main()'s exception: main
g++ -v -O -g -o faq-268 faq-268.C -W -Wall
Reading specs from /usr/local/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.90.16/specs
gcc version egcs-2.90.16 971105 (gcc2-970802 experimental)
/usr/local/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.90.16/cpp -lang-c++ -v -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus -D__GNUC_MINOR__=90 -D__ELF__ -Dunix -Dlinux -D__ELF__ -D__unix__ -D__linux__ -D__unix -D__linux -Asystem(posix) -D__EXCEPTIONS -D_
_OPTIMIZE__ -g -W -Wall -Di386 -Di586 -Asystem(unix) -Acpu(i386) -Amachine(i386) -D__i386__ -D__i586__ -Asystem(unix) -Acpu(i386) -Amachine(i386) faq-268.C /tmp/cca28808.ii
GNU CPP version egcs-2.90.16 971105 (gcc2-970802 experimental) (i386 Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include/g++
/usr/local/include
/usr/local/i586-pc-linux-gnulibc1/include
/usr/local/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.90.16/include
/usr/include
End of search list.
/usr/local/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.90.16/cc1plus /tmp/cca28808.ii -quiet -dumpbase faq-268.cc -g -O -W -Wall -version -o /tmp/cca28808.s
GNU C++ version egcs-2.90.16 971105 (gcc2-970802 experimental) (i586-pc-linux-gnulibc1) compiled by GNU C version egcs-2.90.16 971105 (gcc2-970802 experimental).
faq-268.C: In function `int main()':
faq-268.C:35: warning: unused variable `class X x'
as -V -Qy -o /tmp/cca288081.o /tmp/cca28808.s
GNU assembler version 2.7 (i486-linux), using BFD version 2.7.0.3
/usr/local/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.90.16/ld -m elf_i386 -dynamic-linker /lib/ld-linux.so.1 -o faq-268 /usr/lib/crt1.o /usr/lib/crti.o /usr/local/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.90.16/crtbegin.o -L/usr/local/lib/gcc-lib/i586-pc
-linux-gnulibc1/egcs-2.90.16 -L/usr/local/i586-pc-linux-gnulibc1/lib -L/usr/local/lib /tmp/cca288081.o -lstdc++ -lm -lgcc -lc -lgcc /usr/local/lib/gcc-lib/i586-pc-linux-gnulibc1/egcs-2.90.16/crtend.o /usr/lib/crtn.o
./faq-268
main() throwing
fred() throwing
handling fred()'s exception: barney
IOT trap/Abort (core dumped)
gdb -c core ./faq-268
GDB is free software and you are welcome to distribute copies of it
under certain conditions; type "show copying" to see the conditions.
There is absolutely no warranty for GDB; type "show warranty" for details.
GDB 4.16 (i486-unknown-linux --target i486-linux),
Copyright 1996 Free Software Foundation, Inc...
Core was generated by `./faq-268'.
Program terminated with signal 6, IOT trap/Abort.
Reading symbols from /lib/libm.so.5.0.6...done.
Reading symbols from /lib/libc.so.5.4.7...done.
Reading symbols from /lib/ld-linux.so.1...done.
#0 0x40078a45 in __kill ()
(gdb) where
#0 0x40078a45 in __kill ()
#1 0x40047e5d in raise (sig=134589216)
#2 0x804f96e in __terminate ()
#3 0x804ff49 in __throw ()
#4 0x804a2a2 in main () at faq-268.C:38
#5 0x804a00b in ___crt_dummy__ ()
Peter Schmid
More information about the Gcc-bugs
mailing list