Skip to content

Commit

Permalink
fix: set error type in object.SetData()
Browse files Browse the repository at this point in the history
  • Loading branch information
xgzlucario committed Jul 29, 2024
1 parent b6102e2 commit 60911d8
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ test-cover:
rm coverage.txt
rm *.aof

fuzz-test:
go test -fuzz=FuzzRESPReader

pprof:
go tool pprof -http=:18081 "http://192.168.1.6:6060/debug/pprof/profile?seconds=30"

Expand Down
14 changes: 14 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,20 @@ func TestCommand(t *testing.T) {
assert.NotNil(err)
})

t.Run("trans-zipmap", func(t *testing.T) {
for i := 0; i <= 256; i++ {
k := fmt.Sprintf("%06x", i)
rdb.HSet(ctx, "zipmap", k, k)
}
})

t.Run("trans-zipset", func(t *testing.T) {
for i := 0; i <= 512; i++ {
k := fmt.Sprintf("%06x", i)
rdb.SAdd(ctx, "zipset", k)
}
})

t.Run("client-closed", func(t *testing.T) {
rdb.Close()
})
Expand Down
2 changes: 1 addition & 1 deletion internal/dict/dict_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestDict(t *testing.T) {
dict := New()

dict.SetWithTTL("key", []byte("hello"), time.Now().Add(time.Minute).UnixNano())
time.Sleep(time.Millisecond)
time.Sleep(time.Second / 10)

object, ttl := dict.Get("key")
assert.Equal(ttl, 59)
Expand Down
2 changes: 1 addition & 1 deletion internal/dict/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func typeOfData(data any) Type {
case *hash.Set:
return TypeSet
case *hash.ZipSet:
return TypeSet
return TypeZipSet
case *list.QuickList:
return TypeList
case *zset.ZSet:
Expand Down
12 changes: 6 additions & 6 deletions resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
INTEGER = ':'
BULK = '$'
ARRAY = '*'
MAP = '%' // TODO: https://redis.io/docs/latest/develop/reference/protocol-spec/#maps
)

var CRLF = []byte("\r\n")
Expand Down Expand Up @@ -46,7 +47,7 @@ func cutByCRLF(buf []byte) (before, after []byte, found bool) {
}

// ReadNextCommand reads the next RESP command from the RESPReader.
// It parses both COMMAND_BULK and COMMAND_INLINE formats.
// It parses both `COMMAND_BULK` and `COMMAND_INLINE` formats.
func (r *RESPReader) ReadNextCommand(argsBuf []RESP) (args []RESP, err error) {
if len(r.b) == 0 {
return nil, io.EOF
Expand All @@ -66,11 +67,10 @@ func (r *RESPReader) ReadNextCommand(argsBuf []RESP) (args []RESP, err error) {
}
r.b = after

// read bulk strings for range
for i := 0; i < count; i++ {
switch r.b[0] {
case BULK:
default:
return nil, fmt.Errorf("unsupport array-in type: '%c'", r.b[0])
if len(r.b) == 0 || r.b[0] != BULK {
return nil, errInvalidArguments
}

// read CRLF
Expand All @@ -85,7 +85,7 @@ func (r *RESPReader) ReadNextCommand(argsBuf []RESP) (args []RESP, err error) {
r.b = after

// bound check
if count > len(r.b) {
if count < 0 || count+2 > len(r.b) {
return nil, errInvalidArguments
}

Expand Down
10 changes: 10 additions & 0 deletions resp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func TestReader(t *testing.T) {
args, err = NewReader([]byte("*3\r\n$3ABC")).ReadNextCommand(nil)
assert.Equal(len(args), 0)
assert.NotNil(err)

args, err = NewReader([]byte("*1\r\n")).ReadNextCommand(nil)
assert.Equal(len(args), 0)
assert.NotNil(err)
})

t.Run("command-inline", func(t *testing.T) {
Expand All @@ -108,3 +112,9 @@ func TestReader(t *testing.T) {
assert.Nil(err)
})
}

func FuzzRESPReader(f *testing.F) {
f.Fuzz(func(t *testing.T, b []byte) {
NewReader(b).ReadNextCommand(nil)
})
}

0 comments on commit 60911d8

Please sign in to comment.