Skip to content

Clarify safety invariants in SocketAddrArg.#1325

Merged
sunfishcode merged 3 commits intomainfrom
sunfishcode/addr-safety
Feb 14, 2025
Merged

Clarify safety invariants in SocketAddrArg.#1325
sunfishcode merged 3 commits intomainfrom
sunfishcode/addr-safety

Conversation

@sunfishcode
Copy link
Copy Markdown
Member

In making another safety review of the SocketAddrArg code, I found it too subtle to have MMsgHdr::new_with_addr depending on knowing that with_msghdr happens to avoid using a temporary in the case SocketAddrAny. Change it to avoid doing this, add more comments, and change with_msghdr's argument to a reference, to prevent it from being accidentally used in this way.

And change with_noaddr_msghdr to noaddr_msghdr and make it just return a msghdr, because it never needs to construct any temporaries, so it avoids this issue.

Also, make SocketAddrArg::with_sockaddr and with_recv_msghdr unsafe, as the provided function will need to contain an unsafe block which will need to be able to trust it's being called correctly.

And add safety comments for the callers and impls of these functions and traits.

In making another safety review of the `SocketAddrArg` code, I found it
too subtle to have `MMsgHdr::new_with_addr` depending on knowing that
`with_msghdr` happens to avoid using a temporary in the case `SocketAddrAny`.
Change it to avoid doing this, add more comments, and change `with_msghdr`'s
argument to a reference, to prevent it from being accidentally used in
this way.

And change `with_noaddr_msghdr` to `noaddr_msghdr` and make it just return a
`msghdr`, because it never needs to construct any temporaries, so it
avoids this issue.

Also, make `SocketAddrArg::with_sockaddr` and `with_recv_msghdr` unsafe, as
the provided function will need to contain an `unsafe` block which will need
to be able to trust it's being called correctly.

And add safety comments for the callers and impls of these functions and
traits.
@sunfishcode sunfishcode merged commit 3cbb4c3 into main Feb 14, 2025
@sunfishcode sunfishcode deleted the sunfishcode/addr-safety branch February 14, 2025 18:47
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