Skip to content

Commit

Permalink
Fix handling of escapes within fieldset (#3003)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
danielnelson authored Jul 11, 2017
1 parent 0530985 commit 4780073
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 25 deletions.
20 changes: 1 addition & 19 deletions metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
Expand All @@ -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
Expand Down
14 changes: 8 additions & 6 deletions metric/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -367,7 +369,7 @@ func TestIndexUnescapedByte(t *testing.T) {
{
in: []byte(`foo\\bar`),
b: 'b',
expected: 5,
expected: -1,
},
{
in: []byte(`foobar`),
Expand Down

0 comments on commit 4780073

Please sign in to comment.