File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -875,4 +875,27 @@ mod verify {
875875 assert ! ( c_str. is_safe( ) ) ;
876876 }
877877 }
878+
879+ #[ kani:: proof]
880+ #[ kani:: unwind( 32 ) ]
881+ fn check_count_bytes ( ) {
882+ const MAX_SIZE : usize = 32 ;
883+ let mut bytes: [ u8 ; MAX_SIZE ] = kani:: any ( ) ;
884+
885+ // Non-deterministically generate a length within the valid range [0, MAX_SIZE]
886+ let mut len: usize = kani:: any_where ( |& x| x < MAX_SIZE ) ;
887+
888+ // If a null byte exists before the generated length
889+ // adjust len to its position
890+ if let Some ( pos) = bytes[ ..len] . iter ( ) . position ( |& x| x == 0 ) {
891+ len = pos;
892+ } else {
893+ // If no null byte, insert one at the chosen length
894+ bytes[ len] = 0 ;
895+ }
896+
897+ let c_str = CStr :: from_bytes_until_nul ( & bytes) . unwrap ( ) ;
898+ // Verify that count_bytes matches the adjusted length
899+ assert_eq ! ( c_str. count_bytes( ) , len) ;
900+ }
878901}
You can’t perform that action at this time.
0 commit comments