Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace CStr::from_ptr() with CStr::from_bytes_with_nul() #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MarijnS95
Copy link
Member

For #81 (comment)

CStr::from_ptr() is unsafe because it reads a raw pointer, and searches for a terminating nul character in the pointed region of memory.

This is unnecessary as both calls already initialize a given number of characters and terminate with a nul, allowing us to pass a sized and initialized slice (without casting *const MaybeUninit<u8> to *const u8) directly to CStr::from_bytes_with_nul() (available since Rust 1.10, unlike CStr::from_bytes_until_nul() which was only stabilized in 1.69). Unfortunately all std helper APIs to initialize slices of MaybeUninit are still unstable, making this less ideal to write at the moment.

`CStr::from_ptr()` is `unsafe` because it reads a raw pointer, and
searches for a terminating nul character in the pointed region of
memory.

This is unnecessary as both calls already initialize a given number of
characters and terminate with a nul, allowing us to pass a sized and
initialized slice (without casting `*const MaybeUninit<u8>` to `*const
u8`) directly to `CStr::from_bytes_with_nul()` (available since Rust
1.10, unlike `CStr::from_bytes_until_nul()` which was only stabilized
in 1.69).  Unfortunately all `std` helper APIs to initialize slices of
`MaybeUninit` are still unstable, making this less ideal to write at
the moment.
Comment on lines +459 to 464
// TODO: This function must be `unsafe` - or receive an initialized slice or CStr directly -
// as we can't otherwise guarantee safety here.
let initialized = unsafe { slice_assume_init_ref(&self.buffer[..len + 1]) };
let msg = CStr::from_bytes_with_nul(initialized)
.expect("Unreachable: nul terminator was placed at `len`");
android_log(self.buf_id, self.priority, self.tag, msg);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before merging, we should change the signature of fn output_specified_len() to either be unsafe or already pass an initialized &[u8] slice or CStr. There's no way we can write a valid // SAFETY: here right now.

Comment on lines +274 to +276
// SAFETY: The above code initialized the length of the tag plus a nul terminator, or the whole length of the slice
let initialized = unsafe { slice_assume_init_ref(initialized) };
CStr::from_bytes_with_nul(initialized).expect("Unreachable: we wrote a nul terminator")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This safety comment is a bit too vague for my liking, I'll see if I can update it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant