]> Cypherpunks repositories - gostls13.git/commitdiff
net/netip: fix formatting of IPv4-in-6 address with zone
authorBrad Fitzpatrick <bradfitz@golang.org>
Sun, 12 Dec 2021 00:17:46 +0000 (16:17 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Sun, 12 Dec 2021 02:02:17 +0000 (02:02 +0000)
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 <bradfitz@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
Trust: Matt Layher <mdlayher@gmail.com>
Trust: Ian Lance Taylor <iant@golang.org>

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

index 01f6fe5efa086be061414cf256f2ceb736ce2733..dc5faff40fbe518efd7c6b980bc1599eb05fefaf 100644 (file)
@@ -769,7 +769,11 @@ func (ip Addr) String() string {
        default:
                if ip.Is4In6() {
                        // TODO(bradfitz): this could alloc less.
-                       return "::ffff:" + ip.Unmap().String()
+                       if z := ip.Zone(); z != "" {
+                               return "::ffff:" + ip.Unmap().String() + "%" + z
+                       } else {
+                               return "::ffff:" + ip.Unmap().String()
+                       }
                }
                return ip.string6()
        }
@@ -787,7 +791,12 @@ func (ip Addr) AppendTo(b []byte) []byte {
        default:
                if ip.Is4In6() {
                        b = append(b, "::ffff:"...)
-                       return ip.Unmap().appendTo4(b)
+                       b = ip.Unmap().appendTo4(b)
+                       if z := ip.Zone(); z != "" {
+                               b = append(b, '%')
+                               b = append(b, z...)
+                       }
+                       return b
                }
                return ip.appendTo6(b)
        }
@@ -947,10 +956,16 @@ func (ip Addr) MarshalText() ([]byte, error) {
                b := make([]byte, 0, max)
                if ip.Is4In6() {
                        b = append(b, "::ffff:"...)
-                       return ip.Unmap().appendTo4(b), nil
+                       b = ip.Unmap().appendTo4(b)
+                       if z := ip.Zone(); z != "" {
+                               b = append(b, '%')
+                               b = append(b, z...)
+                       }
+                       return b, nil
                }
                return ip.appendTo6(b), nil
        }
+
 }
 
 // UnmarshalText implements the encoding.TextUnmarshaler interface.
index a6327f0dea826fd7d6035ddfb94da7139959d124..21055451392e3e7b7874b88cee9c9c4f3e7daee8 100644 (file)
@@ -114,6 +114,12 @@ func TestParseAddr(t *testing.T) {
                        ip:  MkAddr(Mk128(0x0001000200000000, 0x0000ffffc0a88cff), intern.Get("eth1")),
                        str: "1:2::ffff:c0a8:8cff%eth1",
                },
+               // 4-in-6 with zone
+               {
+                       in:  "::ffff:192.168.140.255%eth1",
+                       ip:  MkAddr(Mk128(0, 0x0000ffffc0a88cff), intern.Get("eth1")),
+                       str: "::ffff:192.168.140.255%eth1",
+               },
                // IPv6 with capital letters.
                {
                        in:  "FD9E:1A04:F01D::1",