]> Cypherpunks repositories - gostls13.git/commitdiff
net/netip: make Prefix.MarshalText format 4-in-6 IPs consistently
authorMatt Layher <mdlayher@gmail.com>
Sun, 12 Dec 2021 15:53:17 +0000 (10:53 -0500)
committerMatt Layher <mdlayher@gmail.com>
Sun, 12 Dec 2021 16:09:13 +0000 (16:09 +0000)
Fixes #50115.

Change-Id: Iac76e5b486d3a2a784583345eaeb22c31cc4a36d
Reviewed-on: https://go-review.googlesource.com/c/go/+/371134
Trust: Matt Layher <mdlayher@gmail.com>
Run-TryBot: Matt Layher <mdlayher@gmail.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Trust: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/net/netip/netip.go
src/net/netip/netip_test.go

index aaaf435ed8ec21e6a52199f3e9202f0317f84d9d..591d38abc894b5f15d633acb812c2d9e14be1e1c 100644 (file)
@@ -1422,7 +1422,12 @@ func (p Prefix) AppendTo(b []byte) []byte {
        if p.ip.z == z4 {
                b = p.ip.appendTo4(b)
        } else {
-               b = p.ip.appendTo6(b)
+               if p.ip.Is4In6() {
+                       b = append(b, "::ffff:"...)
+                       b = p.ip.Unmap().appendTo4(b)
+               } else {
+                       b = p.ip.appendTo6(b)
+               }
        }
 
        b = append(b, '/')
index 869628050a8acd29992eec9f9a6408147707e8b2..520695cdfb8d25dfad0658b86b409724492e42a5 100644 (file)
@@ -413,6 +413,32 @@ func TestAddrPortMarshalUnmarshalBinary(t *testing.T) {
        }
 }
 
+func TestPrefixMarshalTextString(t *testing.T) {
+       tests := []struct {
+               in   Prefix
+               want string
+       }{
+               {mustPrefix("1.2.3.4/24"), "1.2.3.4/24"},
+               {mustPrefix("fd7a:115c:a1e0:ab12:4843:cd96:626b:430b/118"), "fd7a:115c:a1e0:ab12:4843:cd96:626b:430b/118"},
+               {mustPrefix("::ffff:c000:0280/96"), "::ffff:192.0.2.128/96"},
+               {mustPrefix("::ffff:c000:0280%eth0/37"), "::ffff:192.0.2.128/37"}, // Zone should be stripped
+               {mustPrefix("::ffff:192.168.140.255/8"), "::ffff:192.168.140.255/8"},
+       }
+       for i, tt := range tests {
+               if got := tt.in.String(); got != tt.want {
+                       t.Errorf("%d. for %v String = %q; want %q", i, tt.in, got, tt.want)
+               }
+               mt, err := tt.in.MarshalText()
+               if err != nil {
+                       t.Errorf("%d. for %v MarshalText error: %v", i, tt.in, err)
+                       continue
+               }
+               if string(mt) != tt.want {
+                       t.Errorf("%d. for %v MarshalText = %q; want %q", i, tt.in, mt, tt.want)
+               }
+       }
+}
+
 func TestPrefixMarshalUnmarshalBinary(t *testing.T) {
        type testCase struct {
                prefix   Prefix
@@ -994,7 +1020,6 @@ func TestPrefixMarshalUnmarshal(t *testing.T) {
                "0.0.0.0/0",
                "::/0",
                "::1/128",
-               "::ffff:c000:1234/128",
                "2001:db8::/32",
        }