Skip to content

Commit

Permalink
Fix an issue Redis protocol not handling nil response (#22200) (#22230)
Browse files Browse the repository at this point in the history
  • Loading branch information
greedy52 committed Feb 24, 2023
1 parent 6dcfe0d commit fc64425
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/srv/db/redis/protocol/resp2.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ var ErrCmdNotSupported = trace.NotImplemented("command not supported")
// * slices: arrays are recursively converted to RESP responses.
func WriteCmd(wr *redis.Writer, vals interface{}) error {
switch val := vals.(type) {
case nil:
// Note: RESP3 has different sequence for nil, current nil is RESP2 compatible as the rest
// of our implementation.
if _, err := wr.WriteString("$-1\r\n"); err != nil {
return trace.Wrap(err)
}
case redis.Error:
if val == redis.Nil {
// go-redis returns nil value as errors, but Redis Wire protocol decodes them differently.
// Note: RESP3 has different sequence for nil, current nil is RESP2 compatible as the rest
// of our implementation.
if _, err := wr.WriteString("$-1\r\n"); err != nil {
return trace.Wrap(err)
}
return nil
return trace.Wrap(WriteCmd(wr, nil))
}

if err := writeError(wr, "-", val); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions lib/srv/db/redis/protocol/resp2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func TestWriteCmd(t *testing.T) {
val: []string{"test val1", "test val 2"},
expected: []byte("*2\r\n$9\r\ntest val1\r\n$10\r\ntest val 2\r\n"),
},
{
name: "[]nil",
val: []interface{}{nil},
expected: []byte("*1\r\n$-1\r\n"),
},
{
name: "[]bool",
val: []bool{true, false},
Expand Down

0 comments on commit fc64425

Please sign in to comment.