From be5a012ff7f3a4c83613ad0ef9bb8d675be70d2c Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Sat, 24 Nov 2018 17:36:27 +0200 Subject: [PATCH] Fix NULL dereferencing in get_from_variant and cast_to --- binding_generator.py | 2 +- include/core/Godot.hpp | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/binding_generator.py b/binding_generator.py index 16db7eb9..9bb8dd59 100644 --- a/binding_generator.py +++ b/binding_generator.py @@ -155,7 +155,7 @@ def generate_class_header(used_classes, c): # ___get_class_name source.append("\tstatic inline const char *___get_class_name() { return (const char *) \"" + strip_name(c["name"]) + "\"; }") - source.append("\tstatic inline Object *___get_from_variant(Variant a) { godot_object *o = (godot_object*) a; return (Object *) godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, o); }") + source.append("\tstatic inline Object *___get_from_variant(Variant a) { godot_object *o = (godot_object*) a; return (o) ? (Object *) godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, o) : nullptr; }") enum_values = [] diff --git a/include/core/Godot.hpp b/include/core/Godot.hpp index a09310e3..414a48ad 100644 --- a/include/core/Godot.hpp +++ b/include/core/Godot.hpp @@ -24,7 +24,7 @@ namespace godot { template T *as(const Object *obj) { - return (T *)godot::nativescript_api->godot_nativescript_get_userdata(obj->_owner); + return (obj) ? (T *)godot::nativescript_api->godot_nativescript_get_userdata(obj->_owner) : nullptr; } template @@ -428,6 +428,9 @@ void register_signal(String name, Args... varargs) { #ifndef GODOT_CPP_NO_OBJECT_CAST template T *Object::cast_to(const Object *obj) { + if (!obj) + return nullptr; + size_t have_tag = (size_t)godot::nativescript_1_1_api->godot_nativescript_get_type_tag(obj->_owner); if (have_tag) {