[cfe-dev] Segfault on uncaught exception

Thomas Sanchez thomas.sanchz at gmail.com
Sat Jul 30 18:10:57 PDT 2011


Hi,
I was playing with clang++ a little and I had a surprise with the exceptions.

Here is an example of the code:
#include <exception>

class myexception : public std::exception
{
 public:
    myexception() throw() {}
    virtual ~ myexception() throw() {}

    const char *what() const throw()
    {
        return "this is a good reason";
    }
};

int main()
{
    myexception ex;
    throw ex;
}

This cause a segfault on:
  * Mas OS X (Lion): Apple clang version 2.1
(tags/Apple/clang-163.7.1) (based on LLVM 3.0svn)
  * Linux (archlinux): clang version 2.9 (tags/RELEASE_29/final)

Here is an helpless (for me) stack trace:
#0  0x00007ffff7dc19d0 in ?? () from /usr/lib/libstdc++.so.6
#1  0x0000000100400ae5 in ?? ()
#2  0x0000000000402090 in ?? ()
#3  0x00007fffffffe8f0 in ?? ()
#4  0x0000000000000000 in ?? ()

When I modify the code a little and add some debug:

#include <iostream>
#include <exception>

class myexception:public std::exception
{
  public:
    myexception() throw()
    {
        std::cout << "Create instance of myexception at: " << this << std::endl;
    }

    myexception(const myexception& e) throw()
    {

        std::cout << "Copy instance: " << &e << ", to: " << this << std::endl;
    }

    virtual ~myexception() throw()
    {
        std::cout << "Destruct instance of myexception at: " << this
<< std::endl;
    }

    virtual const char *what() const throw()
    {
      return "this is a good reason";
    }
};


void test()// throw (const myexception&)
{
    myexception ex;
    throw ex;
}

int main()
{
    test();
    return 0;
}

I have (on linux and mac os x):
$>./test | head
Create instance of myexception at: 0x7fff6eba9a48
Copy instance: 0x7fff6eba9a48, to: 0x10f000988
Destruct instance of myexception at: 0x7fff6eba9a48
Create instance of myexception at: 0x7fff6eba99f0
Copy instance: 0x7fff6eba99f0, to: 0x10f000a18
Destruct instance of myexception at: 0x7fff6eba99f0
Create instance of myexception at: 0x7fff6eba9998
Copy instance: 0x7fff6eba9998, to: 0x10f000a98
Destruct instance of myexception at: 0x7fff6eba9998
Create instance of myexception at: 0x7fff6eba9940
[..........]
$> ./test | wc -l
  285647
zsh: segmentation fault  ./test |

When I comment out the throw specifier it works the expected way:
  Linux:
    $> ./test
    Create instance of myexception at: 0x7fff14b0d8c8
    Copy instance: 0x7fff14b0d8c8, to: 0x1ffd090
    Destruct instance of myexception at: 0x7fff14b0d8c8
    terminate called after throwing an instance of 'myexception'
      what():  this is a good reason
    zsh: abort      ./test

  Mac OS X:
    $> ./test
    Create instance of myexception at: 0x7fff69e9ea48
    Copy instance: 0x7fff69e9ea48, to: 0x10a300988
    Destruct instance of myexception at: 0x7fff69e9ea48
    terminate called throwing an exceptionzsh: abort      ./test

According to the standard 15.4.11, a function without
exception-specification allows all exceptions and
according to the section 15.5.1, an uncaught function have to call terminate().

I've looked on google, and I did not see this error reported so I may
have missed something :)
Thank you!

-- 
Thomas Sanchez



More information about the cfe-dev mailing list