Skip to content

Commit

Permalink
fix(rdb): incorrect type conversion during RDB loads the int8 encoding (
Browse files Browse the repository at this point in the history
#2547)

Co-authored-by: hulk <[email protected]>
  • Loading branch information
fstd2 and git-hulk authored Sep 21, 2024
1 parent f402be0 commit 8c42aa0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/storage/rdb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions tests/gocase/unit/dump/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 8c42aa0

Please sign in to comment.