-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the memory safety guarantees around
open_readonly
.
Do more complete NUL termination checking, at compile-time. Remove the `unsafe` from the function as it is now memory-safe.
- Loading branch information
1 parent
6873944
commit 703f0e3
Showing
3 changed files
with
89 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
//! Work around lack of `core::ffi::CStr` prior to Rust 1.64, and the lack of | ||
//! `const fn` support for `CStr` in later versions. | ||
// TODO(MSRV 1.64): Use `core::ffi::c_char`. | ||
use libc::c_char; | ||
|
||
// TODO(MSRV 1.64): Replace with `&core::ffi::CStr`. | ||
pub struct Ref(&'static [u8]); | ||
|
||
impl Ref { | ||
#[inline(always)] | ||
pub fn as_ptr(&self) -> *const c_char { | ||
const _SAME_ALIGNMENT: () = | ||
assert!(core::mem::align_of::<u8>() == core::mem::align_of::<c_char>()); | ||
const _SAME_SIZE: () = | ||
assert!(core::mem::size_of::<u8>() == core::mem::size_of::<c_char>()); | ||
|
||
// It is safe to cast a `*const u8` to a `const c_char` as they are the | ||
// same size and alignment. | ||
self.0.as_ptr().cast() | ||
} | ||
|
||
// SAFETY: Same as `CStr::from_bytes_with_nul_unchecked`. | ||
const unsafe fn from_bytes_with_nul_unchecked(value: &'static [u8]) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
pub const fn unwrap_const_from_bytes_with_nul(value: &'static [u8]) -> Ref { | ||
// XXX: We cannot use `unwrap_const` since `Ref`/`CStr` is not `Copy`. | ||
match const_from_bytes_with_nul(value) { | ||
Some(r) => r, | ||
None => panic!("const_from_bytes_with_nul failed"), | ||
} | ||
} | ||
|
||
// TODO(MSRV 1.72): Replace with `CStr::from_bytes_with_nul`. | ||
#[inline(always)] | ||
const fn const_from_bytes_with_nul(value: &'static [u8]) -> Option<Ref> { | ||
const fn const_contains(mut value: &[u8], needle: &u8) -> bool { | ||
while let [head, tail @ ..] = value { | ||
if *head == *needle { | ||
return true; | ||
} | ||
value = tail; | ||
} | ||
false | ||
} | ||
|
||
// TODO(MSRV 1.69): Use `core::ffi::CStr::from_bytes_until_nul` | ||
match value { | ||
[before_nul @ .., 0] if !const_contains(before_nul, &0) => { | ||
// SAFETY: | ||
// * `value` is nul-terminated according to the slice pattern. | ||
// * `value` doesn't contain any interior null, by the guard. | ||
// TODO(MSRV 1.64): Use `CStr::from_bytes_with_nul_unchecked` | ||
Some(unsafe { Ref::from_bytes_with_nul_unchecked(value) }) | ||
} | ||
_ => None, | ||
} | ||
} | ||
|
||
mod tests { | ||
use super::const_from_bytes_with_nul; | ||
|
||
// Bad. | ||
const _EMPTY_UNTERMINATED: () = assert!(const_from_bytes_with_nul(b"").is_none()); | ||
const _EMPTY_DOUBLE_TERMINATED: () = assert!(const_from_bytes_with_nul(b"\0\0").is_none()); | ||
const _DOUBLE_NUL: () = assert!(const_from_bytes_with_nul(b"\0\0").is_none()); | ||
const _LEADINGL_NUL: () = assert!(const_from_bytes_with_nul(b"\0a\0").is_none()); | ||
const _INTERNAL_NUL_UNTERMINATED: () = assert!(const_from_bytes_with_nul(b"\0a").is_none()); | ||
|
||
// Good. | ||
const EMPTY_TERMINATED: () = assert!(const_from_bytes_with_nul(b"\0").is_some()); | ||
const _NONEMPTY: () = assert!(const_from_bytes_with_nul(b"asdf\0").is_some()); | ||
const _1_CHAR: () = assert!(const_from_bytes_with_nul(b"a\0").is_some()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters