Clarify safety invariants in SocketAddrArg.#1325
Merged
sunfishcode merged 3 commits intomainfrom Feb 14, 2025
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In making another safety review of the
SocketAddrArgcode, I found it too subtle to haveMMsgHdr::new_with_addrdepending on knowing thatwith_msghdrhappens to avoid using a temporary in the caseSocketAddrAny. Change it to avoid doing this, add more comments, and changewith_msghdr's argument to a reference, to prevent it from being accidentally used in this way.And change
with_noaddr_msghdrtonoaddr_msghdrand make it just return amsghdr, because it never needs to construct any temporaries, so it avoids this issue.Also, make
SocketAddrArg::with_sockaddrandwith_recv_msghdrunsafe, as the provided function will need to contain anunsafeblock 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.