Skip to content

Commit

Permalink
Check if _direct_state_changed() argument is valid
Browse files Browse the repository at this point in the history
- Modified classes: RigidBody, PhysicalBone, VehicleBody, RigidBody2D, KinematicBody2D
- The input argument is untrusted even in release mode
  • Loading branch information
rafallus committed Apr 8, 2021
1 parent 8dd6fd0 commit e075b6b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 19 deletions.
8 changes: 2 additions & 6 deletions scene/2d/physics_body_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Physics2DDirectBodyState>(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)
Expand Down Expand Up @@ -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<Physics2DDirectBodyState>(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);
Expand Down
15 changes: 3 additions & 12 deletions scene/3d/physics_body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,8 @@ struct _RigidBodyInOut {
};

void RigidBody::_direct_state_changed(Object *p_state) {

#ifdef DEBUG_ENABLED
state = Object::cast_to<PhysicsDirectBodyState>(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());
Expand Down Expand Up @@ -2220,13 +2216,8 @@ void PhysicalBone::_direct_state_changed(Object *p_state) {

/// Update bone transform

PhysicsDirectBodyState *state;

#ifdef DEBUG_ENABLED
state = Object::cast_to<PhysicsDirectBodyState>(p_state);
#else
state = (PhysicsDirectBodyState *)p_state; //trust it
#endif
PhysicsDirectBodyState *state = Object::cast_to<PhysicsDirectBodyState>(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());

Expand Down
2 changes: 1 addition & 1 deletion scene/3d/vehicle_body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PhysicsDirectBodyState>(p_state);
ERR_FAIL_COND_MSG(!state, "Method '_direct_state_changed' must receive a valid PhysicsDirectBodyState object as argument");

float step = state->get_step();

Expand Down

0 comments on commit e075b6b

Please sign in to comment.