From: Devon Mar Date: Tue, 25 Nov 2025 07:38:09 +0000 (+0000) Subject: net: parse addresses without separators in ParseMac X-Git-Tag: go1.26rc1~21 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=043b9de65825abef6d8946affda075a777dfb322;p=gostls13.git net: parse addresses without separators in ParseMac IEEE EUI guidelines states that "an EUI-48 can be represented in the IEEE RA hexadecimal (hex) form with the octets separated by hyphens, or as a pure base-16 numerical representation without hyphens" (https://standards.ieee.org/wp-content/uploads/import/documents/tutorials/eui.pdf p.9). The latter form is used in Azure Instance Metadata Service (https://github.com/Azure/azure-container-networking/pull/4122) among others. Fixes #66682 Change-Id: Id66c23d50ebb1fed1f3bdb5cf3822a8fd60b886d GitHub-Last-Rev: 77900cc1a68cb535b685a63cf84b5413b480df2c GitHub-Pull-Request: golang/go#76387 Reviewed-on: https://go-review.googlesource.com/c/go/+/722720 Auto-Submit: Damien Neil Reviewed-by: Damien Neil Reviewed-by: Dmitri Shuralyov LUCI-TryBot-Result: Go LUCI --- diff --git a/src/net/mac.go b/src/net/mac.go index 53d5b2dbf5..b69a42d40b 100644 --- a/src/net/mac.go +++ b/src/net/mac.go @@ -36,8 +36,9 @@ func (a HardwareAddr) String() string { // 0000.5e00.5301 // 0200.5e10.0000.0001 // 0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001 +// 00005e005301 func ParseMAC(s string) (hw HardwareAddr, err error) { - if len(s) < 14 { + if len(s) < 12 { goto error } @@ -77,7 +78,23 @@ func ParseMAC(s string) (hw HardwareAddr, err error) { x += 5 } } else { - goto error + if len(s)%2 != 0 { + goto error + } + + n := len(s) / 2 + if n != 6 && n != 8 && n != 20 { + goto error + } + + hw = make(HardwareAddr, len(s)/2) + for x, i := 0, 0; i < n; i++ { + var ok bool + if hw[i], ok = xtoi2(s[x:x+2], 0); !ok { + goto error + } + x += 2 + } } return hw, nil diff --git a/src/net/mac_test.go b/src/net/mac_test.go index cad884fcf5..9bfad4f12e 100644 --- a/src/net/mac_test.go +++ b/src/net/mac_test.go @@ -19,11 +19,13 @@ var parseMACTests = []struct { {"00:00:5e:00:53:01", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""}, {"00-00-5e-00-53-01", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""}, {"0000.5e00.5301", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""}, + {"00005e005301", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""}, // See RFC 7042, Section 2.2.2. {"02:00:5e:10:00:00:00:01", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""}, {"02-00-5e-10-00-00-00-01", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""}, {"0200.5e10.0000.0001", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""}, + {"02005e1000000001", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""}, // See RFC 4391, Section 9.1.1. { @@ -53,6 +55,15 @@ var parseMACTests = []struct { }, "", }, + { + "00000000fe8000000000000002005e1000000001", + HardwareAddr{ + 0x00, 0x00, 0x00, 0x00, + 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01, + }, + "", + }, {"ab:cd:ef:AB:CD:EF", HardwareAddr{0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef}, ""}, {"ab:cd:ef:AB:CD:EF:ab:cd", HardwareAddr{0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd}, ""}, @@ -78,6 +89,7 @@ var parseMACTests = []struct { {"01:02-03-04-05-06", nil, "invalid MAC address"}, {"0123:4567:89AF", nil, "invalid MAC address"}, {"0123-4567-89AF", nil, "invalid MAC address"}, + {"0123456789AF0", nil, "invalid MAC address"}, } func TestParseMAC(t *testing.T) {