Skip to content

encoding of numbers has a serious bug #38

@michael2008

Description

@michael2008

Example Code:

func TestRdbEncDec(t *testing.T) {
	// encoding
	buf := bytes.NewBuffer(make([]byte, 0, 1024*1024*1024))
	enc := encoder.NewEncoder(buf)
	err := enc.WriteHeader()
	require.Nil(t, err)
	auxMap := map[string]string{
		"redis-ver":    "5.0.9",
		"redis-bits":   "64",
		"aof-preamble": "0",
	}
	for k, v := range auxMap {
		err = enc.WriteAux(k, v)
		require.Nil(t, err)
	}

	err = enc.WriteDBHeader(0, 1, 1)
	require.Nil(t, err)
	expirationMs := uint64(time.Now().Add(time.Hour*8).Unix() * 1000)
	err = enc.WriteHashMapObject("hash", map[string][]byte{
		"001": []byte("007"),
	}, encoder.WithTTL(expirationMs))
	require.Nil(t, err)
	err = enc.WriteEnd()
	require.Nil(t, err)

	// decoding
	decoder := parser.NewDecoder(buf)
	err = decoder.Parse(func(o parser.RedisObject) bool {
		switch o.GetType() {
		case parser.StringType:
			str := o.(*parser.StringObject)
			println(str.Key, str.Value)
		case parser.ListType:
			list := o.(*parser.ListObject)
			println(list.Key, list.Values)
		case parser.HashType:
			hash := o.(*parser.HashObject)
			mp := make(map[string]string, len(hash.Hash))
			for k, v := range hash.Hash {
				mp[k] = string(v)
			}
			fmt.Printf("HASH: key=%s, hash=%+v\n", hash.Key, mp)
		case parser.ZSetType:
			zset := o.(*parser.ZSetObject)
			println(zset.Key, zset.Entries)
		case parser.SetType:
			set := o.(*parser.SetObject)
			println(set.Key, set.Members)
		}
		// return true to continue, return false to stop the iteration
		return true
	})
	require.Nil(t, err)
}

it prints

=== RUN   TestRdbEncDec
HASH: key=hash, hash=map[1:7]

the problem is encoding of number like strings is wrong, for instance, the encoder will treat "001" as "1".

I think the problem is originated from this line of code:

intVal, err := strconv.ParseInt(s, 10, 64)

where strconv.ParseInt() is miss-used.

Edit: I came up with a PR #39

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions