From f9c46fb18d68f7edd5bbbb1b6cb505fd9243f6d0 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Thu, 26 May 2022 23:30:31 +0400 Subject: [PATCH] fix: unmarshal HardwareAddr without stdlib help Stdlib `net.ParseMAC` does lots of validations, but some hardware addrs we can see (on logical interfaces) are not valid, so parse MACs in a simple way. Signed-off-by: Andrey Smirnov --- pkg/machinery/nethelpers/hwaddr.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pkg/machinery/nethelpers/hwaddr.go b/pkg/machinery/nethelpers/hwaddr.go index c532e8ac7..6a0e71ea4 100644 --- a/pkg/machinery/nethelpers/hwaddr.go +++ b/pkg/machinery/nethelpers/hwaddr.go @@ -4,7 +4,11 @@ package nethelpers -import "net" +import ( + "bytes" + "encoding/hex" + "net" +) // HardwareAddr wraps net.HardwareAddr for YAML marshaling. type HardwareAddr net.HardwareAddr @@ -16,12 +20,17 @@ func (addr HardwareAddr) MarshalText() ([]byte, error) { // UnmarshalText implements text.Unmarshaler interface. func (addr *HardwareAddr) UnmarshalText(b []byte) error { - mac, err := net.ParseMAC(string(b)) + rawHex := bytes.ReplaceAll(b, []byte(":"), []byte("")) + dstLen := hex.DecodedLen(len(rawHex)) + + dst := make([]byte, dstLen) + + n, err := hex.Decode(dst, rawHex) if err != nil { return err } - *addr = HardwareAddr(mac) + *addr = HardwareAddr(dst[:n]) return nil }