Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions crates/fspy_preload_unix/src/client/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,10 @@ impl ToAbsolutePath for PathAt<'_, '_> {
self,
f: F,
) -> nix::Result<R> {
// SAFETY: self.1 is a valid NUL-terminated string.
let pathname = unsafe { CStr::from_ptr(self.1.as_ptr()) }.to_bytes().as_bstr();
let pathname = self.1.count().as_bytes().as_bstr();

if pathname.first().copied() == Some(b'/') {
f(pathname.into())
if pathname.starts_with(b"/") {
f(Some(pathname))
} else {
self.0.to_absolute_path(|base| {
let Some(base) = base else {
Expand Down
8 changes: 8 additions & 0 deletions crates/sigsafe/src/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ impl<'a> CStr<'a, Fat> {
}
}

/// Returns the string's bytes without the terminating NUL.
#[must_use]
pub const fn as_bytes(&self) -> &'a [u8] {
let bytes = self.as_bytes_with_nul();
bytes.split_at(bytes.len() - 1).0
}

/// Returns the string's bytes, including the terminating NUL.
#[must_use]
pub const fn as_bytes_with_nul(&self) -> &'a [u8] {
Expand Down Expand Up @@ -132,6 +139,7 @@ mod tests {
assert_eq!(size_of::<CStr<'_, Thin>>(), size_of::<*const u8>());
assert_eq!(size_of::<CStr<'_, Fat>>(), size_of::<(*const u8, usize)>());
assert_eq!(fat.len_with_nul(), 4);
assert_eq!(fat.as_bytes(), b"abc");
assert_eq!(fat.as_bytes_with_nul(), b"abc\0");
assert_eq!(counted.as_bytes_with_nul(), fat.as_bytes_with_nul());
}
Expand Down
Loading