This is an automated email from the ASF dual-hosted git repository.

xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git


The following commit(s) were added to refs/heads/main by this push:
     new 0bdef41911 fix(core): Buffer offset misuse (#4481)
0bdef41911 is described below

commit 0bdef419113c82dc2958f74d1d7f9b1f551ff639
Author: Pop <[email protected]>
AuthorDate: Sat Apr 13 11:51:56 2024 +0900

    fix(core): Buffer offset misuse (#4481)
---
 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 33aab95b55..0fd38eda57 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()
             }
         }
     }

Reply via email to