From d35de22405ef9d91f76cc61471c1dc1181def9f3 Mon Sep 17 00:00:00 2001 From: Katherine Kiefer Date: Sat, 30 Mar 2024 21:32:45 +1100 Subject: [PATCH] reserve this --- crates/byondapi-rs/src/map.rs | 4 +++- crates/byondapi-rs/src/value/functions.rs | 4 +++- crates/byondapi-rs/src/value/list.rs | 8 ++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/byondapi-rs/src/map.rs b/crates/byondapi-rs/src/map.rs index d46fdfe..e559192 100644 --- a/crates/byondapi-rs/src/map.rs +++ b/crates/byondapi-rs/src/map.rs @@ -38,6 +38,7 @@ pub fn byond_block(corner1: ByondXYZ, corner2: ByondXYZ) -> Result Result, Error> { + let initial_len = buff.capacity() as u32; let mut len = buff.capacity() as u32; // Safety: buffer capacity is passed to byond, which makes sure it writes in-bound let initial_res = unsafe { @@ -45,7 +46,8 @@ pub fn byond_block(corner1: ByondXYZ, corner2: ByondXYZ) -> Result { - buff.reserve_exact(len as usize); + debug_assert!(len > initial_len); + buff.reserve_exact((len - initial_len) as usize); // Safety: buffer capacity is passed to byond, which makes sure it writes in-bound unsafe { map_byond_error!(byond().Byond_Block( diff --git a/crates/byondapi-rs/src/value/functions.rs b/crates/byondapi-rs/src/value/functions.rs index 05ee416..4ce9eb1 100644 --- a/crates/byondapi-rs/src/value/functions.rs +++ b/crates/byondapi-rs/src/value/functions.rs @@ -42,13 +42,15 @@ impl ByondValue { } let bytes = BUFFER.with_borrow_mut(|buff| -> Result, Error> { + let initial_len = buff.capacity() as u32; let mut len = buff.capacity() as u32; // Safety: buffer capacity is passed to byond, which makes sure it writes in-bound let initial_res = unsafe { byond().Byond_ToString(&self.0, buff.as_mut_ptr().cast(), &mut len) }; match (initial_res, len) { (false, 1..) => { - buff.reserve_exact(len as usize); + debug_assert!(len > initial_len); + buff.reserve_exact((len - initial_len) as usize); // Safety: buffer capacity is passed to byond, which makes sure it writes in-bound unsafe { map_byond_error!(byond().Byond_ToString( diff --git a/crates/byondapi-rs/src/value/list.rs b/crates/byondapi-rs/src/value/list.rs index a93d3bf..8435e83 100644 --- a/crates/byondapi-rs/src/value/list.rs +++ b/crates/byondapi-rs/src/value/list.rs @@ -13,6 +13,7 @@ impl ByondValue { } BUFFER.with_borrow_mut(|buff| -> Result, Error> { + let initial_len = buff.capacity() as u32; let mut len = buff.capacity() as u32; // Safety: buffer capacity is passed to byond, which makes sure it writes in-bound @@ -20,7 +21,8 @@ impl ByondValue { unsafe { byond().Byond_ReadList(&self.0, buff.as_mut_ptr().cast(), &mut len) }; match (initial_res, len) { (false, 1..) => { - buff.reserve_exact(len as usize); + debug_assert!(len > initial_len); + buff.reserve_exact((len - initial_len) as usize); // Safety: buffer capacity is passed to byond, which makes sure it writes in-bound unsafe { map_byond_error!(byond().Byond_ReadList( @@ -56,6 +58,7 @@ impl ByondValue { } BUFFER.with_borrow_mut(|buff| -> Result, Error> { + let initial_len = buff.capacity() as u32; let mut len = buff.capacity() as u32; // Safety: buffer capacity is passed to byond, which makes sure it writes in-bound @@ -63,7 +66,8 @@ impl ByondValue { unsafe { byond().Byond_ReadListAssoc(&self.0, buff.as_mut_ptr().cast(), &mut len) }; match (initial_res, len) { (false, 1..) => { - buff.reserve_exact(len as usize); + debug_assert!(len > initial_len); + buff.reserve_exact((len - initial_len) as usize); // Safety: buffer capacity is passed to byond, which makes sure it writes in-bound unsafe { map_byond_error!(byond().Byond_ReadListAssoc(