From 32fd0804ad005593e205e363c818cf124815a931 Mon Sep 17 00:00:00 2001 From: George Miao Date: Sat, 13 Apr 2024 00:40:30 +0900 Subject: [PATCH 1/2] fix(core): Buffer offset usage --- core/src/types/buffer.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/core/src/types/buffer.rs b/core/src/types/buffer.rs index 33aab95b553a..a858cffd2e12 100644 --- a/core/src/types/buffer.rs +++ b/core/src/types/buffer.rs @@ -216,8 +216,10 @@ impl Buffer { parts, idx, offset, .. } => { let mut ret = Vec::with_capacity(parts.len() - *idx); + let mut new_offset = *offset; for part in parts.iter().skip(*idx) { - ret.push(IoSlice::new(&part[*offset..])); + ret.push(IoSlice::new(&part[*new_offset..])); + new_offset = 0; } ret } @@ -368,16 +370,16 @@ impl Buf for Buffer { return 0; } - let mut i = 0; - for part in parts.iter().skip(*idx) { - if i >= dst.len() { - break; - } - - dst[i] = IoSlice::new(&part[*offset..]); - i += 1; - } - i + let mut new_offset = *offset; + parts + .iter() + .skip(*idx) + .zip(dst.iter_mut()) + .map(|(part, dst)| { + *dst = IoSlice::new(&part[*new_offset..]); + new_offset = 0; + }) + .count() } } } From 5df2c8be7ea7c4540208a4eb7c24fa2724ab3c20 Mon Sep 17 00:00:00 2001 From: George Miao Date: Sat, 13 Apr 2024 00:46:03 +0900 Subject: [PATCH 2/2] fix: wrong deref of usize --- core/src/types/buffer.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/types/buffer.rs b/core/src/types/buffer.rs index a858cffd2e12..0fd38eda579e 100644 --- a/core/src/types/buffer.rs +++ b/core/src/types/buffer.rs @@ -218,7 +218,7 @@ impl Buffer { let mut ret = Vec::with_capacity(parts.len() - *idx); let mut new_offset = *offset; for part in parts.iter().skip(*idx) { - ret.push(IoSlice::new(&part[*new_offset..])); + ret.push(IoSlice::new(&part[new_offset..])); new_offset = 0; } ret @@ -376,7 +376,7 @@ impl Buf for Buffer { .skip(*idx) .zip(dst.iter_mut()) .map(|(part, dst)| { - *dst = IoSlice::new(&part[*new_offset..]); + *dst = IoSlice::new(&part[new_offset..]); new_offset = 0; }) .count()