standards/175453: Catching C++ std::bad_cast doesn't work in FreeBSD 9.1

Hongli Lai hongli at
Sun Jan 20 17:00:01 UTC 2013

>Number:         175453
>Category:       standards
>Synopsis:       Catching C++ std::bad_cast doesn't work in FreeBSD 9.1
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-standards
>State:          open
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Jan 20 17:00:00 UTC 2013
>Originator:     Hongli Lai
>Release:        9.1-RELEASE
FreeBSD freebsd9 9.1-RELEASE FreeBSD 9.1-RELEASE #0 r243825: Tue Dec  4 09:23:10 UTC 2012     
root at  amd64
C++ code is not able to catch std::bad_cast exceptions, even though it should. If a dynamic_cast is within a try-catch block, then that block fails to catch std::bad_cast, and the program crashes with an uncaught exception as a result.

I've attached a reproducible test case. You can also find it at and The code is compiled with the following GCC version:

$ gcc -v
Using built-in specs.
Target: amd64-undermydesk-freebsd
Configured with: FreeBSD/amd64 system compiler
Thread model: posix
gcc version 4.2.1 20070831 patched [FreeBSD]

FreeBSD 9.1 seems to be the only platform on which this bug appears. The code works as expected on Linux and OS X. According to a commenter, FreeBSD 9.0 works as expected too. According to another commenter the code fails on FreeBSD 9.1 with Clang too.
See attached C++ program.

Patch attached with submission follows:

#include <exception>
#include <typeinfo>
#include <stdio.h>

class foo {
    virtual ~foo() {}

class bar: public foo {
    int val;
    bar(): val(123) {}

static void
cast_test(const foo &f) {
    try {
        const bar &b = dynamic_cast<const bar &>(f);
        printf("%d\n", b.val);
    } catch (const std::bad_cast &) {
        printf("bad cast\n");

int main() {
    foo f;
    return 0;


More information about the freebsd-standards mailing list