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

fix(rdb): incorrect type conversion during RDB loads the int8 encoding #2547

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why int if the length is 8?

Copy link
Contributor

@torwig torwig Sep 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try uint8_t? However, I'm not sure that it's the right type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be correct to use int8_t here since the encoding is int8. Cast it to int will drop the sign bit.

} 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)
}
}
Loading