Skip to content
Permalink
Browse files

unix: comply with -d=checkptr in Test_anyToSockaddr

Rather than directly casting between types of different sizes, use an
explicit copy in a helper function.

Fixes golang/go#35106

Change-Id: I2293eb38fa53205ce069e2e5405262294c9ad412
Reviewed-on: https://go-review.googlesource.com/c/sys/+/202820
Run-TryBot: Matt Layher <mdlayher@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
  • Loading branch information
mdlayher committed Oct 23, 2019
1 parent 742c48e commit b69606af412f43a225c1cf2044c90e317f41ae09
Showing with 20 additions and 6 deletions.
  1. +20 −6 unix/syscall_internal_linux_test.go
@@ -30,7 +30,7 @@ func Test_anyToSockaddr(t *testing.T) {
},
{
name: "AF_TIPC NameSeq",
rsa: (*RawSockaddrAny)(unsafe.Pointer(&RawSockaddrTIPC{
rsa: sockaddrTIPCToAny(RawSockaddrTIPC{
Family: AF_TIPC,
Addrtype: TIPC_SERVICE_RANGE,
Scope: 1,
@@ -39,7 +39,7 @@ func Test_anyToSockaddr(t *testing.T) {
Lower: 2,
Upper: 3,
}).tipcAddr(),
})),
}),
sa: &SockaddrTIPC{
Scope: 1,
Addr: &TIPCServiceRange{
@@ -51,7 +51,7 @@ func Test_anyToSockaddr(t *testing.T) {
},
{
name: "AF_TIPC Name",
rsa: (*RawSockaddrAny)(unsafe.Pointer(&RawSockaddrTIPC{
rsa: sockaddrTIPCToAny(RawSockaddrTIPC{
Family: AF_TIPC,
Addrtype: TIPC_SERVICE_ADDR,
Scope: 2,
@@ -60,7 +60,7 @@ func Test_anyToSockaddr(t *testing.T) {
Instance: 2,
Domain: 3,
}).tipcAddr(),
})),
}),
sa: &SockaddrTIPC{
Scope: 2,
Addr: &TIPCServiceName{
@@ -72,15 +72,15 @@ func Test_anyToSockaddr(t *testing.T) {
},
{
name: "AF_TIPC ID",
rsa: (*RawSockaddrAny)(unsafe.Pointer(&RawSockaddrTIPC{
rsa: sockaddrTIPCToAny(RawSockaddrTIPC{
Family: AF_TIPC,
Addrtype: TIPC_SOCKET_ADDR,
Scope: 3,
Addr: (&TIPCSocketAddr{
Ref: 1,
Node: 2,
}).tipcAddr(),
})),
}),
sa: &SockaddrTIPC{
Scope: 3,
Addr: &TIPCSocketAddr{
@@ -214,3 +214,17 @@ func TestSockaddrTIPC_sockaddr(t *testing.T) {
})
}
}

func sockaddrTIPCToAny(in RawSockaddrTIPC) *RawSockaddrAny {
var out RawSockaddrAny

// Explicitly copy the contents of in into out to produce the correct
// sockaddr structure, without relying on unsafe casting to a type of a
// larger size.
copy(
(*(*[SizeofSockaddrAny]byte)(unsafe.Pointer(&out)))[:],
(*(*[SizeofSockaddrTIPC]byte)(unsafe.Pointer(&in)))[:],
)

return &out
}

0 comments on commit b69606a

Please sign in to comment.
You can’t perform that action at this time.