Skip to content

Commit

Permalink
Add explanatory comments to weird overflow-proof conditionals
Browse files Browse the repository at this point in the history
  • Loading branch information
musicinmybrain committed Sep 7, 2024
1 parent e0fea6b commit fd823d5
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ mod test {
#[test]
fn check_array_ref_5() {
fn f(data: Vec<u8>, offset: usize) -> quickcheck::TestResult {
// Compute the following, with correct results even if the sum would overflow:
// if data.len() < offset + 5
if data.len() < 5 || data.len() - 5 < offset {
return quickcheck::TestResult::discard();
}
Expand All @@ -351,6 +353,8 @@ mod test {
#[test]
fn check_array_ref_out_of_bounds_5() {
fn f(data: Vec<u8>, offset: usize) -> quickcheck::TestResult {
// Compute the following, with correct results even if the sum would overflow:
// if data.len() >= offset + 5
if data.len() >= 5 && data.len() - 5 >= offset {
return quickcheck::TestResult::discard();
}
Expand All @@ -364,6 +368,8 @@ mod test {
#[test]
fn check_array_mut_ref_7() {
fn f(mut data: Vec<u8>, offset: usize) -> quickcheck::TestResult {
// Compute the following, with correct results even if the sum would overflow:
// if data.len() < offset + 7
if data.len() < 7 || data.len() - 7 < offset {
return quickcheck::TestResult::discard();
}
Expand All @@ -377,6 +383,8 @@ mod test {
#[test]
fn check_array_mut_ref_out_of_bounds_32() {
fn f(mut data: Vec<u8>, offset: usize) -> quickcheck::TestResult {
// Compute the following, with correct results even if the sum would overflow:
// if data.len() >= offset + 32
if data.len() >= 32 && data.len() - 32 >= offset {
return quickcheck::TestResult::discard();
}
Expand Down

0 comments on commit fd823d5

Please sign in to comment.