mirror of
https://github.com/dragonflydb/dragonfly.git
synced 2025-05-11 10:25:47 +02:00
fix: another fix
This commit is contained in:
parent
2dc92ae823
commit
bc2cc17f08
2 changed files with 25 additions and 12 deletions
|
@ -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)) {
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue