Skip to content

Commit ef02fc8

Browse files
committed
fix(fetcher/checksum): panic when 0 < flen % (block_len * lanes) < block_len
1 parent 9c20476 commit ef02fc8

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

rsync-fetcher/src/rsync/checksum.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,19 @@ fn checksum_payload_(
138138
array_init::array_init(|_| vec![0u8; sum_head.block_len as usize + 4]);
139139

140140
while block_remaining >= simd_impl.lanes() {
141+
if file_remaining < sum_head.block_len as u64 * simd_impl.lanes() as u64 {
142+
// not enough data for simd
143+
break;
144+
}
145+
141146
let mut datas: [&[u8]; md4_simd::simd::MAX_LANES] =
142147
[&[]; md4_simd::simd::MAX_LANES];
143148
for (idx, buf) in bufs[0..simd_impl.lanes()].iter_mut().enumerate() {
144149
// let buf = &mut bufs[idx];
145150

146151
// Sqrt of usize must be in u32 range.
147152
#[allow(clippy::cast_possible_truncation)]
148-
let n1 = min(sum_head.block_len as u64, file_remaining) as usize;
153+
let n1 = sum_head.block_len as usize;
149154

150155
file.read_exact(&mut buf[..n1]).expect("IO error");
151156

@@ -200,8 +205,8 @@ mod tests {
200205

201206
use crate::rsync::checksum::{checksum_payload, checksum_payload_basic, SumHead};
202207

203-
#[proptest]
204-
fn must_checksum_payload_basic_eq_simd(data: Vec<u8>) {
208+
#[inline]
209+
fn must_checksum_payload_basic_eq_simd_(data: Vec<u8>) -> (Vec<u8>, Vec<u8>) {
205210
let file_len = data.len() as u64;
206211

207212
let mut f = tempfile().expect("tempfile");
@@ -213,6 +218,19 @@ mod tests {
213218
let chksum_simd = checksum_payload(sum_head, 0, &mut f, file_len);
214219
f.seek(SeekFrom::Start(0)).expect("seek");
215220
let chksum_basic = checksum_payload_basic(sum_head, 0, &mut f, file_len);
221+
(chksum_simd, chksum_basic)
222+
}
223+
224+
#[proptest]
225+
fn must_checksum_payload_basic_eq_simd(data: Vec<u8>) {
226+
let (chksum_simd, chksum_basic) = must_checksum_payload_basic_eq_simd_(data);
216227
prop_assert_eq!(chksum_simd, chksum_basic);
217228
}
229+
230+
#[test]
231+
fn checksum_payload_simd_regression_1() {
232+
let data = vec![0u8; 11199];
233+
let (chksum_simd, chksum_basic) = must_checksum_payload_basic_eq_simd_(data);
234+
assert_eq!(chksum_simd, chksum_basic);
235+
}
218236
}

0 commit comments

Comments
 (0)