Skip to content

Fix infinite loop during reverse iteration due to invalid numfields of corrupted stream#14472

Merged
sundb merged 1 commit intoredis:unstablefrom
sundb:stream-corrupt-1
Jan 5, 2026
Merged

Fix infinite loop during reverse iteration due to invalid numfields of corrupted stream#14472
sundb merged 1 commit intoredis:unstablefrom
sundb:stream-corrupt-1

Conversation

@sundb
Copy link
Collaborator

@sundb sundb commented Oct 27, 2025

Follow #14423

In #14423,
I thought the last lpNext operation of the iterator occurred at the end of streamIteratorGetID.
However, I overlooked the fact that after calling streamIteratorGetID(), we might still use streamIteratorGetField() to continue moving within the current entry.
This means that during reverse iteration, the iterator could move back to a previous entry position.

To fix this, in this PR I record the current position at the beginning of streamIteratorGetID().
When we enter it again next time, we ensure that the entry position does not exceed the previous one,
that is, during forward iteration the entry must be greater than the last entry position,
and during reverse iteration it must be smaller than the last entry position.

Note that the fix for #14423 has been replaced by this fix.

@sundb sundb marked this pull request as ready for review January 4, 2026 07:08
/* Record the previous lp_ele position to detect data corruption
* that might cause the iterator to move backwards unexpectedly. */
if (si->lp_ele && si->lp_last_ele)
serverAssert(si->rev ? si->lp_ele < si->lp_last_ele : si->lp_ele > si->lp_last_ele);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have guarantee that address in pointers are in increasing order in the list?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, otherwise this iterator would eventually end up in an infinite loop, constantly going back and forth.

Copy link
Collaborator

@sggeorgiev sggeorgiev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@sundb sundb merged commit 4eda670 into redis:unstable Jan 5, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants