@@ -215,13 +215,16 @@ impl fmt::Display for FromBytesWithNulError {
215
215
216
216
#[ unstable( feature = "ub_checks" , issue = "none" ) ]
217
217
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
+ */
218
224
fn is_safe ( & self ) -> bool {
219
225
let bytes: & [ c_char ] = & self . inner ;
220
226
let len = bytes. len ( ) ;
221
227
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.
225
228
if bytes. is_empty ( ) || bytes[ len - 1 ] != 0 || bytes[ ..len-1 ] . contains ( & 0 ) {
226
229
return false ;
227
230
}
@@ -864,17 +867,16 @@ mod verify {
864
867
865
868
// pub const fn from_bytes_until_nul(bytes: &[u8]) -> Result<&CStr, FromBytesUntilNulError>
866
869
#[ 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
868
871
fn check_from_bytes_until_nul ( ) {
869
- const MAX_SIZE : usize = 16 ;
872
+ const MAX_SIZE : usize = 32 ;
870
873
let string: [ u8 ; MAX_SIZE ] = kani:: any ( ) ;
871
874
// Covers the case of a single null byte at the end, no null bytes, as
872
875
// well as intermediate null bytes
873
876
let slice = kani:: slice:: any_slice_of_array ( & string) ;
874
877
875
878
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 {
878
880
assert ! ( c_str. is_safe( ) ) ;
879
881
}
880
882
}
0 commit comments