bug fix: null bulk reply bytes

This commit is contained in:
finley
2022-09-30 14:24:55 +08:00
parent f160dcfcc9
commit 894be52c31
3 changed files with 18 additions and 4 deletions

View File

@@ -25,6 +25,20 @@ func TestSet2(t *testing.T) {
} }
} }
func TestSetEmpty(t *testing.T) {
key := utils.RandString(10)
testDB.Exec(nil, utils.ToCmdLine("SET", key, ""))
actual := testDB.Exec(nil, utils.ToCmdLine("GET", key))
bulkReply, ok := actual.(*protocol.BulkReply)
if !ok {
t.Errorf("expected bulk protocol, actually %s", actual.ToBytes())
return
}
if !(bulkReply.Arg != nil && len(bulkReply.Arg) == 0) {
t.Error("illegal empty string")
}
}
func TestSet(t *testing.T) { func TestSet(t *testing.T) {
testDB.Flush() testDB.Flush()
key := utils.RandString(10) key := utils.RandString(10)

View File

@@ -240,7 +240,7 @@ func parseBulkHeader(msg []byte, state *readState) error {
} }
if state.bulkLen == -1 { // null bulk if state.bulkLen == -1 { // null bulk
return nil return nil
} else if state.bulkLen > 0 { } else if state.bulkLen >= 0 {
state.msgType = msg[0] state.msgType = msg[0]
state.readingMultiLine = true state.readingMultiLine = true
state.expectedArgsCount = 1 state.expectedArgsCount = 1
@@ -281,13 +281,13 @@ func parseSingleLineReply(msg []byte) (redis.Reply, error) {
func readBody(msg []byte, state *readState) error { func readBody(msg []byte, state *readState) error {
line := msg[0 : len(msg)-2] line := msg[0 : len(msg)-2]
var err error var err error
if line[0] == '$' { if len(line) > 0 && line[0] == '$' {
// bulk protocol // bulk protocol
state.bulkLen, err = strconv.ParseInt(string(line[1:]), 10, 64) state.bulkLen, err = strconv.ParseInt(string(line[1:]), 10, 64)
if err != nil { if err != nil {
return errors.New("protocol error: " + string(msg)) return errors.New("protocol error: " + string(msg))
} }
if state.bulkLen <= 0 { // null bulk in multi bulks if state.bulkLen < 0 { // null bulk in multi bulks
state.args = append(state.args, []byte{}) state.args = append(state.args, []byte{})
state.bulkLen = 0 state.bulkLen = 0
} }

View File

@@ -28,7 +28,7 @@ func MakeBulkReply(arg []byte) *BulkReply {
// ToBytes marshal redis.Reply // ToBytes marshal redis.Reply
func (r *BulkReply) ToBytes() []byte { func (r *BulkReply) ToBytes() []byte {
if len(r.Arg) == 0 { if r.Arg == nil {
return nullBulkBytes return nullBulkBytes
} }
return []byte("$" + strconv.Itoa(len(r.Arg)) + CRLF + string(r.Arg) + CRLF) return []byte("$" + strconv.Itoa(len(r.Arg)) + CRLF + string(r.Arg) + CRLF)