Fix potential nullptr dereference in CanvasLayer

* Changed another instance of ERR_FAIL_COND in the same file to
  ERR_FAIL_NULL_MSG instead.
* Checked for potential access of the viewport pointer elsewhere in same
  file.

Fixes #54098

(cherry picked from commit 497b00a937)
This commit is contained in:
Sean Kim 2021-10-21 19:36:02 -07:00 committed by Rémi Verschelde
parent fa2feb9bec
commit 378ca68dc1
No known key found for this signature in database
GPG Key ID: C3336907360768E1
1 changed files with 6 additions and 2 deletions

View File

@ -136,7 +136,7 @@ void CanvasLayer::_notification(int p_what) {
} else { } else {
vp = Node::get_viewport(); vp = Node::get_viewport();
} }
ERR_FAIL_COND(!vp); ERR_FAIL_NULL_MSG(vp, "Viewport is not initialized.");
vp->_canvas_layer_add(this); vp->_canvas_layer_add(this);
viewport = vp->get_viewport_rid(); viewport = vp->get_viewport_rid();
@ -148,6 +148,8 @@ void CanvasLayer::_notification(int p_what) {
} break; } break;
case NOTIFICATION_EXIT_TREE: { case NOTIFICATION_EXIT_TREE: {
ERR_FAIL_NULL_MSG(vp, "Viewport is not initialized.");
vp->_canvas_layer_remove(this); vp->_canvas_layer_remove(this);
VisualServer::get_singleton()->viewport_remove_canvas(viewport, canvas); VisualServer::get_singleton()->viewport_remove_canvas(viewport, canvas);
viewport = RID(); viewport = RID();
@ -168,6 +170,8 @@ Size2 CanvasLayer::get_viewport_size() const {
return Size2(1, 1); return Size2(1, 1);
} }
ERR_FAIL_NULL_V_MSG(vp, Size2(1, 1), "Viewport is not initialized.");
Rect2 r = vp->get_visible_rect(); Rect2 r = vp->get_visible_rect();
return r.size; return r.size;
} }
@ -177,7 +181,7 @@ RID CanvasLayer::get_viewport() const {
} }
void CanvasLayer::set_custom_viewport(Node *p_viewport) { void CanvasLayer::set_custom_viewport(Node *p_viewport) {
ERR_FAIL_NULL(p_viewport); ERR_FAIL_NULL_MSG(p_viewport, "Cannot set viewport to nullptr.");
if (is_inside_tree()) { if (is_inside_tree()) {
vp->_canvas_layer_remove(this); vp->_canvas_layer_remove(this);
VisualServer::get_singleton()->viewport_remove_canvas(viewport, canvas); VisualServer::get_singleton()->viewport_remove_canvas(viewport, canvas);