public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
From: "Uwe F. Mayer" <mayer@tux.org>
To: <gcc-gnats@gcc.gnu.org>
Subject: c++/2659: bad_cast exception not thrown
Date: Thu, 26 Apr 2001 13:56:00 -0000	[thread overview]
Message-ID: <Pine.LNX.4.21.0104261254030.21393-100000@tosca.localnet> (raw)

>Number:         2659
>Category:       c++
>Synopsis:       bad_cast exception not thrown
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Apr 26 13:56:01 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     Uwe F. Mayer
>Release:        3.0 20010409 (prerelease)
>Organization:
tux.org
>Environment:
System: Linux tosca 2.2.18 #2 Thu Mar 8 13:48:25 PST 2001 i686 unknown
Architecture: i686
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ../gcc/configure --prefix=/usr/local/gcc --disable-nls
>Description:
g++ does not throw a bad_cast exception when a base-class pointer is
dynamically cast to a derived-class pointer, which of course is illegal.
It does, however, return a NULL pointer which was the old style error
condition detection.
>How-To-Repeat:
Compile this program with "g++ bad_cast.cpp" and run it.
Gives a Segmentation fault.
Source code below:

/**********************************************************************

  Filename:  bad_cast.cpp
  Chapter:   9      Exceptions
  Compiler:  Borland C++     Version 5.01      Summer 1998
  C++ for C Programmers, Edition 3     By Ira Pohl

********************************************************************/

#include <iostream>
#include <exception>
#include <typeinfo>
using namespace std;

class A {
public:
  virtual void foo() { cout << "in A" << endl; }
};

class B: public A {
public:
  void foo() { cout << "in B" << endl; }
};

int main()
{
   try {
      A a, *pa; B b, *pb;
      pa = &b;
      pb = dynamic_cast<B*>(pa);      //succeeds
      pb -> foo();
      pa = &a;
      pb = dynamic_cast<B*>(pa);      //fails
      // if (pb==NULL) {bad_cast x; throw x; } // my g++ does not throw it
      pb -> foo();
   }
   catch(bad_cast) { cout << "dynamic_cast failed" << endl; }
}
>Fix:
Work around: Use the old style NULL-pointer test and throw the exception
yourself.



>Release-Note:
>Audit-Trail:
>Unformatted:


                 reply	other threads:[~2001-04-26 13:56 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.21.0104261254030.21393-100000@tosca.localnet \
    --to=mayer@tux.org \
    --cc=gcc-gnats@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).