File tree Expand file tree Collapse file tree 1 file changed +8
-7
lines changed Expand file tree Collapse file tree 1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change @@ -1252,15 +1252,16 @@ impl CStr {
12521252 /// assert!(cstr.is_err());
12531253 /// ```
12541254 #[ stable( feature = "cstr_from_bytes" , since = "1.10.0" ) ]
1255- pub fn from_bytes_with_nul ( bytes : & [ u8 ] ) -> Result < & CStr , FromBytesWithNulError > {
1255+ pub fn from_bytes_with_nul ( bytes : & [ u8 ] ) -> Result < & Self , FromBytesWithNulError > {
12561256 let nul_pos = memchr:: memchr ( 0 , bytes) ;
1257- if let Some ( nul_pos) = nul_pos {
1258- if nul_pos + 1 != bytes. len ( ) {
1259- return Err ( FromBytesWithNulError :: interior_nul ( nul_pos) ) ;
1257+ match nul_pos {
1258+ Some ( nul_pos) if nul_pos + 1 == bytes. len ( ) => {
1259+ // SAFETY: We know there is only one nul byte, at the end
1260+ // of the byte slice.
1261+ Ok ( unsafe { Self :: from_bytes_with_nul_unchecked ( bytes) } )
12601262 }
1261- Ok ( unsafe { CStr :: from_bytes_with_nul_unchecked ( bytes) } )
1262- } else {
1263- Err ( FromBytesWithNulError :: not_nul_terminated ( ) )
1263+ Some ( nul_pos) => Err ( FromBytesWithNulError :: interior_nul ( nul_pos) ) ,
1264+ None => Err ( FromBytesWithNulError :: not_nul_terminated ( ) ) ,
12641265 }
12651266 }
12661267
You can’t perform that action at this time.
0 commit comments