Skip to content

Bug: UdpSocket::bind().unwrap() in constructor can panic #11

@fasuizu-br

Description

@fasuizu-br

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions