Remove Path::from_bytes_with_nul_unchecked

Path::from_bytes_with_nul_unchecked was a wrapper for
CStr::from_bytes_with_nul_unchecked and Path::from_cstr_unchecked.
Keeping the two methods separate clearly communicates the requirements
and reduces the likelihood of unsound usage.
This commit is contained in:
Robin Krahl
2024-06-03 10:31:03 +02:00
parent d7336c133c
commit 571aad9b46
2 changed files with 18 additions and 17 deletions
+7 -3
View File
@@ -7,25 +7,29 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## Unreleased
## Added
### Added
- Added object-safe traits `DynFile`, `DynFilesystem` and `DynStorage` for
accessing `Storage`, `Filesystem` and `File` implementations for any storage.
- Added `Filesystem::mount_or_else` function ([#57][])
- Marked `Path::is_empty`, `Path::from_bytes_with_nul`, `Path::from_cstr`, `Path::from_cstr_unchecked`, `Path::as_str_ref_with_trailing_nul`, `Path::as_str`, and `PathBuf::new` as `const`.
## Fixed
### Fixed
- Fixed macro hygiene for `path!`.
- Fixed build error that would occur on Windows systems.
- Fixed compilation without default features.
- Added path iteration utilities ([#47][])
## Changed
### Changed
- Enforced const evaluation for `path!`.
- Removed `cstr_core` and `cty` dependencies.
- Updated `littlefs2-sys` dependency to 0.2.0.
### Removed
- Removed `Path::from_bytes_with_nul_unchecked`. Use `CStr::from_bytes_with_nul_unchecked` and `Path::from_cstr_unchecked` instead.
[#47]: https://github.com/trussed-dev/littlefs2/pull/47
[#57]: https://github.com/trussed-dev/littlefs2/pull/57
+11 -14
View File
@@ -188,7 +188,10 @@ impl Path {
None | Some((_, "\x00")) => None,
Some((_, path)) => {
debug_assert!(path.ends_with('\x00'));
Some(unsafe { Path::from_bytes_with_nul_unchecked(path.as_bytes()) })
unsafe {
let cstr = CStr::from_bytes_with_nul_unchecked(path.as_bytes());
Some(Path::from_cstr_unchecked(cstr))
}
}
}
}
@@ -243,7 +246,10 @@ impl Path {
}
assert!(!bytes.is_empty(), "must not be empty");
assert!(bytes[i] == 0, "last byte must be null");
unsafe { Self::from_bytes_with_nul_unchecked(bytes) }
unsafe {
let cstr = CStr::from_bytes_with_nul_unchecked(bytes);
Self::from_cstr_unchecked(cstr)
}
}
/// Creates a path from a byte buffer
@@ -257,14 +263,6 @@ impl Path {
}
}
/// Unchecked version of `from_bytes_with_nul`
///
/// # Safety
/// `bytes` must be null terminated string comprised of only ASCII characters
pub const unsafe fn from_bytes_with_nul_unchecked(bytes: &[u8]) -> &Self {
&*(bytes as *const [u8] as *const Path)
}
/// Creates a path from a C string
///
/// The string will be checked to be comprised only of ASCII characters
@@ -546,10 +544,9 @@ impl ops::Deref for PathBuf {
fn deref(&self) -> &Path {
unsafe {
Path::from_bytes_with_nul_unchecked(slice::from_raw_parts(
self.buf.as_ptr().cast(),
self.len,
))
let bytes = slice::from_raw_parts(self.buf.as_ptr().cast(), self.len);
let cstr = CStr::from_bytes_with_nul_unchecked(bytes);
Path::from_cstr_unchecked(cstr)
}
}
}