public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Martin Sebor <msebor@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r11-7350] PR c++/99074 - crash in dynamic_cast<>() on null pointer Date: Tue, 23 Feb 2021 21:14:40 +0000 (GMT) [thread overview] Message-ID: <20210223211440.886C3389246E@sourceware.org> (raw) https://gcc.gnu.org/g:6d134ca4b963706f31251f061fc180e517b32546 commit r11-7350-g6d134ca4b963706f31251f061fc180e517b32546 Author: Martin Sebor <msebor@redhat.com> Date: Tue Feb 23 14:09:00 2021 -0700 PR c++/99074 - crash in dynamic_cast<>() on null pointer libstdc++-v3/ChangeLog: PR c++/99074 * libsupc++/dyncast.cc (__dynamic_cast): Return null when first argument is null. gcc/testsuite/ChangeLog: PR c++/99074 * g++.dg/warn/Wnonnull11.C: New test. Diff: --- gcc/testsuite/g++.dg/warn/Wnonnull11.C | 40 ++++++++++++++++++++++++++++++++++ libstdc++-v3/libsupc++/dyncast.cc | 3 +++ 2 files changed, 43 insertions(+) diff --git a/gcc/testsuite/g++.dg/warn/Wnonnull11.C b/gcc/testsuite/g++.dg/warn/Wnonnull11.C new file mode 100644 index 00000000000..5f1b69d9a41 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wnonnull11.C @@ -0,0 +1,40 @@ +/* PR c++/99074 - gcc 8 and above is crashing with dynamic_cast<>() on null + pointer with optimization level -O1 and above + { dg-do run } + { dg-options "-O1 -Wall" } */ + +class Base +{ +public: + virtual ~Base() {} + virtual void op() = 0; +}; + +class Object: public virtual Base { }; + +class AbstractBase: public virtual Base +{ +public: + Object* _to_object () + { + return dynamic_cast<Object*>(this); // { dg-warning "\\\[-Wnonnull" "" { xfail *-*-* } } + } +}; + +class MyAbstractClass: public virtual AbstractBase +{ +public: + static MyAbstractClass* _nil () { return 0; } +}; + + +int main () +{ + MyAbstractClass *my_abs_type = MyAbstractClass::_nil (); + AbstractBase *abs_base = my_abs_type; + Object *obj = abs_base->_to_object (); + + __builtin_printf ("object is: %p\n", obj); + + return 0; +} diff --git a/libstdc++-v3/libsupc++/dyncast.cc b/libstdc++-v3/libsupc++/dyncast.cc index b7d98495ad3..f8f707ee4d4 100644 --- a/libstdc++-v3/libsupc++/dyncast.cc +++ b/libstdc++-v3/libsupc++/dyncast.cc @@ -47,6 +47,9 @@ __dynamic_cast (const void *src_ptr, // object started from const __class_type_info *dst_type, // desired target type ptrdiff_t src2dst) // how src and dst are related { + if (!src_ptr) + /* Handle precondition violations gracefully. */ + return NULL; const void *vtable = *static_cast <const void *const *> (src_ptr); const vtable_prefix *prefix = (adjust_pointer <vtable_prefix>
reply other threads:[~2021-02-23 21:14 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=20210223211440.886C3389246E@sourceware.org \ --to=msebor@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@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: linkBe 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).