Skip to content

Commit

Permalink
Merge pull request elastic#438 from urso/fix/regression-redis-command…
Browse files Browse the repository at this point in the history
…-no-args

fix redis parser panic
  • Loading branch information
andrewkroh committed Dec 4, 2015
2 parents 123b4b6 + d15d1aa commit 55b5202
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
22 changes: 14 additions & 8 deletions packetbeat/protos/redis/redis_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,12 @@ func (p *parser) parseArray(depth int, buf *streambuf.Buffer) (string, bool, boo
}
if count < 0 {
return "nil", false, true, true
} else if count == 0 {
// should not happen, but handle just in case ParseInt did return 0
return "[]", false, true, true
}

// invariant: count > 0
content := make([]string, 0, count)
// read sub elements

Expand All @@ -353,17 +357,19 @@ func (p *parser) parseArray(depth int, buf *streambuf.Buffer) (string, bool, boo
content = append(content, value)
}

if depth == 0 && isRedisCommand(content[0]) { // we've got a request
// handle top-level request command
if depth == 0 && isRedisCommand(content[0]) {
p.message.IsRequest = true
p.message.Method = content[0]
p.message.Path = content[1]
}
if len(content) > 1 {
p.message.Path = content[1]
}

var value string
if depth == 0 && p.message.IsRequest {
value = strings.Join(content, " ")
} else {
value = "[" + strings.Join(content, ", ") + "]"
value := strings.Join(content, " ")
return value, iserror, true, true
}

// return redis array
value := "[" + strings.Join(content, ", ") + "]"
return value, iserror, true, true
}
13 changes: 13 additions & 0 deletions packetbeat/protos/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ func parse(content []byte) (*redisMessage, bool, bool) {
return st.parser.message, ok, complete
}

func TestRedisParser_NoArgsRequest(t *testing.T) {
message := []byte("*1\r\n" +
"$4\r\n" +
"INFO\r\n")
msg, ok, complete := parse(message)

assert.True(t, ok)
assert.True(t, complete)
assert.True(t, msg.IsRequest)
assert.Equal(t, "INFO", msg.Message)
assert.Equal(t, len(message), msg.Size)
}

func TestRedisParser_ArrayRequest(t *testing.T) {
message := []byte("*3\r\n" +
"$3\r\n" +
Expand Down

0 comments on commit 55b5202

Please sign in to comment.