From c6ceef137ec8dcb83b6a61bcab093c8b49cb6bda Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Wed, 1 Jan 2025 17:20:07 -0800 Subject: [PATCH] Place a hard limit on the max_contacts_reported property --- modules/godot_physics_2d/godot_body_2d.h | 1 + modules/godot_physics_3d/godot_body_3d.h | 1 + modules/jolt_physics/objects/jolt_body_3d.cpp | 2 +- scene/2d/physics/rigid_body_2d.cpp | 1 + scene/3d/physics/rigid_body_3d.cpp | 1 + servers/physics_server_2d.h | 2 ++ servers/physics_server_3d.h | 2 ++ 7 files changed, 9 insertions(+), 1 deletion(-) diff --git a/modules/godot_physics_2d/godot_body_2d.h b/modules/godot_physics_2d/godot_body_2d.h index 529305dbb2f8..ec4b898e2b6a 100644 --- a/modules/godot_physics_2d/godot_body_2d.h +++ b/modules/godot_physics_2d/godot_body_2d.h @@ -182,6 +182,7 @@ class GodotBody2D : public GodotCollisionObject2D { } _FORCE_INLINE_ void set_max_contacts_reported(int p_size) { + ERR_FAIL_INDEX(p_size, MAX_CONTACTS_REPORTED_2D_MAX); contacts.resize(p_size); contact_count = 0; if (mode == PhysicsServer2D::BODY_MODE_KINEMATIC && p_size) { diff --git a/modules/godot_physics_3d/godot_body_3d.h b/modules/godot_physics_3d/godot_body_3d.h index 81b668122a60..a591f6177abc 100644 --- a/modules/godot_physics_3d/godot_body_3d.h +++ b/modules/godot_physics_3d/godot_body_3d.h @@ -176,6 +176,7 @@ class GodotBody3D : public GodotCollisionObject3D { } _FORCE_INLINE_ void set_max_contacts_reported(int p_size) { + ERR_FAIL_INDEX(p_size, MAX_CONTACTS_REPORTED_3D_MAX); contacts.resize(p_size); contact_count = 0; if (mode == PhysicsServer3D::BODY_MODE_KINEMATIC && p_size) { diff --git a/modules/jolt_physics/objects/jolt_body_3d.cpp b/modules/jolt_physics/objects/jolt_body_3d.cpp index 59ba42c6ca91..b630a359c6b1 100644 --- a/modules/jolt_physics/objects/jolt_body_3d.cpp +++ b/modules/jolt_physics/objects/jolt_body_3d.cpp @@ -883,7 +883,7 @@ void JoltBody3D::set_center_of_mass_custom(const Vector3 &p_center_of_mass) { } void JoltBody3D::set_max_contacts_reported(int p_count) { - ERR_FAIL_COND(p_count < 0); + ERR_FAIL_INDEX(p_count, MAX_CONTACTS_REPORTED_3D_MAX); if (unlikely((int)contacts.size() == p_count)) { return; diff --git a/scene/2d/physics/rigid_body_2d.cpp b/scene/2d/physics/rigid_body_2d.cpp index 4b1cde6b7af7..4639dbd686a9 100644 --- a/scene/2d/physics/rigid_body_2d.cpp +++ b/scene/2d/physics/rigid_body_2d.cpp @@ -498,6 +498,7 @@ bool RigidBody2D::is_sleeping() const { } void RigidBody2D::set_max_contacts_reported(int p_amount) { + ERR_FAIL_INDEX_MSG(p_amount, MAX_CONTACTS_REPORTED_2D_MAX, "Max contacts reported allocates memory (about 100 bytes each), and therefore must not be set too high."); max_contacts_reported = p_amount; PhysicsServer2D::get_singleton()->body_set_max_contacts_reported(get_rid(), p_amount); } diff --git a/scene/3d/physics/rigid_body_3d.cpp b/scene/3d/physics/rigid_body_3d.cpp index 6c8cea1ac391..7c7b950706d3 100644 --- a/scene/3d/physics/rigid_body_3d.cpp +++ b/scene/3d/physics/rigid_body_3d.cpp @@ -521,6 +521,7 @@ bool RigidBody3D::is_sleeping() const { } void RigidBody3D::set_max_contacts_reported(int p_amount) { + ERR_FAIL_INDEX_MSG(p_amount, MAX_CONTACTS_REPORTED_3D_MAX, "Max contacts reported allocates memory (about 80 bytes each), and therefore must not be set too high."); max_contacts_reported = p_amount; PhysicsServer3D::get_singleton()->body_set_max_contacts_reported(get_rid(), p_amount); } diff --git a/servers/physics_server_2d.h b/servers/physics_server_2d.h index 2e7839dd0a10..f29d38587fa0 100644 --- a/servers/physics_server_2d.h +++ b/servers/physics_server_2d.h @@ -35,6 +35,8 @@ #include "core/object/class_db.h" #include "core/object/ref_counted.h" +constexpr int MAX_CONTACTS_REPORTED_2D_MAX = 100'000'000; + class PhysicsDirectSpaceState2D; template class TypedArray; diff --git a/servers/physics_server_3d.h b/servers/physics_server_3d.h index 61e62aed1480..2e427004e468 100644 --- a/servers/physics_server_3d.h +++ b/servers/physics_server_3d.h @@ -36,6 +36,8 @@ #include "core/io/resource.h" #include "core/object/gdvirtual.gen.inc" +constexpr int MAX_CONTACTS_REPORTED_3D_MAX = 100'000'000; + class PhysicsDirectSpaceState3D; template class TypedArray;