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

encoding of numbers has a serious bug #38

Closed
michael2008 opened this issue Aug 26, 2024 · 3 comments
Closed

encoding of numbers has a serious bug #38

michael2008 opened this issue Aug 26, 2024 · 3 comments

Comments

@michael2008
Copy link

michael2008 commented Aug 26, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
@michael2008 @HDT3213 and others