wgengine/tsdns: truncate Map.PrettyDiffFrom string at 1KB

Hello's were painful.

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/1442/head
Brad Fitzpatrick 3 years ago
parent cc99059fc2
commit 0430c2dd12

@ -92,6 +92,9 @@ func (m *Map) PrettyDiffFrom(old *Map) string {
}
buf := new(strings.Builder)
space := func() bool {
return buf.Len() < (1 << 10)
}
for len(oldNames) > 0 && len(newNames) > 0 {
var name string
@ -109,6 +112,9 @@ func (m *Map) PrettyDiffFrom(old *Map) string {
oldNames = oldNames[1:]
newNames = newNames[1:]
}
if !space() {
continue
}
ipOld, inOld := oldNameToIP[name]
ipNew, inNew := newNameToIP[name]
@ -128,6 +134,9 @@ func (m *Map) PrettyDiffFrom(old *Map) string {
}
for _, name := range oldNames {
if !space() {
break
}
if _, ok := newNameToIP[name]; !ok {
buf.WriteByte('-')
printSingleNameIP(buf, name, oldNameToIP[name])
@ -135,11 +144,17 @@ func (m *Map) PrettyDiffFrom(old *Map) string {
}
for _, name := range newNames {
if !space() {
break
}
if _, ok := oldNameToIP[name]; !ok {
buf.WriteByte('+')
printSingleNameIP(buf, name, newNameToIP[name])
}
}
if !space() {
buf.WriteString("... [truncated]\n")
}
return buf.String()
}

@ -5,6 +5,8 @@
package tsdns
import (
"fmt"
"strings"
"testing"
"inet.af/netaddr"
@ -135,4 +137,20 @@ func TestPrettyDiffFrom(t *testing.T) {
}
})
}
t.Run("truncated", func(t *testing.T) {
small := NewMap(nil, nil)
m := map[string]netaddr.IP{}
for i := 0; i < 5000; i++ {
m[fmt.Sprintf("host%d.ipn.dev.", i)] = netaddr.IPv4(100, 64, 1, 1)
}
veryBig := NewMap(m, nil)
diff := veryBig.PrettyDiffFrom(small)
if len(diff) > 3<<10 {
t.Errorf("pretty diff too large: %d bytes", len(diff))
}
if !strings.Contains(diff, "truncated") {
t.Errorf("big diff not truncated")
}
})
}

Loading…
Cancel
Save