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

Unsafe pointer mutation #72

Open
tobywf opened this issue Jun 20, 2023 · 0 comments
Open

Unsafe pointer mutation #72

tobywf opened this issue Jun 20, 2023 · 0 comments

Comments

@tobywf
Copy link

tobywf commented Jun 20, 2023

For the std::io::Write implementation on TTYPort, a *const ptr is cast to *mut:

libc::write(self.fd, buf.as_ptr() as *mut c_void, buf.len() as size_t)

This is unnecessary, as libc::write takes buf: *const c_void, so buf.as_ptr() as *const c_void would work fine.

For the std::io::Read implementation on TTYPort, similar but worse, as the pointer is mutated:

libc::read(self.fd, buf.as_ptr() as *mut c_void, buf.len() as size_t)

In this case, the solution is buf.as_mut_ptr() as *mut c_void. The documentation on slice.as_ptr():

The caller must also ensure that the memory the pointer (non-transitively) points to is never written to (except inside an UnsafeCell) using this pointer or any pointer derived from it. If you need to mutate the contents of the slice, use as_mut_ptr.

I haven't checked any other code, so there may be more.

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

No branches or pull requests

1 participant