From: qmuntal Date: Tue, 19 Aug 2025 10:10:54 +0000 (+0200) Subject: internal/poll: set the correct file offset in FD.Seek for Windows overlapped handles X-Git-Tag: go1.26rc1~1053 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=853fc12739;p=gostls13.git internal/poll: set the correct file offset in FD.Seek for Windows overlapped handles Windows doesn't keep the file pointer for overlapped file handles. To work around this, we keep track of the current offset ourselves and use it on every Read/Write operation. When the user calls File.Seek with whence == io.SeekCurrent, it expects that the offset we keep track of is also accounted for, else the the seek'ed value won't match the file pointer seen by the user. Updates #74951. Fixes #75081. Change-Id: Ieca7c3779e5349292883ffc293a8474088a4dec7 Reviewed-on: https://go-review.googlesource.com/c/go/+/697275 LUCI-TryBot-Result: Go LUCI Reviewed-by: Damien Neil Reviewed-by: Dmitri Shuralyov --- diff --git a/src/internal/poll/fd_windows.go b/src/internal/poll/fd_windows.go index 1748e5ef5a..52571d5fe0 100644 --- a/src/internal/poll/fd_windows.go +++ b/src/internal/poll/fd_windows.go @@ -1107,6 +1107,12 @@ func (fd *FD) Seek(offset int64, whence int) (int64, error) { fd.l.Lock() defer fd.l.Unlock() + if !fd.isBlocking && whence == io.SeekCurrent { + // Windows doesn't keep the file pointer for overlapped file handles. + // We do it ourselves in case to account for any read or write + // operations that may have occurred. + offset += fd.offset + } n, err := syscall.Seek(fd.Sysfd, offset, whence) fd.setOffset(n) return n, err diff --git a/src/os/os_windows_test.go b/src/os/os_windows_test.go index 5a479051ee..3cdaad32c7 100644 --- a/src/os/os_windows_test.go +++ b/src/os/os_windows_test.go @@ -1845,6 +1845,44 @@ func TestFile(t *testing.T) { } } +func TestFileOverlappedSeek(t *testing.T) { + t.Parallel() + name := filepath.Join(t.TempDir(), "foo") + f := newFileOverlapped(t, name, true) + content := []byte("foo") + if _, err := f.Write(content); err != nil { + t.Fatal(err) + } + // Check that the file pointer is at the expected offset. + n, err := f.Seek(0, io.SeekCurrent) + if err != nil { + t.Fatal(err) + } + if n != int64(len(content)) { + t.Errorf("expected file pointer to be at offset %d, got %d", len(content), n) + } + // Set the file pointer to the start of the file. + if _, err := f.Seek(0, io.SeekStart); err != nil { + t.Fatal(err) + } + // Read the first byte. + var buf [1]byte + if _, err := f.Read(buf[:]); err != nil { + t.Fatal(err) + } + if !bytes.Equal(buf[:], content[:len(buf)]) { + t.Errorf("expected %q, got %q", content[:len(buf)], buf[:]) + } + // Check that the file pointer is at the expected offset. + n, err = f.Seek(0, io.SeekCurrent) + if err != nil { + t.Fatal(err) + } + if n != int64(len(buf)) { + t.Errorf("expected file pointer to be at offset %d, got %d", len(buf), n) + } +} + func TestPipe(t *testing.T) { t.Parallel() r, w, err := os.Pipe()