Skip to content

Commit 727590c

Browse files
committed
unix: avoid "just past the end" pointers in UnixRights
Same as CL 201617 did for package syscall. Caught with -d=checkptr Updates golang/go#22218 Change-Id: I8208f8e6d9bd62376bf9e0458dc18956daabd785 Reviewed-on: https://go-review.googlesource.com/c/sys/+/201937 Run-TryBot: Tobias Klauser <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Elias Naur <[email protected]>
1 parent b09406a commit 727590c

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

unix/sockcmsg_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func UnixCredentials(ucred *Ucred) []byte {
1717
h.Level = SOL_SOCKET
1818
h.Type = SCM_CREDENTIALS
1919
h.SetLen(CmsgLen(SizeofUcred))
20-
*((*Ucred)(cmsgData(h))) = *ucred
20+
*(*Ucred)(h.data(0)) = *ucred
2121
return b
2222
}
2323

unix/sockcmsg_unix.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ func CmsgSpace(datalen int) int {
5050
return cmsgAlignOf(SizeofCmsghdr) + cmsgAlignOf(datalen)
5151
}
5252

53-
func cmsgData(h *Cmsghdr) unsafe.Pointer {
54-
return unsafe.Pointer(uintptr(unsafe.Pointer(h)) + uintptr(cmsgAlignOf(SizeofCmsghdr)))
53+
func (h *Cmsghdr) data(offset uintptr) unsafe.Pointer {
54+
return unsafe.Pointer(uintptr(unsafe.Pointer(h)) + uintptr(cmsgAlignOf(SizeofCmsghdr)) + offset)
5555
}
5656

5757
// SocketControlMessage represents a socket control message.
@@ -94,10 +94,8 @@ func UnixRights(fds ...int) []byte {
9494
h.Level = SOL_SOCKET
9595
h.Type = SCM_RIGHTS
9696
h.SetLen(CmsgLen(datalen))
97-
data := cmsgData(h)
98-
for _, fd := range fds {
99-
*(*int32)(data) = int32(fd)
100-
data = unsafe.Pointer(uintptr(data) + 4)
97+
for i, fd := range fds {
98+
*(*int32)(h.data(4 * uintptr(i))) = int32(fd)
10199
}
102100
return b
103101
}

0 commit comments

Comments
 (0)