Skip to content

std::net: clamp multicast ttl value to u8 max.#154113

Open
devnexen wants to merge 4 commits intorust-lang:mainfrom
devnexen:socket_mod_ttl_fix
Open

std::net: clamp multicast ttl value to u8 max.#154113
devnexen wants to merge 4 commits intorust-lang:mainfrom
devnexen:socket_mod_ttl_fix

Conversation

@devnexen
Copy link
Copy Markdown
Contributor

on some platforms the underlying type is c_uchar,
values above 255 would silently truncate.

on some platforms the underlying type is c_uchar,
values above 255 would silently truncate.
@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 Mar 19, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 19, 2026

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 8 candidates
  • Random selection from Mark-Simulacrum, joboet

@Mark-Simulacrum Mark-Simulacrum added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Mar 27, 2026
@Mark-Simulacrum
Copy link
Copy Markdown
Member

Nominating for libs-api to decide if we want to truncate in std or not. AFAICT, the choice of u32 for the API is historical (I see it's been defined as such since the RFC proposing these, rust-lang/rfcs#1461).

The underlying IPv4 packet only has space for u8 (255) TTL, which is what constrains this under the hood. I think the primary alternative is to panic or return an Err in userspace. Linux returns EINVAL on values > 255 (https://github.com/torvalds/linux/blob/7df48e36313029e4c0907b2023905dd7213fd678/net/ipv4/ip_sockglue.c#L1051-L1052) so we'd technically be hiding an error here for at least Linux.

Maybe the right thing is to change the as-cast into .try_into()? or so?

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-t-libs-api Status: Awaiting decision from T-libs-api and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 29, 2026
@nia-e
Copy link
Copy Markdown
Member

nia-e commented Mar 31, 2026

It seems like on most platforms including Linux, giving a TTL over 255 will return EINVAL - so in the @rust-lang/libs-api meeting, we decided that we'd rather just error here instead of clamping. A similar PR would be welcome for other cases where we set TTL, because this only addresses the multicast case

@nia-e nia-e added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed I-libs-api-nominated Nominated for discussion during a libs-api team meeting. S-waiting-on-t-libs-api Status: Awaiting decision from T-libs-api labels Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

5 participants