[Backport 5.2] cql: fix hang during certain SELECT statements #18716
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The function intersection(r1,r2) in statement_restrictions.cc is used when several WHERE restrictions were applied to the same column. For example, for "WHERE b<1 AND b<2" the intersection of the two ranges is calculated to be b<1.
As noted in issue #18690, Scylla is inconsistent in where it allows or doesn't allow these intersecting restrictions. But where they are allowed they must be implemented correctly. And it turns out the function intersection() had a bug that caused it to sometimes enter an infinite loop - when the intent was only to call itself once with swapped parameters.
This patch includes a test reproducing this bug, and a fix for the bug. The test hangs before the fix, and passes after the fix.
While at it, I carefully reviewed the entire code used to implement the intersection() function to try to make sure that the bug we found was the only one. I also added a few more comments where I thought they were needed to understand complicated logic of the code.
The bug, the fix and the test were originally discovered by Michał Chojnowski.
Fixes #18688
Refs #18690
This patch should be backported to all extant branches: It is a bug that affected an actual user; The hang is a serious problem (in some sense it's even worse than a crash because we can't recover from it, while after a crash we restart Scylla), and this bug can even be used to DoS-attack Scylla by sending a CQL request with a maliciously-crafted set of restrictions.
(cherry picked from commit 2f6cd04)
Refs #18694