Fix DivideByZeroException when compressing empty files with BZip2#1043
Merged
adamhathcock merged 2 commits intomasterfrom Nov 27, 2025
Merged
Fix DivideByZeroException when compressing empty files with BZip2#1043adamhathcock merged 2 commits intomasterfrom
adamhathcock merged 2 commits intomasterfrom
Conversation
Co-authored-by: adamhathcock <[email protected]>
Copilot
AI
changed the title
[WIP] Fix DivideByZeroException when using Zip with BZip2
Fix DivideByZeroException when compressing empty files with BZip2
Nov 27, 2025
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a DivideByZeroException that occurs when writing empty files or folders to a Zip archive using BZip2 compression. The exception was caused by the modulo operation (i % (last + 1)) in MainSort() evaluating to (i % 0) when last = -1 (indicating no data was written to the block).
- Added an early return in
CBZip2OutputStream.EndBlock()to skip block processing when no data has been written (last < 0) - Added test coverage for both empty file and empty folder scenarios with BZip2 compression
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs | Adds early return in EndBlock() to prevent divide-by-zero when compressing empty input |
| tests/SharpCompress.Test/Zip/ZipWriterTests.cs | Adds regression tests for empty file and folder scenarios with BZip2 compression |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
adamhathcock
approved these changes
Nov 27, 2025
This was referenced Nov 28, 2025
This was referenced Dec 8, 2025
This was referenced Dec 22, 2025
This was referenced Jan 5, 2026
This was referenced Jan 12, 2026
This was referenced Jan 19, 2026
This was referenced Jan 26, 2026
This was referenced Feb 13, 2026
This was referenced Feb 23, 2026
This was referenced Mar 4, 2026
This was referenced Mar 23, 2026
This was referenced Apr 1, 2026
This was referenced Apr 20, 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.
Writing empty files or folders to a Zip archive with BZip2 compression throws
DivideByZeroExceptioninMainSort()due to(i % (last + 1))evaluating to(i % 0)whenlast = -1.Changes
EndBlock()whenlast < 0to skip block processing for empty inputOriginal prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.