Skip to content

Commit 72f772c

Browse files
ericlagergrengopherbot
authored andcommitted
unix: offs2lohi should shift by bits, not bytes
Fixes golang/go#57291 Change-Id: I212ab8a9f47563d9c124a0f20c17df35d7aa8e6d Reviewed-on: https://go-review.googlesource.com/c/sys/+/457315 Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Than McIntosh <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]>
1 parent cffae8e commit 72f772c

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

unix/syscall_linux.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,12 +1986,10 @@ func bytes2iovec(bs [][]byte) []Iovec {
19861986
return iovecs
19871987
}
19881988

1989-
// offs2lohi splits offs into its lower and upper unsigned long. On 64-bit
1990-
// systems, hi will always be 0. On 32-bit systems, offs will be split in half.
1991-
// preadv/pwritev chose this calling convention so they don't need to add a
1992-
// padding-register for alignment on ARM.
1989+
// offs2lohi splits offs into its low and high order bits.
19931990
func offs2lohi(offs int64) (lo, hi uintptr) {
1994-
return uintptr(offs), uintptr(uint64(offs) >> SizeofLong)
1991+
const longBits = SizeofLong * 8
1992+
return uintptr(offs), uintptr(uint64(offs) >> longBits)
19951993
}
19961994

19971995
func Readv(fd int, iovs [][]byte) (n int, err error) {

unix/syscall_linux_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,3 +1099,36 @@ func TestIoctlFileDedupeRange(t *testing.T) {
10991099
dedupe.Info[1].Bytes_deduped, 0)
11001100
}
11011101
}
1102+
1103+
// TestPwritevOffsets tests golang.org/issues/57291 where
1104+
// offs2lohi was shifting by the size of long in bytes, not bits.
1105+
func TestPwritevOffsets(t *testing.T) {
1106+
path := filepath.Join(t.TempDir(), "x.txt")
1107+
1108+
f, err := os.Create(path)
1109+
if err != nil {
1110+
t.Fatal(err)
1111+
}
1112+
t.Cleanup(func() { f.Close() })
1113+
1114+
const (
1115+
off = 20
1116+
)
1117+
b := [][]byte{{byte(0)}}
1118+
n, err := unix.Pwritev(int(f.Fd()), b, off)
1119+
if err != nil {
1120+
t.Fatal(err)
1121+
}
1122+
if n != len(b) {
1123+
t.Fatalf("expected to write %d, wrote %d", len(b), n)
1124+
}
1125+
1126+
info, err := f.Stat()
1127+
if err != nil {
1128+
t.Fatal(err)
1129+
}
1130+
want := off + int64(len(b))
1131+
if info.Size() != want {
1132+
t.Fatalf("expected size to be %d, got %d", want, info.Size())
1133+
}
1134+
}

0 commit comments

Comments
 (0)