Fix potential small timeout bug on unix implementation

A very small port.readTimeout may lead to a negative timeout if the
elapsed time between:

    deadline = time.Now().Add(port.readTimeout)

and

    timeout = time.Until(deadline)

is longer than port.readTimeout.

Fix #134
This commit is contained in:
Cristian Maglie
2022-02-18 12:42:15 +01:00
parent de2de784c4
commit 5e9cceab81

View File

@@ -72,7 +72,11 @@ func (port *unixPort) Read(p []byte) (int, error) {
for { for {
timeout := time.Duration(-1) timeout := time.Duration(-1)
if port.readTimeout != NoTimeout { if port.readTimeout != NoTimeout {
timeout = deadline.Sub(time.Now()) timeout = time.Until(deadline)
if timeout < 0 {
// a negative timeout means "no-timeout" in Select(...)
timeout = 0
}
} }
res, err := unixutils.Select(fds, nil, fds, timeout) res, err := unixutils.Select(fds, nil, fds, timeout)
if err == unix.EINTR { if err == unix.EINTR {