-
Notifications
You must be signed in to change notification settings - Fork 510
Fix InvalidOperationException when RAR uncompressed size exceeds header value #1104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
adamhathcock
merged 6 commits into
master
from
copilot/fix-invalidoperationexception-rar
Jan 4, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bb66100
Initial plan
Copilot b25493f
Fix InvalidOperationException when RAR unpacks more data than header …
Copilot 83b1125
Add test for InvalidOperationException fix in RAR extraction
Copilot 1257479
Update test to document exception behavior for RAR stream validation
Copilot 90a33ce
Remove duplicate test that doesn't verify exception case
Copilot 41ed4c8
Add negative test case for premature stream termination
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| using System; | ||
| using System.IO; | ||
|
|
||
| namespace SharpCompress.Test.Mocks; | ||
|
|
||
| /// <summary> | ||
| /// A stream wrapper that truncates the underlying stream after reading a specified number of bytes. | ||
| /// Used for testing error handling when streams end prematurely. | ||
| /// </summary> | ||
| public class TruncatedStream : Stream | ||
| { | ||
| private readonly Stream baseStream; | ||
| private readonly long truncateAfterBytes; | ||
| private long bytesRead; | ||
|
|
||
| public TruncatedStream(Stream baseStream, long truncateAfterBytes) | ||
| { | ||
| this.baseStream = baseStream ?? throw new ArgumentNullException(nameof(baseStream)); | ||
| this.truncateAfterBytes = truncateAfterBytes; | ||
| bytesRead = 0; | ||
| } | ||
|
|
||
| public override bool CanRead => baseStream.CanRead; | ||
| public override bool CanSeek => baseStream.CanSeek; | ||
| public override bool CanWrite => false; | ||
| public override long Length => baseStream.Length; | ||
|
|
||
| public override long Position | ||
| { | ||
| get => baseStream.Position; | ||
| set => baseStream.Position = value; | ||
| } | ||
|
|
||
| public override int Read(byte[] buffer, int offset, int count) | ||
| { | ||
| if (bytesRead >= truncateAfterBytes) | ||
| { | ||
| // Simulate premature end of stream | ||
| return 0; | ||
| } | ||
|
|
||
| var maxBytesToRead = (int)Math.Min(count, truncateAfterBytes - bytesRead); | ||
| var actualBytesRead = baseStream.Read(buffer, offset, maxBytesToRead); | ||
| bytesRead += actualBytesRead; | ||
| return actualBytesRead; | ||
| } | ||
|
|
||
| public override long Seek(long offset, SeekOrigin origin) => baseStream.Seek(offset, origin); | ||
|
|
||
| public override void SetLength(long value) => throw new NotSupportedException(); | ||
|
|
||
| public override void Write(byte[] buffer, int offset, int count) => | ||
| throw new NotSupportedException(); | ||
|
|
||
| public override void Flush() => baseStream.Flush(); | ||
|
|
||
| protected override void Dispose(bool disposing) | ||
| { | ||
| if (disposing) | ||
| { | ||
| baseStream?.Dispose(); | ||
| } | ||
| base.Dispose(disposing); | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.