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

Cstr - add proof from_bytes_with_null #198

Merged
Changes from 5 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
29 changes: 27 additions & 2 deletions library/core/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,32 @@ mod verify {
assert!(c_str.is_safe());
}
}


// pub const fn from_bytes_with_nul(bytes: &[u8]) -> Result<&Self, FromBytesWithNulError>
#[kani::proof]
#[kani::unwind(17)]
fn check_from_bytes_with_nul() {
rajathkotyal marked this conversation as resolved.
Show resolved Hide resolved
const MAX_SIZE: usize = 16;
let mut string: [u8; MAX_SIZE] = kani::any();
let null_pos: usize = kani::any_where(|x| *x < MAX_SIZE);
tautschnig marked this conversation as resolved.
Show resolved Hide resolved
rajathkotyal marked this conversation as resolved.
Show resolved Hide resolved

string[null_pos] = 0;
// check to make sure all bytes are non null
for i in 0..null_pos {
string[i] = kani::any_where(|x| *x != 0);
}

let slice = &string[..=null_pos];
let result = CStr::from_bytes_with_nul(slice);
assert!(result.is_ok());
rajathkotyal marked this conversation as resolved.
Show resolved Hide resolved

let c_str = result.unwrap();
assert!(c_str.is_safe());
// additional checks
assert_eq!(c_str.to_bytes_with_nul(), slice);
assert_eq!(c_str.to_bytes(), &slice[..slice.len() - 1]);
}

// pub const fn count_bytes(&self) -> usize
#[kani::proof]
#[kani::unwind(32)]
Expand Down Expand Up @@ -956,4 +981,4 @@ mod verify {
assert_eq!(expected_is_empty, c_str.is_empty());
assert!(c_str.is_safe());
}
}
}
Loading