Skip to content

Commit 8a2efb0

Browse files
committed
net: parse addresses without separators in ParseMac
Fixes #66682
1 parent 4b0e3cc commit 8a2efb0

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/net/mac.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ func (a HardwareAddr) String() string {
3636
// 0000.5e00.5301
3737
// 0200.5e10.0000.0001
3838
// 0000.0000.fe80.0000.0000.0000.0200.5e10.0000.0001
39+
// 00005e005301
3940
func ParseMAC(s string) (hw HardwareAddr, err error) {
40-
if len(s) < 14 {
41+
if len(s) < 12 {
4142
goto error
4243
}
4344

@@ -76,6 +77,14 @@ func ParseMAC(s string) (hw HardwareAddr, err error) {
7677
}
7778
x += 5
7879
}
80+
} else if len(s)%2 == 0 {
81+
hw = make(HardwareAddr, len(s)/2)
82+
for i := range hw {
83+
var ok bool
84+
if hw[i], ok = xtoi2(s[i*2:i*2+2], 0); !ok {
85+
goto error
86+
}
87+
}
7988
} else {
8089
goto error
8190
}

src/net/mac_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ var parseMACTests = []struct {
1919
{"00:00:5e:00:53:01", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
2020
{"00-00-5e-00-53-01", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
2121
{"0000.5e00.5301", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
22+
{"00005e005301", HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}, ""},
2223

2324
// See RFC 7042, Section 2.2.2.
2425
{"02:00:5e:10:00:00:00:01", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
2526
{"02-00-5e-10-00-00-00-01", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
2627
{"0200.5e10.0000.0001", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
28+
{"02005e1000000001", HardwareAddr{0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01}, ""},
2729

2830
// See RFC 4391, Section 9.1.1.
2931
{
@@ -53,6 +55,15 @@ var parseMACTests = []struct {
5355
},
5456
"",
5557
},
58+
{
59+
"00000000fe8000000000000002005e1000000001",
60+
HardwareAddr{
61+
0x00, 0x00, 0x00, 0x00,
62+
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63+
0x02, 0x00, 0x5e, 0x10, 0x00, 0x00, 0x00, 0x01,
64+
},
65+
"",
66+
},
5667

5768
{"ab:cd:ef:AB:CD:EF", HardwareAddr{0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef}, ""},
5869
{"ab:cd:ef:AB:CD:EF:ab:cd", HardwareAddr{0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd}, ""},
@@ -78,6 +89,7 @@ var parseMACTests = []struct {
7889
{"01:02-03-04-05-06", nil, "invalid MAC address"},
7990
{"0123:4567:89AF", nil, "invalid MAC address"},
8091
{"0123-4567-89AF", nil, "invalid MAC address"},
92+
{"0123456789AF0", nil, "invalid MAC address"},
8193
}
8294

8395
func TestParseMAC(t *testing.T) {

0 commit comments

Comments
 (0)