Skip to content

Don't use a shared reference in MsgHdrMut - #674

Merged
Thomasdezeeuw merged 3 commits into
masterfrom
thomas/673
Oct 10, 2026
Merged

Thomasdezeeuw merged 3 commits into
masterfrom
thomas/673

Conversation

@Thomasdezeeuw

Copy link
Copy Markdown
Collaborator

Because we used set_msghdr_name, which uses a reference to SockAddr we
broke the aliasing rules. See #673 for more details. To fix this use raw pointers instead.

Also adds SockAddr::as_mut_ptr, which I needed to do this and since it's quite useful I made it public.

Fixes #673

Returns a raw mutable pointer to the address.
Because we used set_msghdr_name, which uses a reference to SockAddr we
broke the aliasing rules. See #673 for more details.

To fix this use raw pointers instead.

Fixes #673
Comment thread src/lib.rs Outdated
Comment thread src/sockaddr.rs

/// Returns a raw mutable pointer to the address.
pub fn as_mut_ptr(&mut self) -> *mut SockAddrStorage {
&mut self.storage as *mut sockaddr_storage as *mut SockAddrStorage

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
&mut self.storage as *mut sockaddr_storage as *mut SockAddrStorage
&raw mut self.storage as *mut SockAddrStorage

or if this doesn't work on MSRV:

Suggested change
&mut self.storage as *mut sockaddr_storage as *mut SockAddrStorage
addr_of_mut!(self.storage) as *mut SockAddrStorage

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I tried the nicer way, but the MSRV is too low. The code matches the as_ptr function.

This way the raw pointer is not invalidated.
@Thomasdezeeuw
Thomasdezeeuw merged commit da6885e into master Oct 10, 2026
57 of 58 checks passed
@Thomasdezeeuw
Thomasdezeeuw deleted the thomas/673 branch October 10, 2026 12:22
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.

MsgHdrMut::with_addr derives msg_name from a shared reborrow, so recvmsg writes through SharedReadOnly provenance

2 participants