Skip to content

fix: Refactor usages of unsafe #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
5 changes: 2 additions & 3 deletions pkg/utils/utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"net"
"syscall"
"unsafe"

"github.com/pkg/errors"
"github.com/vishvananda/netlink"
Expand Down Expand Up @@ -38,7 +37,7 @@ func OpenRawSocket(index int) (int, error) {
func htons(i uint16) uint16 {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, i)
return *(*uint16)(unsafe.Pointer(&b[0]))
return binary.BigEndian.Uint16(b)
}

// https://gist.github.com/ammario/649d4c0da650162efd404af23e25b86b
Expand Down Expand Up @@ -78,7 +77,7 @@ func HostToNetShort(i uint16) uint16 {
func determineEndian() binary.ByteOrder {
var endian binary.ByteOrder
buf := [2]byte{}
*(*uint16)(unsafe.Pointer(&buf[0])) = uint16(0xABCD)
binary.BigEndian.PutUint16(buf[:], uint16(0xABCD)) //nolint:gomnd // endian test value

switch buf {
case [2]byte{0xCD, 0xAB}:
Expand Down
Loading