all: use Go 1.20's bytes.Clone

Updates #7123
Updates #6257 (more to do in other repos)

Change-Id: I073e2a6d81a5d7fbecc29caddb7e057ff65239d0
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/7149/head
Brad Fitzpatrick 2 years ago committed by Brad Fitzpatrick
parent f7b3156f16
commit cf8dd7aa09

@ -126,7 +126,7 @@ func TestSendRecv(t *testing.T) {
if m.Source.IsZero() { if m.Source.IsZero() {
t.Errorf("zero Source address in ReceivedPacket") t.Errorf("zero Source address in ReceivedPacket")
} }
recvChs[i] <- append([]byte(nil), m.Data...) recvChs[i] <- bytes.Clone(m.Data)
} }
} }
}(i) }(i)

@ -4,6 +4,7 @@
package derphttp package derphttp
import ( import (
"bytes"
"context" "context"
"crypto/tls" "crypto/tls"
"net" "net"
@ -102,7 +103,7 @@ func TestSendRecv(t *testing.T) {
case derp.PeerGoneMessage: case derp.PeerGoneMessage:
// Ignore. // Ignore.
case derp.ReceivedPacket: case derp.ReceivedPacket:
recvChs[i] <- append([]byte(nil), m.Data...) recvChs[i] <- bytes.Clone(m.Data)
} }
} }
}(i) }(i)

@ -5,6 +5,7 @@
package mem package mem
import ( import (
"bytes"
"encoding/json" "encoding/json"
"sync" "sync"
@ -43,7 +44,7 @@ func (s *Store) WriteState(id ipn.StateKey, bs []byte) error {
if s.cache == nil { if s.cache == nil {
s.cache = map[ipn.StateKey][]byte{} s.cache = map[ipn.StateKey][]byte{}
} }
s.cache[id] = append([]byte(nil), bs...) s.cache[id] = bytes.Clone(bs)
return nil return nil
} }

@ -179,7 +179,7 @@ func (s *FileStore) WriteState(id ipn.StateKey, bs []byte) error {
if bytes.Equal(s.cache[id], bs) { if bytes.Equal(s.cache[id], bs) {
return nil return nil
} }
s.cache[id] = append([]byte(nil), bs...) s.cache[id] = bytes.Clone(bs)
bs, err := json.MarshalIndent(s.cache, "", " ") bs, err := json.MarshalIndent(s.cache, "", " ")
if err != nil { if err != nil {
return err return err

@ -5,6 +5,7 @@
package stun package stun
import ( import (
"bytes"
crand "crypto/rand" crand "crypto/rand"
"encoding/binary" "encoding/binary"
"errors" "errors"
@ -300,7 +301,7 @@ func mappedAddress(b []byte) (addr []byte, port uint16, err error) {
if len(addrField) < addrLen { if len(addrField) < addrLen {
return nil, 0, ErrMalformedAttrs return nil, 0, ErrMalformedAttrs
} }
return append([]byte(nil), addrField[:addrLen]...), port, nil return bytes.Clone(addrField[:addrLen]), port, nil
} }
// Is reports whether b is a STUN message. // Is reports whether b is a STUN message.

@ -49,7 +49,7 @@ func (p *Packet) Clone() *Packet {
return &Packet{ return &Packet{
Src: p.Src, Src: p.Src,
Dst: p.Dst, Dst: p.Dst,
Payload: append([]byte(nil), p.Payload...), Payload: bytes.Clone(p.Payload),
locator: p.locator, locator: p.locator,
} }
} }
@ -863,7 +863,7 @@ func (c *conn) WriteToUDPAddrPort(p []byte, ipp netip.AddrPort) (n int, err erro
pkt := &Packet{ pkt := &Packet{
Src: c.ipp, Src: c.ipp,
Dst: ipp, Dst: ipp,
Payload: append([]byte(nil), p...), Payload: bytes.Clone(p),
} }
pkt.setLocator("mach=%s", c.m.Name) pkt.setLocator("mach=%s", c.m.Name)
pkt.Trace("PacketConn.WriteTo") pkt.Trace("PacketConn.WriteTo")

@ -4,6 +4,7 @@
package key package key
import ( import (
"bytes"
"crypto/subtle" "crypto/subtle"
"encoding/hex" "encoding/hex"
@ -86,7 +87,7 @@ func (k *MachinePrivate) UnmarshalText(b []byte) error {
// specific raw byte serialization, please use // specific raw byte serialization, please use
// MarshalText/UnmarshalText. // MarshalText/UnmarshalText.
func (k MachinePrivate) UntypedBytes() []byte { func (k MachinePrivate) UntypedBytes() []byte {
return append([]byte(nil), k.k[:]...) return bytes.Clone(k.k[:])
} }
// SealTo wraps cleartext into a NaCl box (see // SealTo wraps cleartext into a NaCl box (see
@ -230,7 +231,7 @@ func (k MachinePublic) UntypedHexString() string {
// specific raw byte serialization, please use // specific raw byte serialization, please use
// MarshalText/UnmarshalText. // MarshalText/UnmarshalText.
func (k MachinePublic) UntypedBytes() []byte { func (k MachinePublic) UntypedBytes() []byte {
return append([]byte(nil), k.k[:]...) return bytes.Clone(k.k[:])
} }
// String returns the output of MarshalText as a string. // String returns the output of MarshalText as a string.

@ -5,6 +5,7 @@
package netstack package netstack
import ( import (
"bytes"
"context" "context"
"errors" "errors"
"fmt" "fmt"
@ -401,7 +402,7 @@ func (ns *Impl) handleLocalPackets(p *packet.Parsed, t *tstun.Wrapper) filter.Re
} }
packetBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{ packetBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{
Payload: bufferv2.MakeWithData(append([]byte(nil), p.Buffer()...)), Payload: bufferv2.MakeWithData(bytes.Clone(p.Buffer())),
}) })
ns.linkEP.InjectInbound(pn, packetBuf) ns.linkEP.InjectInbound(pn, packetBuf)
packetBuf.DecRef() packetBuf.DecRef()
@ -683,7 +684,7 @@ func (ns *Impl) injectInbound(p *packet.Parsed, t *tstun.Wrapper) filter.Respons
ns.logf("[v2] packet in (from %v): % x", p.Src, p.Buffer()) ns.logf("[v2] packet in (from %v): % x", p.Src, p.Buffer())
} }
packetBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{ packetBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{
Payload: bufferv2.MakeWithData(append([]byte(nil), p.Buffer()...)), Payload: bufferv2.MakeWithData(bytes.Clone(p.Buffer())),
}) })
ns.linkEP.InjectInbound(pn, packetBuf) ns.linkEP.InjectInbound(pn, packetBuf)
packetBuf.DecRef() packetBuf.DecRef()

Loading…
Cancel
Save