Skip to content

Commit

Permalink
Harnesses for count_bytes (#191)
Browse files Browse the repository at this point in the history
Towards #150 

Changes
Added harnesses for count_bytes

Verification Result
```
Checking harness ffi::c_str::verify::check_count_bytes...

VERIFICATION RESULT:
 ** 0 of 241 failed (5 unreachable)

VERIFICATION:- SUCCESSFUL
Verification Time: 5.377671s
```
  • Loading branch information
MWDZ authored Nov 28, 2024
1 parent 014965a commit f87b1ae
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions library/core/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -875,4 +875,27 @@ mod verify {
assert!(c_str.is_safe());
}
}

#[kani::proof]
#[kani::unwind(32)]
fn check_count_bytes() {
const MAX_SIZE: usize = 32;
let mut bytes: [u8; MAX_SIZE] = kani::any();

// Non-deterministically generate a length within the valid range [0, MAX_SIZE]
let mut len: usize = kani::any_where(|&x| x < MAX_SIZE);

// If a null byte exists before the generated length
// adjust len to its position
if let Some(pos) = bytes[..len].iter().position(|&x| x == 0) {
len = pos;
} else {
// If no null byte, insert one at the chosen length
bytes[len] = 0;
}

let c_str = CStr::from_bytes_until_nul(&bytes).unwrap();
// Verify that count_bytes matches the adjusted length
assert_eq!(c_str.count_bytes(), len);
}
}

0 comments on commit f87b1ae

Please sign in to comment.