From 945446b36f228ce9f4c416c93487132a2568acbc Mon Sep 17 00:00:00 2001
From: Daniel Nelson <daniel@wavesofdawn.com>
Date: Mon, 10 Jul 2017 19:05:18 -0700
Subject: [PATCH] Fix handling of escapes within fieldset (#3003)

Line protocol does not require or allow escaping of backslash, the only
requirement for a byte to be escaped is if it is an escapable char and
preceeded immediately by a slash.
---
 metric/metric.go      | 20 +-------------------
 metric/metric_test.go | 14 ++++++++------
 2 files changed, 9 insertions(+), 25 deletions(-)

diff --git a/metric/metric.go b/metric/metric.go
index 0dbc1fa8..fdef072c 100644
--- a/metric/metric.go
+++ b/metric/metric.go
@@ -98,7 +98,7 @@ func indexUnescapedByte(buf []byte, b byte) int {
 			break
 		}
 		keyi += i
-		if countBackslashes(buf, keyi-1)%2 == 0 {
+		if buf[keyi-1] != '\\' {
 			break
 		} else {
 			keyi++
@@ -107,24 +107,6 @@ func indexUnescapedByte(buf []byte, b byte) int {
 	return keyi
 }
 
-// countBackslashes counts the number of preceding backslashes starting at
-// the 'start' index.
-func countBackslashes(buf []byte, index int) int {
-	var count int
-	for {
-		if index < 0 {
-			return count
-		}
-		if buf[index] == '\\' {
-			count++
-			index--
-		} else {
-			break
-		}
-	}
-	return count
-}
-
 type metric struct {
 	name   []byte
 	tags   []byte
diff --git a/metric/metric_test.go b/metric/metric_test.go
index eccb60e6..c154549f 100644
--- a/metric/metric_test.go
+++ b/metric/metric_test.go
@@ -250,11 +250,13 @@ func TestNewMetric_Fields(t *testing.T) {
 		"host": "localhost",
 	}
 	fields := map[string]interface{}{
-		"float":  float64(1),
-		"int":    int64(1),
-		"bool":   true,
-		"false":  false,
-		"string": "test",
+		"float":                  float64(1),
+		"int":                    int64(1),
+		"bool":                   true,
+		"false":                  false,
+		"string":                 "test",
+		"quote_string":           `x"y`,
+		"backslash_quote_string": `x\"y`,
 	}
 	m, err := New("cpu", tags, fields, now)
 	assert.NoError(t, err)
@@ -367,7 +369,7 @@ func TestIndexUnescapedByte(t *testing.T) {
 		{
 			in:       []byte(`foo\\bar`),
 			b:        'b',
-			expected: 5,
+			expected: -1,
 		},
 		{
 			in:       []byte(`foobar`),
-- 
GitLab