Fix infinite loop in SourceStream.Seek for malformed archives#1178
Merged
adamhathcock merged 3 commits intoreleasefrom Feb 2, 2026
Merged
Fix infinite loop in SourceStream.Seek for malformed archives#1178adamhathcock merged 3 commits intoreleasefrom
adamhathcock merged 3 commits intoreleasefrom
Conversation
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix infinite loop when counting entries in malformed RAR archive
Fix infinite loop in SourceStream.Seek for malformed archives
Feb 2, 2026
adamhathcock
approved these changes
Feb 2, 2026
Contributor
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (3 files)
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens SourceStream.Seek to avoid infinite loops when reading malformed RAR archives by detecting non-progressing stream transitions and surfacing them as clear exceptions. It also adds a regression test using the real 512-byte malformed RAR from the linked issue to ensure enumeration now fails fast instead of hanging.
Changes:
- Updated
SourceStream.Seekto:- Throw
InvalidOperationExceptionwhenSetStreamindicates there are no more streams available before reaching the requested position. - Detect consecutive zero-length streams that would prevent forward progress and throw a descriptive
InvalidOperationException.
- Throw
- Added a RAR archive test
Rar_MalformedArchive_NoInfiniteLoopthat opens the malformed 512-byte RAR withLookForHeader = trueand asserts anInvalidOperationExceptionis thrown when enumerating entries, verifying the exception message contains"Cannot seek to position". - Added the
Rar.malformed_512byte.rartest fixture undertests/TestArchives/Archivesto reproduce and lock in behavior for the reported issue.
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/SharpCompress/IO/SourceStream.cs |
Adjusts Seek’s loop over internal streams to throw on end-of-stream and consecutive zero-length streams, preventing the infinite loop when seeking into malformed archives. |
tests/SharpCompress.Test/Rar/RarArchiveTests.cs |
Adds a focused regression test that opens the malformed RAR with ReaderOptions.LookForHeader = true and asserts a non-hanging InvalidOperationException with a seek-related message. |
tests/TestArchives/Archives/Rar.malformed_512byte.rar |
Introduces the malformed 512-byte RAR fixture used by the new regression test to reproduce the prior infinite-loop condition. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This was referenced Feb 11, 2026
Closed
This was referenced Feb 16, 2026
Closed
This was referenced Feb 23, 2026
Closed
This was referenced Mar 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Malformed RAR archives cause
SourceStream.Seekto loop infinitely when attempting to enumerate entries. The loop continues indefinitely becauseSetStreamdoesn't advance the stream position but also doesn't signal failure.Changes
Seekmethod's stream advancement loop:SetStreamreturns false (no more streams available)Both cases now throw
InvalidOperationExceptionwith descriptive messages indicating position and failure reason.Example
Test case added using actual 512-byte malformed RAR file from issue report.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.