Skip to content

Commit

Permalink
net/netip: use slice-to-array conversions
Browse files Browse the repository at this point in the history
Resend of CL 432735 (with one additional conversion that the original CL
missed) after it broke the longtest builder on x/tools and was reverted
in CL 433478. Now that x/tools/go/ssa has support for this, the longtest
x/tools build passes as well.

Use slice-to-array conversions in AddrFromSlice and
(*Addr).UnmarshalBinary. This allows using AddrFrom16 and drop the
redundant ipv6Slice helper.

For #46505

Change-Id: I4d8084b7a97f162e4f7d685c86aac56d960ff693
Reviewed-on: https://go-review.googlesource.com/c/go/+/448396
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
  • Loading branch information
tklauser authored and gopherbot committed Nov 8, 2022
1 parent b417f62 commit dc98ccd
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions src/net/netip/netip.go
Expand Up @@ -109,18 +109,6 @@ func AddrFrom16(addr [16]byte) Addr {
}
}

// ipv6Slice is like IPv6Raw, but operates on a 16-byte slice. Assumes
// slice is 16 bytes, caller must enforce this.
func ipv6Slice(addr []byte) Addr {
return Addr{
addr: uint128{
beUint64(addr[:8]),
beUint64(addr[8:]),
},
z: z6noz,
}
}

// ParseAddr parses s as an IP address, returning the result. The string
// s can be in dotted decimal ("192.0.2.1"), IPv6 ("2001:db8::68"),
// or IPv6 with a scoped addressing zone ("fe80::1cc0:3e8c:119f:c2e1%ens18").
Expand Down Expand Up @@ -352,9 +340,9 @@ func parseIPv6(in string) (Addr, error) {
func AddrFromSlice(slice []byte) (ip Addr, ok bool) {
switch len(slice) {
case 4:
return AddrFrom4(*(*[4]byte)(slice)), true
return AddrFrom4([4]byte(slice)), true
case 16:
return ipv6Slice(slice), true
return AddrFrom16([16]byte(slice)), true
}
return Addr{}, false
}
Expand Down Expand Up @@ -1022,13 +1010,13 @@ func (ip *Addr) UnmarshalBinary(b []byte) error {
*ip = Addr{}
return nil
case n == 4:
*ip = AddrFrom4(*(*[4]byte)(b))
*ip = AddrFrom4([4]byte(b))
return nil
case n == 16:
*ip = ipv6Slice(b)
*ip = AddrFrom16([16]byte(b))
return nil
case n > 16:
*ip = ipv6Slice(b[:16]).WithZone(string(b[16:]))
*ip = AddrFrom16([16]byte(b[:16])).WithZone(string(b[16:]))
return nil
}
return errors.New("unexpected slice size")
Expand Down

0 comments on commit dc98ccd

Please sign in to comment.