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(