Description
In SntpRequest::new(), the UDP socket bind uses .unwrap():
pub fn new() -> SntpRequest {
let sntp = SntpRequest {
socket: UdpSocket::bind("0.0.0.0:0").unwrap(),
// ...
};
If the bind fails (e.g., no network, permission denied, all ephemeral ports exhausted), this causes an unrecoverable panic instead of a graceful error.
Suggested fix
Change the constructor to return Result:
pub fn new() -> io::Result<SntpRequest> {
Ok(SntpRequest {
socket: UdpSocket::bind("0.0.0.0:0")?,
// ...
})
}
Note: This is a breaking API change. Alternatively, document that new() may panic, or provide a try_new() method.
Also, "0.0.0.0:0" only binds to IPv4. For IPv6 support, consider binding to [::]:0 or providing an option to choose.
Description
In
SntpRequest::new(), the UDP socket bind uses.unwrap():If the bind fails (e.g., no network, permission denied, all ephemeral ports exhausted), this causes an unrecoverable panic instead of a graceful error.
Suggested fix
Change the constructor to return
Result:Note: This is a breaking API change. Alternatively, document that
new()may panic, or provide atry_new()method.Also,
"0.0.0.0:0"only binds to IPv4. For IPv6 support, consider binding to[::]:0or providing an option to choose.