Skip to content

Commit c56281b

Browse files
dmitryashjoshlf
andauthored
Fix Ref::new_slice_from_suffix() (#506)
* Fix Ref::new_slice_from_suffix() `new_slice_from_suffix()` has a bug and implemented exactly as `new_slice_from_prefix()`. Now part of buffer is chosen right. Tests couldn't have caught because buffer was just zeros. Now buffer is not just zeros so that tests can prove correctness of `new_slice_from_suffix()`. * position -> split_at Co-authored-by: Joshua Liebow-Feeser <[email protected]> --------- Co-authored-by: Joshua Liebow-Feeser <[email protected]>
1 parent ec5e2ab commit c56281b

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/lib.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -2051,10 +2051,8 @@ where
20512051
Some(len) => len,
20522052
None => return None,
20532053
};
2054-
if bytes.len() < expected_len {
2055-
return None;
2056-
}
2057-
let (bytes, suffix) = bytes.split_at(expected_len);
2054+
let split_at = bytes.len().checked_sub(expected_len)?;
2055+
let (bytes, suffix) = bytes.split_at(split_at);
20582056
Self::new_slice(suffix).map(move |l| (bytes, l))
20592057
}
20602058
}
@@ -4236,8 +4234,9 @@ mod tests {
42364234

42374235
{
42384236
buf.set_default();
4237+
buf.t[8..].fill(0xFF);
42394238
let (r, suffix) = Ref::<_, [AU64]>::new_slice_from_prefix(&mut buf.t[..], 1).unwrap();
4240-
assert_eq!(suffix, [0; 8]);
4239+
assert_eq!(suffix, [0xFF; 8]);
42414240
test_new_helper_slice(r, 1);
42424241
}
42434242
{
@@ -4249,8 +4248,9 @@ mod tests {
42494248
}
42504249
{
42514250
buf.set_default();
4251+
buf.t[..8].fill(0xFF);
42524252
let (prefix, r) = Ref::<_, [AU64]>::new_slice_from_suffix(&mut buf.t[..], 1).unwrap();
4253-
assert_eq!(prefix, [0; 8]);
4253+
assert_eq!(prefix, [0xFF; 8]);
42544254
test_new_helper_slice(r, 1);
42554255
}
42564256
{

0 commit comments

Comments
 (0)