fix: another fix

This commit is contained in:
Volodymyr Yavdoshenko 2025-04-24 17:26:23 +03:00
parent 2dc92ae823
commit bc2cc17f08
No known key found for this signature in database
GPG key ID: 24BC74845F4F4064
2 changed files with 25 additions and 12 deletions

View file

@ -728,9 +728,17 @@ ResultType Get::ApplyTo(Overflow ov, const string* bitfield) {
const size_t offset = attr_.offset;
auto last_byte_offset = GetByteIndex(attr_.offset + attr_.encoding_bit_size - 1);
if (GetByteIndex(offset) >= total_bytes && attr_.encoding_bit_size > 0) {
return 0;
}
const string* result_str = bitfield;
string buff;
uint32_t lsb = attr_.offset + attr_.encoding_bit_size - 1;
if (last_byte_offset > total_bytes) {
return {};
if (last_byte_offset >= total_bytes) {
buff = *bitfield;
buff.resize(last_byte_offset + 1, 0);
result_str = &buff;
}
const bool is_negative =
@ -738,7 +746,7 @@ ResultType Get::ApplyTo(Overflow ov, const string* bitfield) {
int64_t result = 0;
for (size_t i = 0; i < attr_.encoding_bit_size; ++i) {
uint8_t byte{GetByteValue(bytes, lsb)};
uint8_t byte{GetByteValue(*result_str, lsb)};
int32_t index = GetNormalizedBitIndex(lsb);
int64_t old_bit = CheckBitStatus(byte, index);
result |= old_bit << i;
@ -830,10 +838,11 @@ ResultType IncrBy::ApplyTo(Overflow ov, string* bitfield) {
string& bytes = *bitfield;
Get get(attr_);
auto res = get.ApplyTo(ov, &bytes);
const int32_t total_bytes = static_cast<int32_t>(bytes.size());
auto last_byte_offset = GetByteIndex(attr_.offset + attr_.encoding_bit_size - 1);
if (!res) {
Set set(attr_, incr_value_);
return set.ApplyTo(ov, &bytes);
if (last_byte_offset >= total_bytes) {
bytes.resize(last_byte_offset + 1, 0);
}
if (!HandleOverflow(ov, &*res)) {

View file

@ -808,14 +808,18 @@ TEST_F(BitOpsFamilyTest, BitFieldOperations) {
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", "0", "overflow", "fail", "incrby", "u32", "0",
"4294967295"});
EXPECT_THAT(resp, RespArray(ElementsAre(IntArg(1650553344), ArgType(RespExpr::NIL))));
auto resp = Run({"bitfield", "foo", "get", "u32", "4294967295"});
EXPECT_THAT(resp, ArgType(RespExpr::NIL));
resp = Run({"strlen", "foo"});
EXPECT_THAT(resp, 4);
resp = Run({"bitfield", "foo", "get", "u32", "8589934590"});
EXPECT_THAT(resp, ArgType(RespExpr::NIL));
resp = Run({"get", "foo"});
EXPECT_THAT(ToSV(resp.GetBuf()), Eq(std::string_view("bar\0", 4)));
resp = Run({"bitfield", "foo", "get", "u32", "4294967295"});
EXPECT_THAT(resp, 0);
}
} // end of namespace dfly