fix: bitops_family crash fixed

This commit is contained in:
Volodymyr Yavdoshenko 2025-04-24 10:58:01 +03:00
parent 680722d8ff
commit 2c3959b066
No known key found for this signature in database
GPG key ID: 24BC74845F4F4064
2 changed files with 18 additions and 1 deletions

View file

@ -138,7 +138,11 @@ constexpr int32_t GetByteIndex(uint32_t offset) noexcept {
}
uint8_t GetByteValue(string_view str, uint32_t offset) {
return static_cast<uint8_t>(str[GetByteIndex(offset)]);
int32_t byte_index = GetByteIndex(offset);
if (byte_index >= static_cast<int32_t>(str.size())) {
return 0;
}
return static_cast<uint8_t>(str[byte_index]);
}
constexpr bool CheckBitStatus(uint8_t byte, uint32_t offset) {

View file

@ -805,4 +805,17 @@ TEST_F(BitOpsFamilyTest, BitFieldOperations) {
ASSERT_THAT(Run({"bitfield", "foo", "get", "u1", "15"}), IntArg(1));
}
TEST_F(BitOpsFamilyTest, BitFieldLargeOffset) {
Run({"set", "foo", "bar"});
Run({"bitfield", "foo", "get", "u32", "0", "overflow", "fail", "incrby", "u32", "0",
"4294967295"});
auto resp = Run({"bitfield", "foo", "get", "u32", "4294967295"});
EXPECT_EQ(resp, 825276672);
resp = Run({"bitfield", "foo", "get", "u32", "8589934590"});
EXPECT_EQ(resp, 412638336);
}
} // end of namespace dfly