From 3ab33d3ce9953ce891ecec63dfb521f711964568 Mon Sep 17 00:00:00 2001 From: rafallus Date: Tue, 30 Mar 2021 00:36:30 -0600 Subject: [PATCH] Check if _direct_state_changed() argument is valid - Modified classes: RigidBody, PhysicalBone, VehicleBody, RigidBody2D, KinematicBody2D - The input argument is untrusted even in release mode (cherry picked from commit e075b6b411f1c1a5cf63e7fb0e66104d6a62c33f) --- scene/2d/physics_body_2d.cpp | 8 ++------ scene/3d/physics_body.cpp | 15 +++------------ scene/3d/vehicle_body.cpp | 2 +- 3 files changed, 6 insertions(+), 19 deletions(-) diff --git a/scene/2d/physics_body_2d.cpp b/scene/2d/physics_body_2d.cpp index f62804eb539..f989a5c6733 100644 --- a/scene/2d/physics_body_2d.cpp +++ b/scene/2d/physics_body_2d.cpp @@ -440,12 +440,8 @@ bool RigidBody2D::_test_motion(const Vector2 &p_motion, bool p_infinite_inertia, } void RigidBody2D::_direct_state_changed(Object *p_state) { - -#ifdef DEBUG_ENABLED state = Object::cast_to(p_state); -#else - state = (Physics2DDirectBodyState *)p_state; //trust it -#endif + ERR_FAIL_COND_MSG(!state, "Method '_direct_state_changed' must receive a valid Physics2DDirectBodyState object as argument"); set_block_transform_notify(true); // don't want notify (would feedback loop) if (mode != MODE_KINEMATIC) @@ -1437,11 +1433,11 @@ bool KinematicBody2D::is_sync_to_physics_enabled() const { } void KinematicBody2D::_direct_state_changed(Object *p_state) { - if (!sync_to_physics) return; Physics2DDirectBodyState *state = Object::cast_to(p_state); + ERR_FAIL_COND_MSG(!state, "Method '_direct_state_changed' must receive a valid Physics2DDirectBodyState object as argument"); last_valid_transform = state->get_transform(); set_notify_local_transform(false); diff --git a/scene/3d/physics_body.cpp b/scene/3d/physics_body.cpp index 3bf0c57e36f..0d21fbe1b60 100644 --- a/scene/3d/physics_body.cpp +++ b/scene/3d/physics_body.cpp @@ -442,12 +442,8 @@ struct _RigidBodyInOut { }; void RigidBody::_direct_state_changed(Object *p_state) { - -#ifdef DEBUG_ENABLED state = Object::cast_to(p_state); -#else - state = (PhysicsDirectBodyState *)p_state; //trust it -#endif + ERR_FAIL_COND_MSG(!state, "Method '_direct_state_changed' must receive a valid PhysicsDirectBodyState object as argument"); set_ignore_transform_notification(true); set_global_transform(state->get_transform()); @@ -2220,13 +2216,8 @@ void PhysicalBone::_direct_state_changed(Object *p_state) { /// Update bone transform - PhysicsDirectBodyState *state; - -#ifdef DEBUG_ENABLED - state = Object::cast_to(p_state); -#else - state = (PhysicsDirectBodyState *)p_state; //trust it -#endif + PhysicsDirectBodyState *state = Object::cast_to(p_state); + ERR_FAIL_COND_MSG(!state, "Method '_direct_state_changed' must receive a valid PhysicsDirectBodyState object as argument"); Transform global_transform(state->get_transform()); diff --git a/scene/3d/vehicle_body.cpp b/scene/3d/vehicle_body.cpp index 8d3206e714c..8f18720c1db 100644 --- a/scene/3d/vehicle_body.cpp +++ b/scene/3d/vehicle_body.cpp @@ -857,10 +857,10 @@ void VehicleBody::_update_friction(PhysicsDirectBodyState *s) { } void VehicleBody::_direct_state_changed(Object *p_state) { - RigidBody::_direct_state_changed(p_state); state = Object::cast_to(p_state); + ERR_FAIL_COND_MSG(!state, "Method '_direct_state_changed' must receive a valid PhysicsDirectBodyState object as argument"); float step = state->get_step();