Skip to content
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

Harness for CStr::is_empty #194

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Small optimization of arbitray_cstr
  • Loading branch information
Yenyun035 committed Nov 30, 2024
commit 9d4476feea2819fa6691837a8933719ae33237c4
7 changes: 5 additions & 2 deletions 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 @@ -946,7 +949,7 @@ mod verify {
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); // introduced in PR#189
let c_str = arbitrary_cstr(slice);

let bytes = c_str.to_bytes(); // does not include null terminator
let expected_is_empty = bytes.len() == 0;
Expand Down
Loading