Skip to content

Commit 68981bf

Browse files
committed
syscall: make TestGetdirentries checkptr safe
Fixes Darwin. Updates #35092 Change-Id: I045f070c8549d00610b459e3a82cac870d9ddb54 Reviewed-on: https://go-review.googlesource.com/c/go/+/203077 Run-TryBot: Brad Fitzpatrick <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent a52c0a1 commit 68981bf

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/syscall/getdirentries_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ func testGetdirentries(t *testing.T, count int) {
6666
}
6767
data := buf[:n]
6868
for len(data) > 0 {
69-
dirent := (*syscall.Dirent)(unsafe.Pointer(&data[0]))
69+
// syscall.Getdirentries's return value may be (and usually is) much
70+
// smaller than a syscall.Dirent, which has lots of padding for
71+
// the name at the end. The compiler's checkptr validation doesn't like
72+
// that. So allocate direntMem that's always big enough, and use that
73+
// when converting to *syscall.Dirent.
74+
var direntMem [unsafe.Sizeof(syscall.Dirent{})]byte
75+
copy(direntMem[:], data)
76+
dirent := (*syscall.Dirent)(unsafe.Pointer(&direntMem[0]))
77+
7078
data = data[dirent.Reclen:]
7179
name := make([]byte, dirent.Namlen)
7280
for i := 0; i < int(dirent.Namlen); i++ {

0 commit comments

Comments
 (0)