From 5381437664f588c67a424765dfeb3c28fa8092df Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 3 Aug 2022 21:29:11 -0700 Subject: [PATCH] logtail, net/portmapper, wgengine/magicsock: use fmt.Appendf Fixes #5206 Change-Id: I490bb92e774ce7c044040537e2cd864fcf1dbe5a Signed-off-by: Brad Fitzpatrick --- logtail/buffer.go | 5 +---- logtail/logtail.go | 2 +- net/portmapper/igd_test.go | 2 +- wgengine/magicsock/magicsock.go | 2 +- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/logtail/buffer.go b/logtail/buffer.go index 196382c49..f79b06b89 100644 --- a/logtail/buffer.go +++ b/logtail/buffer.go @@ -5,7 +5,6 @@ package logtail import ( - "bytes" "errors" "fmt" "sync" @@ -51,9 +50,7 @@ func (m *memBuffer) TryReadLine() ([]byte, error) { case ent := <-m.pending: if ent.dropCount > 0 { m.next = ent.msg - buf := new(bytes.Buffer) - fmt.Fprintf(buf, "----------- %d logs dropped ----------", ent.dropCount) - return buf.Bytes(), nil + return fmt.Appendf(nil, "----------- %d logs dropped ----------", ent.dropCount), nil } return ent.msg, nil default: diff --git a/logtail/logtail.go b/logtail/logtail.go index 6da67848f..368e13ad4 100644 --- a/logtail/logtail.go +++ b/logtail/logtail.go @@ -270,7 +270,7 @@ func (l *Logger) drainPending(scratch []byte) (res []byte) { if err == io.EOF { break } else if err != nil { - b = []byte(fmt.Sprintf("reading ringbuffer: %v", err)) + b = fmt.Appendf(nil, "reading ringbuffer: %v", err) batchDone = true } else if b == nil { if entries > 0 { diff --git a/net/portmapper/igd_test.go b/net/portmapper/igd_test.go index 4b56ce196..d44c9edc8 100644 --- a/net/portmapper/igd_test.go +++ b/net/portmapper/igd_test.go @@ -143,7 +143,7 @@ func (d *TestIGD) serveUPnPDiscovery() { pkt := buf[:n] if bytes.Equal(pkt, uPnPPacket) { // a super lazy "parse" d.inc(&d.counters.numUPnPDiscoRecv) - resPkt := []byte(fmt.Sprintf("HTTP/1.1 200 OK\r\nCACHE-CONTROL: max-age=120\r\nST: urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\nUSN: uuid:bee7052b-49e8-3597-b545-55a1e38ac11::urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\nEXT:\r\nSERVER: Tailscale-Test/1.0 UPnP/1.1 MiniUPnPd/2.2.1\r\nLOCATION: %s\r\nOPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n01-NLS: 1627958564\r\nBOOTID.UPNP.ORG: 1627958564\r\nCONFIGID.UPNP.ORG: 1337\r\n\r\n", d.ts.URL+"/rootDesc.xml")) + resPkt := fmt.Appendf(nil, "HTTP/1.1 200 OK\r\nCACHE-CONTROL: max-age=120\r\nST: urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\nUSN: uuid:bee7052b-49e8-3597-b545-55a1e38ac11::urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\nEXT:\r\nSERVER: Tailscale-Test/1.0 UPnP/1.1 MiniUPnPd/2.2.1\r\nLOCATION: %s\r\nOPT: \"http://schemas.upnp.org/upnp/1/0/\"; ns=01\r\n01-NLS: 1627958564\r\nBOOTID.UPNP.ORG: 1627958564\r\nCONFIGID.UPNP.ORG: 1337\r\n\r\n", d.ts.URL+"/rootDesc.xml") if d.doUPnP { _, err = d.upnpConn.WriteTo(resPkt, src) if err != nil { diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index f1330f3ca..97c1455c0 100644 --- a/wgengine/magicsock/magicsock.go +++ b/wgengine/magicsock/magicsock.go @@ -811,7 +811,7 @@ func (c *Conn) pickDERPFallback() int { } h := fnv.New64() - h.Write([]byte(fmt.Sprintf("%p/%d", c, processStartUnixNano))) // arbitrary + fmt.Fprintf(h, "%p/%d", c, processStartUnixNano) // arbitrary return ids[rand.New(rand.NewSource(int64(h.Sum64()))).Intn(len(ids))] }