Skip to content

Special case assume_init for Bool on Windows#655

Merged
Thomasdezeeuw merged 1 commit into
rust-lang:masterfrom
ChrisDenton:init-bool
May 27, 2026
Merged

Special case assume_init for Bool on Windows#655
Thomasdezeeuw merged 1 commit into
rust-lang:masterfrom
ChrisDenton:init-bool

Conversation

@ChrisDenton
Copy link
Copy Markdown
Member

On Windows many socket options are documented as having the value "DWORD (boolean)". A DWORD is 4 bytes but when writing a boolean, often only one byte is actually written. While this is mostly consistent, it has been observed that using getsockopt with the IPV6_V6ONLY option can sometimes write 1 byte but other time write 4 bytes. So this PR allows us to accept either 1 or 4 bytes when the type of the option is Bool.

Fixes #564

Comment thread src/sys/windows.rs Outdated
}
}

impl<T: Copy> AssumeInit for T {}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Note that this fakes specialisation by not implementing Copy for the type (i.e. Bool) we want to special case. This is fine for our usage since we only actually need to move values and Bool is private.

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.

What other types is this required by? Could we just explicitly implement it for the 3 or 4 other types?

Copy link
Copy Markdown
Member Author

@ChrisDenton ChrisDenton May 25, 2026

Choose a reason for hiding this comment

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

It would currently need to be implemented for i32, u32, IN_ADDR, linger and WSAPROTOCOL_INFOW. Which seems fine. My only reluctance was that more things could be added in the future.

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.

I would consider doing that and renaming this to GetsockoptOutput or similar.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done!

Copy link
Copy Markdown
Collaborator

@Thomasdezeeuw Thomasdezeeuw left a comment

Choose a reason for hiding this comment

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

This seems like a reasonable solution. Though I'm not super happy that we need to add this much code.

Comment thread src/sys/windows.rs Outdated
///
/// # Safety
///
/// `size` must be either 1 or 4 and that many bytes must have been initialized.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: we don't need this documentation, the trait itself already explains it and the implementation does the rest.

Comment thread src/sys/windows.rs Outdated
debug_assert_eq!(optlen as usize, mem::size_of::<T>());
// Safety: `getsockopt` initialised `optval` for us.
optval.assume_init()
T::assume_init(optval, optlen as usize)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: do we want change the trait to accept an c_int? That way we don't need the cast here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure, that does make sense to me.

@Thomasdezeeuw Thomasdezeeuw enabled auto-merge (rebase) May 27, 2026 15:37
@Thomasdezeeuw Thomasdezeeuw disabled auto-merge May 27, 2026 15:37
@Thomasdezeeuw Thomasdezeeuw enabled auto-merge (squash) May 27, 2026 15:37
@Thomasdezeeuw
Copy link
Copy Markdown
Collaborator

GitHub Actions was stuck (what else is new), so I force pushed the commit to get it to start.

@Thomasdezeeuw Thomasdezeeuw disabled auto-merge May 27, 2026 15:42
@Thomasdezeeuw Thomasdezeeuw merged commit 5f64403 into rust-lang:master May 27, 2026
52 checks passed
@Thomasdezeeuw
Copy link
Copy Markdown
Collaborator

Thanks @ChrisDenton

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.

only_v6 panic on windows

3 participants