Skip to content

Commit 67117ff

Browse files
authored
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 <[email protected]>
1 parent 2c32e50 commit 67117ff

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/server/generic_family.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,9 +1184,10 @@ void GenericFamily::Sort(CmdArgList args, ConnectionContext* cntx) {
11841184
});
11851185
} else {
11861186
std::sort(entries.begin(), entries.end(),
1187-
[reversed, &entries](const auto& lhs, const auto& rhs) {
1188-
DCHECK(&rhs < &(*entries.end()) && &rhs >= &(*entries.begin()));
1189-
return bool(lhs.Cmp() < rhs.Cmp()) ^ reversed;
1187+
[reversed, &entries](const auto& lhs, const auto& rhs) -> bool {
1188+
DCHECK((&rhs - entries.data()) >= 0);
1189+
DCHECK((&rhs - entries.data()) < int64_t(entries.size()));
1190+
return reversed ? rhs.Cmp() < lhs.Cmp() : lhs.Cmp() < rhs.Cmp();
11901191
});
11911192
}
11921193

src/server/generic_family_test.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,15 @@ TEST_F(GenericFamilyTest, Sort) {
481481
;
482482
}
483483

484+
TEST_F(GenericFamilyTest, SortBug3636) {
485+
Run({"RPUSH", "foo", "1.100000023841858", "1.100000023841858", "1.100000023841858", "-15710",
486+
"1.100000023841858", "1.100000023841858", "1.100000023841858", "-15710", "-15710",
487+
"1.100000023841858", "-15710", "-15710", "-15710", "-15710", "1.100000023841858", "-15710",
488+
"-15710"});
489+
auto resp = Run({"SORT", "foo", "desc", "alpha"});
490+
ASSERT_THAT(resp, ArrLen(17));
491+
}
492+
484493
TEST_F(GenericFamilyTest, TimeNoKeys) {
485494
auto resp = Run({"time"});
486495
EXPECT_THAT(resp, ArrLen(2));

0 commit comments

Comments
 (0)