From 5e9cceab810b23beb4a73208cd5326bcf9805e5a Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Fri, 18 Feb 2022 12:42:15 +0100 Subject: [PATCH] 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 --- serial_unix.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/serial_unix.go b/serial_unix.go index 6a492ad..d7c7078 100644 --- a/serial_unix.go +++ b/serial_unix.go @@ -72,7 +72,11 @@ func (port *unixPort) Read(p []byte) (int, error) { for { timeout := time.Duration(-1) 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) if err == unix.EINTR {