Skip to content

Commit

Permalink
fix link statistics pointer alignment
Browse files Browse the repository at this point in the history
gc's `-d checkptr` option reported the following problem:
```
--- FAIL: TestAddrAdd (0.00s)
panic: runtime error: unsafe pointer conversion [recovered]
	panic: runtime error: unsafe pointer conversion

goroutine 19 [running]:
testing.tRunner.func1(0xc00011a240)
	go/src/testing/testing.go:916 +0xaeb
panic(0x7f5c80, 0xc00013a100)
	go/src/runtime/panic.go:973 +0x396
github.com/vishvananda/netlink.LinkDeserialize(0x0, 0xc000174010, 0x514, 0x514, 0x1, 0x1, 0x0, 0x0)
	netlink/link_linux.go:1674 +0x33c5
github.com/vishvananda/netlink.execGetLink(0xc0001540a0, 0xc00013a0a0, 0x2, 0x2, 0x3)
	netlink/link_linux.go:1495 +0x205
github.com/vishvananda/netlink.(*Handle).LinkByName(0xa95e70, 0x81c4c9, 0x2, 0x4, 0x4, 0xc, 0x1)
	netlink/link_linux.go:1415 +0x578
github.com/vishvananda/netlink.LinkByName(...)
	netlink/link_linux.go:1395
github.com/vishvananda/netlink.DoTestAddr(0xc00011a240, 0x82d878)
	netlink/addr_test.go:62 +0xfbc
github.com/vishvananda/netlink.TestAddrAdd(0xc00011a240)
	netlink/addr_test.go:15 +0x45
testing.tRunner(0xc00011a240, 0x82d898)
	go/src/testing/testing.go:954 +0x1ec
created by testing.(*T).Run
	go/src/testing/testing.go:1005 +0x661
FAIL	github.com/vishvananda/netlink	0.006s
FAIL
```

Make sure the link structures are properly aligned.
  • Loading branch information
benjaminp authored and aboch committed Jan 8, 2020
1 parent ed89313 commit ec20eb4
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1515,8 +1515,8 @@ func LinkDeserialize(hdr *unix.NlMsghdr, m []byte) (Link, error) {
}
var (
link Link
stats32 []byte
stats64 []byte
stats32 *LinkStatistics32
stats64 *LinkStatistics64
linkType string
linkSlave LinkSlave
slaveType string
Expand Down Expand Up @@ -1669,9 +1669,15 @@ func LinkDeserialize(hdr *unix.NlMsghdr, m []byte) (Link, error) {
case unix.IFLA_IFALIAS:
base.Alias = string(attr.Value[:len(attr.Value)-1])
case unix.IFLA_STATS:
stats32 = attr.Value[:]
stats32 = new(LinkStatistics32)
if err := binary.Read(bytes.NewBuffer(attr.Value[:]), nl.NativeEndian(), stats32); err != nil {
return nil, err
}
case unix.IFLA_STATS64:
stats64 = attr.Value[:]
stats64 = new(LinkStatistics64)
if err := binary.Read(bytes.NewBuffer(attr.Value[:]), nl.NativeEndian(), stats64); err != nil {
return nil, err
}
case unix.IFLA_XDP:
xdp, err := parseLinkXdp(attr.Value[:])
if err != nil {
Expand Down Expand Up @@ -1716,9 +1722,9 @@ func LinkDeserialize(hdr *unix.NlMsghdr, m []byte) (Link, error) {
}

if stats64 != nil {
base.Statistics = parseLinkStats64(stats64)
base.Statistics = (*LinkStatistics)(stats64)
} else if stats32 != nil {
base.Statistics = parseLinkStats32(stats32)
base.Statistics = (*LinkStatistics)(stats32.to64())
}

// Links that don't have IFLA_INFO_KIND are hardware devices
Expand Down Expand Up @@ -2483,14 +2489,6 @@ func parseGretunData(link Link, data []syscall.NetlinkRouteAttr) {
}
}

func parseLinkStats32(data []byte) *LinkStatistics {
return (*LinkStatistics)((*LinkStatistics32)(unsafe.Pointer(&data[0:SizeofLinkStats32][0])).to64())
}

func parseLinkStats64(data []byte) *LinkStatistics {
return (*LinkStatistics)((*LinkStatistics64)(unsafe.Pointer(&data[0:SizeofLinkStats64][0])))
}

func addXdpAttrs(xdp *LinkXdp, req *nl.NetlinkRequest) {
attrs := nl.NewRtAttr(unix.IFLA_XDP|unix.NLA_F_NESTED, nil)
b := make([]byte, 4)
Expand Down

0 comments on commit ec20eb4

Please sign in to comment.