Skip to content

Commit f87b1ae

Browse files
authored
Harnesses for count_bytes (#191)
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 ```
1 parent 014965a commit f87b1ae

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

library/core/src/ffi/c_str.rs

+23
Original file line numberDiff line numberDiff line change
@@ -875,4 +875,27 @@ mod verify {
875875
assert!(c_str.is_safe());
876876
}
877877
}
878+
879+
#[kani::proof]
880+
#[kani::unwind(32)]
881+
fn check_count_bytes() {
882+
const MAX_SIZE: usize = 32;
883+
let mut bytes: [u8; MAX_SIZE] = kani::any();
884+
885+
// Non-deterministically generate a length within the valid range [0, MAX_SIZE]
886+
let mut len: usize = kani::any_where(|&x| x < MAX_SIZE);
887+
888+
// If a null byte exists before the generated length
889+
// adjust len to its position
890+
if let Some(pos) = bytes[..len].iter().position(|&x| x == 0) {
891+
len = pos;
892+
} else {
893+
// If no null byte, insert one at the chosen length
894+
bytes[len] = 0;
895+
}
896+
897+
let c_str = CStr::from_bytes_until_nul(&bytes).unwrap();
898+
// Verify that count_bytes matches the adjusted length
899+
assert_eq!(c_str.count_bytes(), len);
900+
}
878901
}

0 commit comments

Comments
 (0)