serial-rs
serial-rs copied to clipboard
Unsafe pointer mutation
For the std::io::Write
implementation on TTYPort
, a *const
ptr is cast to *mut
:
https://github.com/dcuddeback/serial-rs/blob/cb28b1439a5653f1777ad11d85d37f2c8074259b/serial-unix/src/tty.rs#L149
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:
https://github.com/dcuddeback/serial-rs/blob/cb28b1439a5653f1777ad11d85d37f2c8074259b/serial-unix/src/tty.rs#L132
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, useas_mut_ptr
.
I haven't checked any other code, so there may be more.