remove redundant defer and improved comments and code in sockaddr

This commit is contained in:
Albert Sundjaja 2025-02-21 23:34:09 +11:00
parent e0e09aef4e
commit 68913c5fc8
4 changed files with 30 additions and 18 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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

View File

@ -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