From a854de787f511b8292f1f066efbddd78dc71a1e6 Mon Sep 17 00:00:00 2001 From: Di Saber <37475791+DiSaber@users.noreply.github.com> Date: Sun, 23 Jun 2024 16:26:08 -0500 Subject: [PATCH] Fix `attempt to subtract with overflow` panic in `SAPRegion::update_after_subregion_removal()` (#663) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Swap a regular subtraction for a saturating subtraction * chore: display a debug message if the SAP reach an unexpected state regarding sub-proper proxies removal. --------- Co-authored-by: Sébastien Crozet --- src/geometry/broad_phase_multi_sap/sap_region.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/geometry/broad_phase_multi_sap/sap_region.rs b/src/geometry/broad_phase_multi_sap/sap_region.rs index 7e38eaaa0..f54b4aeb5 100644 --- a/src/geometry/broad_phase_multi_sap/sap_region.rs +++ b/src/geometry/broad_phase_multi_sap/sap_region.rs @@ -192,12 +192,23 @@ impl SAPRegion { pub fn update_after_subregion_removal(&mut self, proxies: &SAPProxies, layer_depth: i8) { if self.needs_update_after_subregion_removal { for axis in &mut self.axes { - self.subproper_proxy_count -= axis + let removed_count = axis .delete_deleted_proxies_and_endpoints_after_subregion_removal( proxies, &mut self.existing_proxies, layer_depth, ); + + if removed_count > self.subproper_proxy_count { + log::debug!( + "Reached unexpected state: attempted to remove more sub-proper proxies than added (removing: {}, total: {}).", + removed_count, + self.subproper_proxy_count + ); + self.subproper_proxy_count = 0; + } else { + self.subproper_proxy_count -= removed_count; + } } self.needs_update_after_subregion_removal = false; }