From a29d5cfa572af8f28540e72c6ff5cde777d94aa7 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Thu, 19 Mar 2026 20:40:24 +0000 Subject: [PATCH 1/4] std::net: clamp multicast ttl value to u8 max. on some platforms the underlying type is c_uchar, values above 255 would silently truncate. --- library/std/src/sys/net/connection/socket/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/sys/net/connection/socket/mod.rs b/library/std/src/sys/net/connection/socket/mod.rs index 256b99dfa9874..9aa99be40a5f3 100644 --- a/library/std/src/sys/net/connection/socket/mod.rs +++ b/library/std/src/sys/net/connection/socket/mod.rs @@ -764,7 +764,7 @@ impl UdpSocket { &self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL, - multicast_ttl_v4 as IpV4MultiCastType, + cmp::min(multicast_ttl_v4, u8::MAX as u32) as IpV4MultiCastType, ) } } From 80bae85ba2ae7796b3a68e1d23ea3077aa675164 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Thu, 19 Mar 2026 21:24:31 +0000 Subject: [PATCH 2/4] addressing feedback --- library/std/src/net/udp.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/std/src/net/udp.rs b/library/std/src/net/udp.rs index 136803ab16f1f..37221ab3dda45 100644 --- a/library/std/src/net/udp.rs +++ b/library/std/src/net/udp.rs @@ -478,6 +478,9 @@ impl UdpSocket { /// /// Note that this might not have any effect on IPv6 sockets. /// + /// Since the underlying socket option type is a byte-sized value + /// on some platforms, any value above 255 will be clamped to 255. + /// /// # Examples /// /// ```no_run From 1e444b40e187e0d274d09e2248c483bc89f95d4b Mon Sep 17 00:00:00 2001 From: David Carlier Date: Tue, 31 Mar 2026 18:24:13 +0100 Subject: [PATCH 3/4] address feedback --- library/std/src/net/udp.rs | 4 ++-- library/std/src/sys/net/connection/socket/mod.rs | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/library/std/src/net/udp.rs b/library/std/src/net/udp.rs index 37221ab3dda45..cf19cad4c0395 100644 --- a/library/std/src/net/udp.rs +++ b/library/std/src/net/udp.rs @@ -478,8 +478,8 @@ impl UdpSocket { /// /// Note that this might not have any effect on IPv6 sockets. /// - /// Since the underlying socket option type is a byte-sized value - /// on some platforms, any value above 255 will be clamped to 255. + /// Since the underlying socket option value is byte-sized, + /// any value above 255 will result in an error. /// /// # Examples /// diff --git a/library/std/src/sys/net/connection/socket/mod.rs b/library/std/src/sys/net/connection/socket/mod.rs index 9aa99be40a5f3..fe75b2ed10890 100644 --- a/library/std/src/sys/net/connection/socket/mod.rs +++ b/library/std/src/sys/net/connection/socket/mod.rs @@ -760,12 +760,10 @@ impl UdpSocket { pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> { unsafe { - setsockopt( - &self.inner, - c::IPPROTO_IP, - c::IP_MULTICAST_TTL, - cmp::min(multicast_ttl_v4, u8::MAX as u32) as IpV4MultiCastType, - ) + let ttl: IpV4MultiCastType = multicast_ttl_v4 + .try_into() + .map_err(|_| io::Error::from(io::ErrorKind::InvalidInput))?; + setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL, ttl) } } From 6f309780ce06a626a17fb5afdb79c81a0682e88f Mon Sep 17 00:00:00 2001 From: David Carlier Date: Thu, 2 Apr 2026 12:52:50 +0100 Subject: [PATCH 4/4] convert to u8 early on before casting to the platform type --- library/std/src/sys/net/connection/socket/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/std/src/sys/net/connection/socket/mod.rs b/library/std/src/sys/net/connection/socket/mod.rs index fe75b2ed10890..aa5eefe2dd5e0 100644 --- a/library/std/src/sys/net/connection/socket/mod.rs +++ b/library/std/src/sys/net/connection/socket/mod.rs @@ -759,11 +759,11 @@ impl UdpSocket { } pub fn set_multicast_ttl_v4(&self, multicast_ttl_v4: u32) -> io::Result<()> { + let ttl: u8 = multicast_ttl_v4 + .try_into() + .map_err(|_| io::Error::from(io::ErrorKind::InvalidInput))?; unsafe { - let ttl: IpV4MultiCastType = multicast_ttl_v4 - .try_into() - .map_err(|_| io::Error::from(io::ErrorKind::InvalidInput))?; - setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL, ttl) + setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL, ttl as IpV4MultiCastType) } }