fix: crash during SORT DESC call (#3637)

fixes #3636

The problem was that instead of implementing GT operator, we implemented
!LT, which is GE operator. As a result the iterators inside the sort algorithm reached
out of bounds.

---------

Signed-off-by: Roman Gershman <roman@dragonflydb.io>
This commit is contained in:
Roman Gershman 2024-09-03 15:00:31 +03:00 committed by GitHub
parent 2c32e5085c
commit 67117ff081
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 3 deletions

View file

@ -1184,9 +1184,10 @@ void GenericFamily::Sort(CmdArgList args, ConnectionContext* cntx) {
});
} else {
std::sort(entries.begin(), entries.end(),
[reversed, &entries](const auto& lhs, const auto& rhs) {
DCHECK(&rhs < &(*entries.end()) && &rhs >= &(*entries.begin()));
return bool(lhs.Cmp() < rhs.Cmp()) ^ reversed;
[reversed, &entries](const auto& lhs, const auto& rhs) -> bool {
DCHECK((&rhs - entries.data()) >= 0);
DCHECK((&rhs - entries.data()) < int64_t(entries.size()));
return reversed ? rhs.Cmp() < lhs.Cmp() : lhs.Cmp() < rhs.Cmp();
});
}

View file

@ -481,6 +481,15 @@ TEST_F(GenericFamilyTest, Sort) {
;
}
TEST_F(GenericFamilyTest, SortBug3636) {
Run({"RPUSH", "foo", "1.100000023841858", "1.100000023841858", "1.100000023841858", "-15710",
"1.100000023841858", "1.100000023841858", "1.100000023841858", "-15710", "-15710",
"1.100000023841858", "-15710", "-15710", "-15710", "-15710", "1.100000023841858", "-15710",
"-15710"});
auto resp = Run({"SORT", "foo", "desc", "alpha"});
ASSERT_THAT(resp, ArrLen(17));
}
TEST_F(GenericFamilyTest, TimeNoKeys) {
auto resp = Run({"time"});
EXPECT_THAT(resp, ArrLen(2));