Kani - Generate an array with a constraint on elements #181
-
In the harness for Our current approach: #[kani::proof]
#[kani::unwind(32)]
fn check_from_bytes_until_nul_single_nul_byte_end() {
const ARR_LEN: usize = 32;
// ensure that the string does not have intermediate null bytes
// TODO: there might be a better way
let mut string: [u8; ARR_LEN] = kani::any_where(|x: &[u8; ARR_LEN]| !x[..ARR_LEN-1].contains(&0));
// ensure that the string is properly null-terminated
string[ARR_LEN - 1] = 0;
let c_str = CStr::from_bytes_until_nul(&string).unwrap();
assert!(c_str.is_safe());
} Appreciate any guidance. @celinval @zhassan-aws |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @Yenyun035. Can you clarify the reason why you want to ensure none of the bytes except the last is null? Wouldn't |
Beta Was this translation helpful? Give feedback.
Yes, it seems to me that harnesses 1 and 2 should be merged.
It might be more efficient to generate an array and assume the value is non-zero in a loop, e.g.: