public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/35898]  New: C++ exception bug at run time when mutually recursive functions
@ 2008-04-10  3:23 vania at liama dot ia dot ac dot cn
  2008-04-10  3:34 ` [Bug c++/35898] " vania at liama dot ia dot ac dot cn
  2008-04-10 10:40 ` rguenth at gcc dot gnu dot org
  0 siblings, 2 replies; 3+ messages in thread
From: vania at liama dot ia dot ac dot cn @ 2008-04-10  3:23 UTC (permalink / raw)
  To: gcc-bugs

Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--with-gxx-include-dir=/usr/include/c++/4.1.3 --program-suffix=-4.1
--enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug
--enable-mpfr --enable-checking=release x86_64-linux-gnu
Thread model: posix
gcc version 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)

The following is a C++ program that shows the problem.
It is a program that always end up with an exception sooner or later
based on the input value...

Compilation with -ansi -Wall works fine and report no errors and warning.
First run the program with input '1000' (argument on command line). 
It works fine. The exception is caught properly.
Run the program with input 1. The program aborts because the exception
is thrown but not caught.

The program has 4 functions which can be mutually recursive based on the input
value. When there is mutual recursion between only 2 functions it works fine,
the exception is thrown and the appropriate catch clause operates.
When there is mutual recursion between more than 2 functions, when the
exception
is thrown, it not caught as it should and it goes into terminate...

#include <iostream>
#include <cstdlib>

using namespace std;

unsigned int seed;

unsigned int my_random() {
  unsigned int low = 16807 * (seed & 0xffff);
  unsigned int high = 16807 * (seed >> 16);
  low += (high &0x7FFF) << 16;
  low += high >> 15;
  if (low > 0x7FFFFFFF)
    low -= 0x7FFFFFFF;
  return (seed = low);
}

const unsigned int small_max = 0xFFF;
const unsigned int medium_max = 0xFFFFF;

unsigned int small_size = 0;
unsigned int medium_size = small_max;
unsigned int large_size = medium_max;

void dummy_test();

class MyException {
public:
  const char *msg;
  MyException(const char * s):msg(s){};

};
// MyException::MyException(const char *s):msg(s){};

class SmallException: public MyException {
public:
  SmallException(const char *s, int r);
  int small;
};
SmallException::SmallException(const char * s, int r): MyException(s)
{
  small = r;
}

class MediumException: public MyException {
public:
  MediumException(const char * s, int r);
  int medium;
};
MediumException::MediumException(const char * s, int r): MyException(s) {
  medium = r;
}

class LargeException: public MyException {
public:
  LargeException(const char *s, int r);
  int large;
};
LargeException::LargeException(const char *s, int r): MyException(s) {
  large = r;
}

void small(unsigned int v) throw (SmallException &) {
  small_size = small_size << 1;
  cout << "small ? " << v << endl;
  if (small_size >= v)
    throw SmallException("small test fails : ", v);
  else
    dummy_test();
}

void medium(unsigned int v) throw (MediumException &) {
  medium_size = medium_size << 1;
  cout << "medium ? " << v << endl;
  if (medium_size >= v)
    throw MediumException("medium test fails : ", v);
  else
    dummy_test();
}

void large(unsigned int v) throw (LargeException &) {
  large_size =  large_size << 1;
  cout << "large ? " << v << endl;
  if (large_size >= v)
    throw LargeException("large test fails : ", v);
  else
    dummy_test();
}

void dummy_test() {
  unsigned int v = my_random();

  if (v < small_max)
    small(v);
  else if (v > medium_max)
    large(v);
  else
    medium(v);
}

void test_exception(){
  try {
    dummy_test();
  }
  catch ( SmallException &e) {
    cout << "Exception -- " << e.msg << e.small 
         << endl;
  } 
  catch ( MediumException &e ) {
    cout << "Exception -- " << e.msg << e.medium
         <<  endl;
  } 
  catch ( LargeException &e) {
    cout << "Exception -- " << e.msg << e.large 
         << endl;
  } 
  catch (...) {
    cout << "Unspecified exception type\n" << endl;
  };
}

int main(int argc, char* argv[]) {

  if (argc < 2 || (seed = atoi(argv[1])) == 0) {
    cout << "Must have valid number argument" << endl;
    return 0;
  }

  cout << "start test with seed " << seed << endl;
  test_exception();
  cout << "test ending normally " << endl;
  return 0;
}


-- 
           Summary: C++ exception bug at run time when mutually recursive
                    functions
           Product: gcc
           Version: 4.1.3
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: vania at liama dot ia dot ac dot cn
  GCC host triplet: x86_64 GNU/Linux 2.6.22-14-


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


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

* [Bug c++/35898] C++ exception bug at run time when mutually recursive functions
  2008-04-10  3:23 [Bug c++/35898] New: C++ exception bug at run time when mutually recursive functions vania at liama dot ia dot ac dot cn
@ 2008-04-10  3:34 ` vania at liama dot ia dot ac dot cn
  2008-04-10 10:40 ` rguenth at gcc dot gnu dot org
  1 sibling, 0 replies; 3+ messages in thread
From: vania at liama dot ia dot ac dot cn @ 2008-04-10  3:34 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from vania at liama dot ia dot ac dot cn  2008-04-10 03:34 -------
I have a simplified version of this program that works fine however !!!
When the exception thrown is an int instead of a class instance, everything
works fine!


-- 

vania at liama dot ia dot ac dot cn changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |vania at liama dot ia dot ac
                   |                            |dot cn
              Alias|                            |C++exception_catch
           Keywords|                            |sjlj-eh


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


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

* [Bug c++/35898] C++ exception bug at run time when mutually recursive functions
  2008-04-10  3:23 [Bug c++/35898] New: C++ exception bug at run time when mutually recursive functions vania at liama dot ia dot ac dot cn
  2008-04-10  3:34 ` [Bug c++/35898] " vania at liama dot ia dot ac dot cn
@ 2008-04-10 10:40 ` rguenth at gcc dot gnu dot org
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2008-04-10 10:40 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from rguenth at gcc dot gnu dot org  2008-04-10 10:40 -------
Your exception specification on small, medium and large makes throws from the
recursively called dummy_test bogus.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


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


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

end of thread, other threads:[~2008-04-10 10:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-10  3:23 [Bug c++/35898] New: C++ exception bug at run time when mutually recursive functions vania at liama dot ia dot ac dot cn
2008-04-10  3:34 ` [Bug c++/35898] " vania at liama dot ia dot ac dot cn
2008-04-10 10:40 ` rguenth at gcc dot gnu 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).