This is an automated email from the ASF dual-hosted git repository.

hulk pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new 8c42aa08 fix(rdb): incorrect type conversion during RDB loads the int8 
encoding (#2547)
8c42aa08 is described below

commit 8c42aa0856e14e1c3c859e55e6832c0b3017c1cd
Author: Genji <[email protected]>
AuthorDate: Sat Sep 21 17:05:03 2024 +0800

    fix(rdb): incorrect type conversion during RDB loads the int8 encoding 
(#2547)
    
    Co-authored-by: hulk <[email protected]>
---
 src/storage/rdb.cc                  |  2 +-
 tests/gocase/unit/dump/dump_test.go | 26 ++++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/src/storage/rdb.cc b/src/storage/rdb.cc
index 544e679f..95a9a757 100644
--- a/src/storage/rdb.cc
+++ b/src/storage/rdb.cc
@@ -157,7 +157,7 @@ StatusOr<std::string> RDB::loadEncodedString() {
     unsigned char buf[4] = {0};
     if (len == RDBEncInt8) {
       auto next = GET_OR_RET(stream_->ReadByte());
-      return std::to_string(static_cast<int>(next));
+      return std::to_string(static_cast<int8_t>(next));
     } else if (len == RDBEncInt16) {
       GET_OR_RET(stream_->Read(reinterpret_cast<char *>(buf), 2));
       auto value = static_cast<uint16_t>(buf[0]) | 
(static_cast<uint16_t>(buf[1]) << 8);
diff --git a/tests/gocase/unit/dump/dump_test.go 
b/tests/gocase/unit/dump/dump_test.go
index 154891c3..ea2625f0 100644
--- a/tests/gocase/unit/dump/dump_test.go
+++ b/tests/gocase/unit/dump/dump_test.go
@@ -167,3 +167,29 @@ func TestDump_Bitset(t *testing.T) {
        require.NoError(t, rdb.RestoreReplace(ctx, restoredKey, 0, 
serialized).Err())
        require.Equal(t, rdb.Get(ctx, key).Val(), rdb.Get(ctx, 
restoredKey).Val())
 }
+
+func TestDump_IntegerEncoding(t *testing.T) {
+       srv := util.StartServer(t, map[string]string{})
+       defer srv.Close()
+
+       ctx := context.Background()
+       rdb := srv.NewClient()
+       defer func() { require.NoError(t, rdb.Close()) }()
+
+       values := []int64{
+               0, 1, -1, 127, -128, 128, -129, 129,
+               32767, -32768, 32768, -32769,
+               2147483647, -2147483648, 2147483648, -2147483649,
+       }
+       for i, v := range values {
+               key := fmt.Sprintf("key_%d", i)
+               require.NoError(t, rdb.Set(ctx, key, v, 0).Err())
+               serialized, err := rdb.Dump(ctx, key).Result()
+               require.NoError(t, err)
+               restoreKey := fmt.Sprintf("restore_%s", key)
+               require.NoError(t, rdb.Restore(ctx, restoreKey, 0, 
serialized).Err())
+               got, err := rdb.Get(ctx, restoreKey).Int64()
+               require.NoError(t, err)
+               require.Equal(t, v, got)
+       }
+}

Reply via email to