From e4699c0c4c16638387c0a718436166bc7b04dfb4 Mon Sep 17 00:00:00 2001 From: Thomas LOMBARD Date: Wed, 4 Feb 2026 11:15:20 +0100 Subject: [PATCH 1/2] Proposed a fix handling negative values on read and write --- serial_posix.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/serial_posix.go b/serial_posix.go index 6d25b2d..aa065d6 100644 --- a/serial_posix.go +++ b/serial_posix.go @@ -1,3 +1,4 @@ +//go:build darwin || linux || freebsd || openbsd || netbsd // +build darwin linux freebsd openbsd netbsd package serial @@ -111,12 +112,14 @@ func (p *port) Read(b []byte) (n int, err error) { return } n, err = syscall.Read(fd, b) + n = max(0, n) return } // Write writes data to the serial port. func (p *port) Write(b []byte) (n int, err error) { n, err = syscall.Write(p.fd, b) + n = max(0, n) return } From fd88095a5490cb3510f1c57f60a3b38675d7bc91 Mon Sep 17 00:00:00 2001 From: Thomas LOMBARD Date: Thu, 5 Feb 2026 09:28:21 +0100 Subject: [PATCH 2/2] Better handling --- serial_posix.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/serial_posix.go b/serial_posix.go index aa065d6..996427e 100644 --- a/serial_posix.go +++ b/serial_posix.go @@ -1,4 +1,3 @@ -//go:build darwin || linux || freebsd || openbsd || netbsd // +build darwin linux freebsd openbsd netbsd package serial @@ -111,15 +110,19 @@ func (p *port) Read(b []byte) (n int, err error) { err = ErrTimeout return } - n, err = syscall.Read(fd, b) - n = max(0, n) + if n, err = syscall.Read(fd, b); err != nil { + return 0, err + } + return } // Write writes data to the serial port. func (p *port) Write(b []byte) (n int, err error) { - n, err = syscall.Write(p.fd, b) - n = max(0, n) + if n, err = syscall.Write(p.fd, b); err != nil { + return 0, err + } + return }