Skip to content

Commit d35de22

Browse files
committed
reserve this
1 parent b1a04ed commit d35de22

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

crates/byondapi-rs/src/map.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ pub fn byond_block(corner1: ByondXYZ, corner2: ByondXYZ) -> Result<Vec<ByondValu
3838
}
3939

4040
BUFFER.with_borrow_mut(|buff| -> Result<Vec<ByondValue>, Error> {
41+
let initial_len = buff.capacity() as u32;
4142
let mut len = buff.capacity() as u32;
4243
// Safety: buffer capacity is passed to byond, which makes sure it writes in-bound
4344
let initial_res = unsafe {
4445
byond().Byond_Block(&corner1.0, &corner2.0, buff.as_mut_ptr().cast(), &mut len)
4546
};
4647
match (initial_res, len) {
4748
(false, 1..) => {
48-
buff.reserve_exact(len as usize);
49+
debug_assert!(len > initial_len);
50+
buff.reserve_exact((len - initial_len) as usize);
4951
// Safety: buffer capacity is passed to byond, which makes sure it writes in-bound
5052
unsafe {
5153
map_byond_error!(byond().Byond_Block(

crates/byondapi-rs/src/value/functions.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ impl ByondValue {
4242
}
4343

4444
let bytes = BUFFER.with_borrow_mut(|buff| -> Result<Vec<u8>, Error> {
45+
let initial_len = buff.capacity() as u32;
4546
let mut len = buff.capacity() as u32;
4647
// Safety: buffer capacity is passed to byond, which makes sure it writes in-bound
4748
let initial_res =
4849
unsafe { byond().Byond_ToString(&self.0, buff.as_mut_ptr().cast(), &mut len) };
4950
match (initial_res, len) {
5051
(false, 1..) => {
51-
buff.reserve_exact(len as usize);
52+
debug_assert!(len > initial_len);
53+
buff.reserve_exact((len - initial_len) as usize);
5254
// Safety: buffer capacity is passed to byond, which makes sure it writes in-bound
5355
unsafe {
5456
map_byond_error!(byond().Byond_ToString(

crates/byondapi-rs/src/value/list.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@ impl ByondValue {
1313
}
1414

1515
BUFFER.with_borrow_mut(|buff| -> Result<Vec<ByondValue>, Error> {
16+
let initial_len = buff.capacity() as u32;
1617
let mut len = buff.capacity() as u32;
1718

1819
// Safety: buffer capacity is passed to byond, which makes sure it writes in-bound
1920
let initial_res =
2021
unsafe { byond().Byond_ReadList(&self.0, buff.as_mut_ptr().cast(), &mut len) };
2122
match (initial_res, len) {
2223
(false, 1..) => {
23-
buff.reserve_exact(len as usize);
24+
debug_assert!(len > initial_len);
25+
buff.reserve_exact((len - initial_len) as usize);
2426
// Safety: buffer capacity is passed to byond, which makes sure it writes in-bound
2527
unsafe {
2628
map_byond_error!(byond().Byond_ReadList(
@@ -56,14 +58,16 @@ impl ByondValue {
5658
}
5759

5860
BUFFER.with_borrow_mut(|buff| -> Result<Vec<ByondValue>, Error> {
61+
let initial_len = buff.capacity() as u32;
5962
let mut len = buff.capacity() as u32;
6063

6164
// Safety: buffer capacity is passed to byond, which makes sure it writes in-bound
6265
let initial_res =
6366
unsafe { byond().Byond_ReadListAssoc(&self.0, buff.as_mut_ptr().cast(), &mut len) };
6467
match (initial_res, len) {
6568
(false, 1..) => {
66-
buff.reserve_exact(len as usize);
69+
debug_assert!(len > initial_len);
70+
buff.reserve_exact((len - initial_len) as usize);
6771
// Safety: buffer capacity is passed to byond, which makes sure it writes in-bound
6872
unsafe {
6973
map_byond_error!(byond().Byond_ReadListAssoc(

0 commit comments

Comments
 (0)