fix(models): reset provided slice correctly (#23223)

A preallocated slice needs to be cleared to be used with append,
otherwise the existing elements will be seen in the result and this does
not appear to be the intention. The bug doesn't seem to have caused
issues as no callsites use a preallocated slice.
pull/23226/head
Phil Bracikowski 2022-03-24 07:37:48 -07:00 committed by GitHub
parent e304ef9764
commit 050449803a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -2119,7 +2119,7 @@ func (a Tags) KeyValues(v [][]byte) [][]byte {
if cap(v) < l {
v = make([][]byte, 0, l)
} else {
v = v[:l]
v = v[:0]
}
for i := range a {
v = append(v, a[i].Key, a[i].Value)

View File

@ -13,6 +13,7 @@ import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/influxdata/influxdb/v2/models"
)
@ -2428,6 +2429,27 @@ func TestParseName(t *testing.T) {
}
}
func TestTags_KeyValues(t *testing.T) {
tags := models.NewTags(map[string]string{
"tag0": "v0",
"tag1": "v1",
"tag2": "v2",
})
got := tags.KeyValues(nil)
exp := [][]byte{[]byte("tag0"), []byte("v0"), []byte("tag1"), []byte("v1"), []byte("tag2"), []byte("v2")}
if !cmp.Equal(got, exp) {
t.Errorf("unexpected, -got/+exp\n%s", cmp.Diff(got, exp))
}
v := make([][]byte, 0, 10)
v = tags.KeyValues(v)
got2 := tags.KeyValues(v)
if !cmp.Equal(got2, exp) {
t.Errorf("unexpected, -got/+exp\n%s", cmp.Diff(got2, exp))
}
}
func BenchmarkEscapeStringField_Plain(b *testing.B) {
s := "nothing special"
for i := 0; i < b.N; i++ {