Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix redis parser panic #438

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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