From d18057917003970322335bd1ad73a68ae6994ccd Mon Sep 17 00:00:00 2001 From: Joel Sing Date: Sat, 4 Jan 2014 00:29:20 +1100 Subject: [PATCH] syscall: handle varied path lengths for unix sockets Most BSDs include the trailing NUL character of the socket path in the length, however some do not (such as NetBSD 6.99). Handle this by only subtracting the family and length bytes from the returned length, then scanning the path and removing any terminating NUL bytes. Fixes #6627. R=golang-codereviews, mikioh.mikioh CC=golang-codereviews https://golang.org/cl/46420044 --- src/pkg/syscall/syscall_bsd.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/pkg/syscall/syscall_bsd.go b/src/pkg/syscall/syscall_bsd.go index 76b1f41b46..fce764028b 100644 --- a/src/pkg/syscall/syscall_bsd.go +++ b/src/pkg/syscall/syscall_bsd.go @@ -221,14 +221,20 @@ func anyToSockaddr(rsa *RawSockaddrAny) (Sockaddr, error) { case AF_UNIX: pp := (*RawSockaddrUnix)(unsafe.Pointer(rsa)) - if pp.Len < 3 || pp.Len > SizeofSockaddrUnix { + if pp.Len < 2 || pp.Len > SizeofSockaddrUnix { return nil, EINVAL } sa := new(SockaddrUnix) - n := int(pp.Len) - 3 // subtract leading Family, Len, terminating NUL + + // Some BSDs include the trailing NUL in the length, whereas + // others do not. Work around this by subtracting the leading + // family and len. The path is then scanned to see if a NUL + // terminator still exists within the length. + n := int(pp.Len) - 2 // subtract leading Family, Len for i := 0; i < n; i++ { if pp.Path[i] == 0 { - // found early NUL; assume Len is overestimating + // found early NUL; assume Len included the NUL + // or was overestimating. n = i break } -- 2.48.1