diff --git a/library/std/src/net/udp.rs b/library/std/src/net/udp.rs index 136803ab16f1f..cf19cad4c0395 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 value is byte-sized, + /// any value above 255 will result in an error. + /// /// # Examples /// /// ```no_run diff --git a/library/std/src/sys/net/connection/socket/mod.rs b/library/std/src/sys/net/connection/socket/mod.rs index 256b99dfa9874..aa5eefe2dd5e0 100644 --- a/library/std/src/sys/net/connection/socket/mod.rs +++ b/library/std/src/sys/net/connection/socket/mod.rs @@ -759,13 +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 { - setsockopt( - &self.inner, - c::IPPROTO_IP, - c::IP_MULTICAST_TTL, - multicast_ttl_v4 as IpV4MultiCastType, - ) + setsockopt(&self.inner, c::IPPROTO_IP, c::IP_MULTICAST_TTL, ttl as IpV4MultiCastType) } }