-
-
Notifications
You must be signed in to change notification settings - Fork 3
Bug: UdpSocket::bind().unwrap() in constructor can panic #11
Copy link
Copy link
Open
Description
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels