From 68913c5fc8a6df4f8ca4b28a621163f190388e96 Mon Sep 17 00:00:00 2001 From: Albert Sundjaja Date: Fri, 21 Feb 2025 23:34:09 +1100 Subject: [PATCH] remove redundant defer and improved comments and code in sockaddr --- src/net/unixsock_linux_test.go | 4 ++-- src/net/unixsock_windows_test.go | 4 ++-- src/syscall/syscall_linux.go | 20 +++++++++++++------- src/syscall/syscall_windows.go | 20 +++++++++++++------- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/net/unixsock_linux_test.go b/src/net/unixsock_linux_test.go index 3c8fd85a9f..fa50ba7bd5 100644 --- a/src/net/unixsock_linux_test.go +++ b/src/net/unixsock_linux_test.go @@ -49,7 +49,7 @@ func TestUnixAutobindClose(t *testing.T) { ln.Close() } -func TestUnixAbstractLongNameNullStart(t *testing.T) { +func TestUnixAbstractLongNameNulStart(t *testing.T) { // Create an abstract socket name that starts with a null byte ("\x00") // whose length is the maximum of RawSockaddrUnix Path len paddedAddr := make([]byte, len(syscall.RawSockaddrUnix{}.Path)) @@ -63,7 +63,7 @@ func TestUnixAbstractLongNameNullStart(t *testing.T) { if err != nil { t.Fatal(err) } - defer c.Close() + c.Close() } func TestUnixgramLinuxAbstractLongName(t *testing.T) { diff --git a/src/net/unixsock_windows_test.go b/src/net/unixsock_windows_test.go index c30916c838..4c1a8d7e7e 100644 --- a/src/net/unixsock_windows_test.go +++ b/src/net/unixsock_windows_test.go @@ -70,7 +70,7 @@ func TestUnixConnLocalWindows(t *testing.T) { } } -func TestUnixAbstractLongNameNullStart(t *testing.T) { +func TestUnixAbstractLongNameNulStart(t *testing.T) { if !windows.SupportUnixSocket() { t.Skip("unix test") } @@ -88,7 +88,7 @@ func TestUnixAbstractLongNameNullStart(t *testing.T) { if err != nil { t.Fatal(err) } - defer c.Close() + c.Close() } func TestModeSocket(t *testing.T) { diff --git a/src/syscall/syscall_linux.go b/src/syscall/syscall_linux.go index 7e3443e106..3c73a09ff2 100644 --- a/src/syscall/syscall_linux.go +++ b/src/syscall/syscall_linux.go @@ -550,7 +550,12 @@ func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) { if n > len(sa.raw.Path) { return nil, 0, EINVAL } + // Abstract addresses start with NUL. + // '@' is also a valid way to specify abstract addresses. isAbstract := n > 0 && (name[0] == '@' || name[0] == '\x00') + + // Non-abstract addresses is NUL terminated. + // The length can't use the full capacity as we need to add NUL. if n == len(sa.raw.Path) && !isAbstract { return nil, 0, EINVAL } @@ -558,15 +563,16 @@ func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, _Socklen, error) { for i := 0; i < n; i++ { sa.raw.Path[i] = int8(name[i]) } - // length is family (uint16), name, NUL. - sl := _Socklen(2) - if n > 0 { - sl += _Socklen(n) + 1 - } + // Length is family + name (+ NUL if non-abstract). + // Family is of type uint16 (2 bytes). + sl := _Socklen(2 + n) if isAbstract { + // Abstract addresses are not NUL terminated. + // We rewrite '@' prefix to NUL here. sa.raw.Path[0] = 0 - // Don't count trailing NUL for abstract address. - sl-- + } else { + // Add NUL for non-abstract addresses. + sl++ } return unsafe.Pointer(&sa.raw), sl, nil diff --git a/src/syscall/syscall_windows.go b/src/syscall/syscall_windows.go index 6bc2c5a6bb..da72c72e3b 100644 --- a/src/syscall/syscall_windows.go +++ b/src/syscall/syscall_windows.go @@ -858,7 +858,12 @@ func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, int32, error) { if n > len(sa.raw.Path) { return nil, 0, EINVAL } + // Abstract addresses start with NUL. + // '@' is also a valid way to specify abstract addresses. isAbstract := n > 0 && (name[0] == '@' || name[0] == '\x00') + + // Non-abstract addresses is NUL terminated. + // The length can't use the full capacity as we need to add NUL. if n == len(sa.raw.Path) && !isAbstract { return nil, 0, EINVAL } @@ -866,15 +871,16 @@ func (sa *SockaddrUnix) sockaddr() (unsafe.Pointer, int32, error) { for i := 0; i < n; i++ { sa.raw.Path[i] = int8(name[i]) } - // length is family (uint16), name, NUL. - sl := int32(2) - if n > 0 { - sl += int32(n) + 1 - } + // Length is family + name (+ NUL if non-abstract). + // Family is of type uint16 (2 bytes). + sl := _Socklen(2 + n) if isAbstract { + // Abstract addresses are not NUL terminated. + // We rewrite '@' prefix to NUL here. sa.raw.Path[0] = 0 - // Don't count trailing NUL for abstract address. - sl-- + } else { + // Add NUL for non-abstract addresses. + sl++ } return unsafe.Pointer(&sa.raw), sl, nil