Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Fix crash when casting from null #53015

Merged
merged 1 commit into from
Sep 26, 2021

Conversation

timothyqiu
Copy link
Member

The following script crashes Godot 3.4 beta5:

onready var node := get_node_or_null("the-node-does-not-exist") as Control

It does not crash in previous betas (or after removing the "as Control" part).

The crash MRP: TestAs.zip


_get_obj().rc was not checked for null, so it would crash due to null pointer dereference.

Copy link
Member

@RandomShaper RandomShaper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your fix makes sense. However, I've realized that I could have shortened this function when validity checks were made universal. Please, replace the full function by this one, which contains your null check in a different way (and test it a bit 😃):

ObjectID Variant::get_object_instance_id() const {
	if (unlikely(type != OBJECT)) {
		return 0;
	} else if (likely(_get_obj().rc)) {
		return _get_obj().rc->instance_id;
	} else if (likely(!_get_obj().ref.is_null())) {
		return _REF_OBJ_PTR(*this)->get_instance_id();
	} else {
		return 0;
	}
}

@akien-mga akien-mga merged commit 8fb06b4 into godotengine:3.x Sep 26, 2021
@akien-mga
Copy link
Member

Thanks!

@timothyqiu timothyqiu deleted the cast-crash branch September 26, 2021 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants