Skip to content

Fix infinite loop during reverse iteration due to corrupted stream#14423

Merged
sundb merged 4 commits intoredis:unstablefrom
sundb:stream-revrange-corrupt
Oct 15, 2025
Merged

Fix infinite loop during reverse iteration due to corrupted stream#14423
sundb merged 4 commits intoredis:unstablefrom
sundb:stream-revrange-corrupt

Conversation

@sundb
Copy link
Collaborator

@sundb sundb commented Oct 10, 2025

When the lp_count of a stream entry is incorrect, and we are performing a reverse iteration, we can normally move backward by lp_count to locate the previous entry.
However, if lp_count is wrong, during forward iteration, after obtaining the flag, ID, and other fields, we may step beyond the current entry into the start position of the next entry(where we came from), which could cause an infinite loop.

@sundb sundb requested review from Copilot and oranagra October 10, 2025 08:21
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes an infinite loop bug that occurs during reverse iteration of Redis streams when the lp_count field in a stream entry is corrupted. The fix adds a boundary check to prevent the iterator from moving beyond the current entry during reverse iteration.

  • Adds boundary validation to detect when reverse iteration steps beyond entry limits
  • Includes test case to verify the fix prevents infinite loops with corrupted stream data

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/t_stream.c Adds boundary tracking and assertion to prevent infinite loops during reverse stream iteration
tests/integration/corrupt-dump.tcl Adds test case with corrupted stream data to verify the infinite loop fix

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@sundb sundb merged commit 166ae60 into redis:unstable Oct 15, 2025
19 checks passed
@github-project-automation github-project-automation bot moved this from Todo to Done in Redis 8.4 Oct 15, 2025
@sundb sundb deleted the stream-revrange-corrupt branch October 27, 2025 11:50
sundb added a commit that referenced this pull request Jan 5, 2026
…f corrupted stream (#14472)

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants