From 8a47fe9eb05a3a05ebd6430d47ba4fdc56e0a91a Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Fri, 24 Sep 2021 23:31:17 +0800 Subject: [PATCH] Fix crash when casting from null --- core/variant.cpp | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/core/variant.cpp b/core/variant.cpp index c7851fb837d0..093e0667f5f4 100644 --- a/core/variant.cpp +++ b/core/variant.cpp @@ -804,22 +804,15 @@ bool Variant::is_one() const { } ObjectID Variant::get_object_instance_id() const { - if (type != OBJECT) { + if (unlikely(type != OBJECT)) { return 0; - } -#ifdef DEBUG_ENABLED - if (is_ref()) { - return !_get_obj().ref.is_null() ? _REF_OBJ_PTR(*this)->get_instance_id() : 0; - } else { + } else if (likely(_get_obj().rc)) { return _get_obj().rc->instance_id; - } -#else - if (is_ref() && _get_obj().ref.is_null()) { - return 0; + } else if (likely(!_get_obj().ref.is_null())) { + return _REF_OBJ_PTR(*this)->get_instance_id(); } else { - return _get_obj().rc->get_ptr()->get_instance_id(); + return 0; } -#endif } bool Variant::is_invalid_object() const {