Skip to content

Commit 2d734df

Browse files
committed
Change array size to 32 && Add is_safe function doc && Fix harness
1 parent 667b76f commit 2d734df

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

library/core/src/ffi/c_str.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,16 @@ impl fmt::Display for FromBytesWithNulError {
215215

216216
#[unstable(feature = "ub_checks", issue = "none")]
217217
impl Invariant for &CStr {
218+
/**
219+
* Safety invariant of a valid CStr:
220+
* 1. An empty CStr should has a null byte.
221+
* 2. A valid CStr should end with a null-terminator and contains
222+
* no intermediate null bytes.
223+
*/
218224
fn is_safe(&self) -> bool {
219225
let bytes: &[c_char] = &self.inner;
220226
let len = bytes.len();
221227

222-
// An empty CStr should has a null byte.
223-
// A valid CStr should end with a null-terminator and contains
224-
// no intermediate null bytes.
225228
if bytes.is_empty() || bytes[len - 1] != 0 || bytes[..len-1].contains(&0) {
226229
return false;
227230
}
@@ -864,17 +867,16 @@ mod verify {
864867

865868
// pub const fn from_bytes_until_nul(bytes: &[u8]) -> Result<&CStr, FromBytesUntilNulError>
866869
#[kani::proof]
867-
#[kani::unwind(16)] // 7.3 seconds when 16; 33.1 seconds when 32
870+
#[kani::unwind(32)] // 7.3 seconds when 16; 33.1 seconds when 32
868871
fn check_from_bytes_until_nul() {
869-
const MAX_SIZE: usize = 16;
872+
const MAX_SIZE: usize = 32;
870873
let string: [u8; MAX_SIZE] = kani::any();
871874
// Covers the case of a single null byte at the end, no null bytes, as
872875
// well as intermediate null bytes
873876
let slice = kani::slice::any_slice_of_array(&string);
874877

875878
let result = CStr::from_bytes_until_nul(slice);
876-
if result.is_ok() {
877-
let c_str = result.unwrap();
879+
if let Ok(c_str) = result {
878880
assert!(c_str.is_safe());
879881
}
880882
}

0 commit comments

Comments
 (0)