Skip to content

Commit 8219ad4

Browse files
authored
Rollup merge of #92724 - inteon:cleanup, r=Mark-Simulacrum
Cleanup c_str.rs Some code cleanups in `c_str.rs`. No functional changes. ref: bytecodealliance/rustix#163
2 parents 557d300 + afb7a50 commit 8219ad4

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

library/std/src/ffi/c_str.rs

+8-7
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)