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

Reword incorrect documentation about SocketAddr having varying layout #136230

Merged
merged 1 commit into from
Mar 14, 2025

Conversation

clarfonthey
Copy link
Contributor

@clarfonthey clarfonthey commented Jan 29, 2025

This has no longer been the case since these types were moved to core. The note on portability remains, but it is reworded to not imply that the size varies by target.

@clarfonthey clarfonthey marked this pull request as ready for review January 29, 2025 07:09
@rustbot
Copy link
Collaborator

rustbot commented Jan 29, 2025

r? @cuviper

rustbot has assigned @cuviper.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 29, 2025
@@ -42,10 +39,6 @@ pub enum SocketAddr {
///
/// See [`SocketAddr`] for a type encompassing both IPv4 and IPv6 socket addresses.
///
/// The size of a `SocketAddrV4` struct may vary depending on the target operating
/// system. Do not assume that this type has the same memory layout as the underlying
/// system representation.
Copy link
Member

Choose a reason for hiding this comment

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

I'm ok with removing the size comment, but isn't the other layout note still relevant? (e.g. don't try to treat this like a C struct sockaddr!)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, it's technically relevant, but it feels unprecedented in the standard library, since no other type adds a comment like this. I actually took a bit to look through the existing types, though, and I guess these are the types where this would technically be most applicable. (You wouldn't expect a File to be equivalent to a file handler, for example.)

Like, most people are not going to try transmuting random standard library structs to libc structs unless it's explicitly marked as allowed, and since transmuting is unsafe, they're also kind of admitting fault if they do this.

But also, is there actually a reason we couldn't make this equivalent to a C sockaddr anyway? Since no padding is required, and we don't really gain anything from letting the fields be randomly arranged.

Copy link
Member

Choose a reason for hiding this comment

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

We used to wrap sockaddr directly, and prominent crates did make unsafe assumptions about that. We changed to Rust-native types in #78802, which also documented many of those problems along the way.

So yes, in general users shouldn't make any assumptions about the underlying type, but they did in this case. Therefore, I think it's worth keeping some warning in the docs, even though it would be obviously broken if you tried it anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, for some reason, I had naïvely assumed that sockaddr just used the IETF definition on all platforms, which is clearly not correct. And besides that, it's not the same definition as the one we use.

I'll update to remark on this.

@cuviper
Copy link
Member

cuviper commented Feb 24, 2025

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 24, 2025
@clarfonthey clarfonthey changed the title Remove incorrect documentation about SocketAddr having varying layout Reword incorrect documentation about SocketAddr having varying layout Mar 8, 2025
@clarfonthey clarfonthey force-pushed the net-memory-layout-assumptions branch from 91c2b28 to 58de5a8 Compare March 8, 2025 13:17
@clarfonthey clarfonthey force-pushed the net-memory-layout-assumptions branch from 58de5a8 to ffa86bf Compare March 8, 2025 13:19
@clarfonthey
Copy link
Contributor Author

Sorry for taking so long, but I've finally gotten around to revising the wording. It's longer than the original, but at least it's accurate now.

@clarfonthey
Copy link
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 8, 2025
@cuviper
Copy link
Member

cuviper commented Mar 13, 2025

LGTM, thanks!

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Mar 13, 2025

📌 Commit ffa86bf has been approved by cuviper

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 13, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 14, 2025
Rollup of 16 pull requests

Successful merges:

 - rust-lang#136001 (Overhaul examples for PermissionsExt)
 - rust-lang#136230 (Reword incorrect documentation about SocketAddr having varying layout)
 - rust-lang#136892 (Sync Fuchsia target spec with clang Fuchsia driver)
 - rust-lang#136911 (Add documentation URL to selected jobs)
 - rust-lang#137870 ( Improve HashMap docs for const and static initializers)
 - rust-lang#138179 (Add `src/tools/x` to the main workspace)
 - rust-lang#138389 (use `expect` instead of `allow`)
 - rust-lang#138396 (Enable metrics and verbose tests in PR CI)
 - rust-lang#138398 (atomic intrinsics: clarify which types are supported and (if applicable) what happens with provenance)
 - rust-lang#138432 (fix: remove the check of lld not supporting `@response-file)`
 - rust-lang#138434 (Visit `PatField` when collecting lint levels)
 - rust-lang#138441 (update error message)
 - rust-lang#138442 (EUV: fix place of deref pattern's interior's scrutinee)
 - rust-lang#138457 (Remove usage of legacy scheme paths on RedoxOS)
 - rust-lang#138461 (Remove an outdated line from a test comment)
 - rust-lang#138466 (Remove myself from libs review)

Failed merges:

 - rust-lang#138452 (Remove `RUN_CHECK_WITH_PARALLEL_QUERIES`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 595c624 into rust-lang:master Mar 14, 2025
6 checks passed
@rustbot rustbot added this to the 1.87.0 milestone Mar 14, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 14, 2025
Rollup merge of rust-lang#136230 - clarfonthey:net-memory-layout-assumptions, r=cuviper

Reword incorrect documentation about SocketAddr having varying layout

This has no longer been the case since these types were moved to `core`. The note on portability remains, but it is reworded to not imply that the size varies by target.
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Mar 19, 2025
…mptions, r=cuviper

Reword incorrect documentation about SocketAddr having varying layout

This has no longer been the case since these types were moved to `core`. The note on portability remains, but it is reworded to not imply that the size varies by target.
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Mar 19, 2025
Rollup of 16 pull requests

Successful merges:

 - rust-lang#136001 (Overhaul examples for PermissionsExt)
 - rust-lang#136230 (Reword incorrect documentation about SocketAddr having varying layout)
 - rust-lang#136892 (Sync Fuchsia target spec with clang Fuchsia driver)
 - rust-lang#136911 (Add documentation URL to selected jobs)
 - rust-lang#137870 ( Improve HashMap docs for const and static initializers)
 - rust-lang#138179 (Add `src/tools/x` to the main workspace)
 - rust-lang#138389 (use `expect` instead of `allow`)
 - rust-lang#138396 (Enable metrics and verbose tests in PR CI)
 - rust-lang#138398 (atomic intrinsics: clarify which types are supported and (if applicable) what happens with provenance)
 - rust-lang#138432 (fix: remove the check of lld not supporting `@response-file)`
 - rust-lang#138434 (Visit `PatField` when collecting lint levels)
 - rust-lang#138441 (update error message)
 - rust-lang#138442 (EUV: fix place of deref pattern's interior's scrutinee)
 - rust-lang#138457 (Remove usage of legacy scheme paths on RedoxOS)
 - rust-lang#138461 (Remove an outdated line from a test comment)
 - rust-lang#138466 (Remove myself from libs review)

Failed merges:

 - rust-lang#138452 (Remove `RUN_CHECK_WITH_PARALLEL_QUERIES`)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants