]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: handle varied path lengths for unix sockets
authorJoel Sing <jsing@google.com>
Fri, 3 Jan 2014 13:29:20 +0000 (00:29 +1100)
committerJoel Sing <jsing@google.com>
Fri, 3 Jan 2014 13:29:20 +0000 (00:29 +1100)
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

index 76b1f41b46aeafad87bbc206df412aedcbce18c0..fce764028b7917ce884c32eb7953b3c504bec80e 100644 (file)
@@ -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
                        }