Skip to content

Harness for CStr::is_empty #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion library/core/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,8 +860,11 @@ mod verify {

// Helper function
fn arbitrary_cstr(slice: &[u8]) -> &CStr {
// At a minimum, the slice has a null terminator to form a valid CStr.
kani::assume(slice.len() > 0 && slice[slice.len() - 1] == 0);
let result = CStr::from_bytes_until_nul(&slice);
kani::assume(result.is_ok());
// Given the assumption, from_bytes_until_nul should never fail
assert!(result.is_ok());
let c_str = result.unwrap();
assert!(c_str.is_safe());
c_str
Expand Down Expand Up @@ -939,4 +942,18 @@ mod verify {
assert_eq!(bytes, &slice[..end_idx]);
assert!(c_str.is_safe());
}

#[kani::proof]
#[kani::unwind(33)]
fn check_is_empty() {
const MAX_SIZE: usize = 32;
let string: [u8; MAX_SIZE] = kani::any();
let slice = kani::slice::any_slice_of_array(&string);
let c_str = arbitrary_cstr(slice);

let bytes = c_str.to_bytes(); // does not include null terminator
let expected_is_empty = bytes.len() == 0;
assert_eq!(expected_is_empty, c_str.is_empty());
assert!(c_str.is_safe());
}
}
Loading