From f13af15e7143195058143158170c952a8163722d Mon Sep 17 00:00:00 2001 From: ch33hau Date: Wed, 28 Oct 2015 22:12:53 +0800 Subject: [PATCH] skip empty string for start position --- models/points.go | 2 +- models/points_test.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/models/points.go b/models/points.go index 277748679da..cf99d53e325 100644 --- a/models/points.go +++ b/models/points.go @@ -726,7 +726,7 @@ func skipWhitespace(buf []byte, i int) int { return i } - if buf[i] == ' ' || buf[i] == '\t' { + if buf[i] == ' ' || buf[i] == '\t' || buf[i] == 0 { i += 1 continue } diff --git a/models/points_test.go b/models/points_test.go index c6b7f08d9ba..c76cb2f3240 100644 --- a/models/points_test.go +++ b/models/points_test.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "math" + "math/rand" "reflect" "strconv" "strings" @@ -1509,3 +1510,25 @@ func TestPrecisionString(t *testing.T) { } } } + +func TestParsePointsStringWithExtraBuffer(t *testing.T) { + b := make([]byte, 70*5000) + buf := bytes.NewBuffer(b) + key := "cpu,host=A,region=uswest" + buf.WriteString(fmt.Sprintf("%s value=%.3f 1\n", key, rand.Float64())) + + points, err := models.ParsePointsString(buf.String()) + if err != nil { + t.Fatalf("failed to write points: %s", err.Error()) + } + + pointKey := string(points[0].Key()) + + if len(key) != len(pointKey) { + t.Fatalf("expected length of both keys are same but got %d and %d", len(key), len(pointKey)) + } + + if key != pointKey { + t.Fatalf("expected both keys are same but got %s and %s", key, pointKey) + } +}