Skip to content

Commit 1c1998e

Browse files
committed
net/netip: fix formatting of IPv4-in-6 address with zone
Weird, but don't drop the zone when stringifying. Fixes #50111 Change-Id: I5fbccdfedcdc77a77ee6bafc8d82b8ec8ec7220c Reviewed-on: https://go-review.googlesource.com/c/go/+/371094 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Matt Layher <[email protected]> Trust: Matt Layher <[email protected]> Trust: Ian Lance Taylor <[email protected]>
1 parent 766f89b commit 1c1998e

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/net/netip/netip.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,11 @@ func (ip Addr) String() string {
769769
default:
770770
if ip.Is4In6() {
771771
// TODO(bradfitz): this could alloc less.
772-
return "::ffff:" + ip.Unmap().String()
772+
if z := ip.Zone(); z != "" {
773+
return "::ffff:" + ip.Unmap().String() + "%" + z
774+
} else {
775+
return "::ffff:" + ip.Unmap().String()
776+
}
773777
}
774778
return ip.string6()
775779
}
@@ -787,7 +791,12 @@ func (ip Addr) AppendTo(b []byte) []byte {
787791
default:
788792
if ip.Is4In6() {
789793
b = append(b, "::ffff:"...)
790-
return ip.Unmap().appendTo4(b)
794+
b = ip.Unmap().appendTo4(b)
795+
if z := ip.Zone(); z != "" {
796+
b = append(b, '%')
797+
b = append(b, z...)
798+
}
799+
return b
791800
}
792801
return ip.appendTo6(b)
793802
}
@@ -947,10 +956,16 @@ func (ip Addr) MarshalText() ([]byte, error) {
947956
b := make([]byte, 0, max)
948957
if ip.Is4In6() {
949958
b = append(b, "::ffff:"...)
950-
return ip.Unmap().appendTo4(b), nil
959+
b = ip.Unmap().appendTo4(b)
960+
if z := ip.Zone(); z != "" {
961+
b = append(b, '%')
962+
b = append(b, z...)
963+
}
964+
return b, nil
951965
}
952966
return ip.appendTo6(b), nil
953967
}
968+
954969
}
955970

956971
// UnmarshalText implements the encoding.TextUnmarshaler interface.

src/net/netip/netip_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ func TestParseAddr(t *testing.T) {
114114
ip: MkAddr(Mk128(0x0001000200000000, 0x0000ffffc0a88cff), intern.Get("eth1")),
115115
str: "1:2::ffff:c0a8:8cff%eth1",
116116
},
117+
// 4-in-6 with zone
118+
{
119+
in: "::ffff:192.168.140.255%eth1",
120+
ip: MkAddr(Mk128(0, 0x0000ffffc0a88cff), intern.Get("eth1")),
121+
str: "::ffff:192.168.140.255%eth1",
122+
},
117123
// IPv6 with capital letters.
118124
{
119125
in: "FD9E:1A04:F01D::1",

0 commit comments

Comments
 (0)