Skip to content

Cstr - add proof from_bytes_with_null #198

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
Changes from 3 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
30 changes: 29 additions & 1 deletion library/core/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,34 @@ mod verify {
assert!(c_str.is_safe());
}
}

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

// Iterate over all possible slice lengths from 1 to MAX_SIZE
for slice_length in 1..=MAX_SIZE {
// Ensure the slice is valid by setting the last byte to null
buffer[slice_length - 1] = 0;

// Assume all bytes before the last are non-null
for i in 0..slice_length - 1 {
kani::assume(buffer[i] != 0);
}

// Create the slice up to the current slice_length
let slice = &buffer[..slice_length];
let result = CStr::from_bytes_with_nul(slice);

if let Ok(c_str) = result {
assert!(c_str.is_safe());
assert_eq!(c_str.to_bytes(), &buffer[..slice_length - 1]);
assert_eq!(c_str.to_bytes_with_nul(), &buffer[..slice_length]);
}
}
}

// pub const fn count_bytes(&self) -> usize
#[kani::proof]
Expand Down Expand Up @@ -939,4 +967,4 @@ mod verify {
assert_eq!(bytes, &slice[..end_idx]);
assert!(c_str.is_safe());
}
}
}