Skip to content

Commit

Permalink
fix(fetcher/checksum): panic when 0 < flen % (block_len * lanes) < bl…
Browse files Browse the repository at this point in the history
…ock_len
  • Loading branch information
PhotonQuantum committed Aug 31, 2023
1 parent 9c20476 commit ef02fc8
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions rsync-fetcher/src/rsync/checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,19 @@ fn checksum_payload_(
array_init::array_init(|_| vec![0u8; sum_head.block_len as usize + 4]);

while block_remaining >= simd_impl.lanes() {
if file_remaining < sum_head.block_len as u64 * simd_impl.lanes() as u64 {
// not enough data for simd
break;
}

let mut datas: [&[u8]; md4_simd::simd::MAX_LANES] =
[&[]; md4_simd::simd::MAX_LANES];
for (idx, buf) in bufs[0..simd_impl.lanes()].iter_mut().enumerate() {
// let buf = &mut bufs[idx];

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

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

Expand Down Expand Up @@ -200,8 +205,8 @@ mod tests {

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

#[proptest]
fn must_checksum_payload_basic_eq_simd(data: Vec<u8>) {
#[inline]
fn must_checksum_payload_basic_eq_simd_(data: Vec<u8>) -> (Vec<u8>, Vec<u8>) {
let file_len = data.len() as u64;

let mut f = tempfile().expect("tempfile");
Expand All @@ -213,6 +218,19 @@ mod tests {
let chksum_simd = checksum_payload(sum_head, 0, &mut f, file_len);
f.seek(SeekFrom::Start(0)).expect("seek");
let chksum_basic = checksum_payload_basic(sum_head, 0, &mut f, file_len);
(chksum_simd, chksum_basic)
}

#[proptest]
fn must_checksum_payload_basic_eq_simd(data: Vec<u8>) {
let (chksum_simd, chksum_basic) = must_checksum_payload_basic_eq_simd_(data);
prop_assert_eq!(chksum_simd, chksum_basic);
}

#[test]
fn checksum_payload_simd_regression_1() {
let data = vec![0u8; 11199];
let (chksum_simd, chksum_basic) = must_checksum_payload_basic_eq_simd_(data);
assert_eq!(chksum_simd, chksum_basic);
}
}

0 comments on commit ef02fc8

Please sign in to comment.