[PR #1162] release to master #1601

Closed
opened 2026-01-29 22:21:19 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/adamhathcock/sharpcompress/pull/1162

State: closed
Merged: Yes


This pull request improves the safety and robustness of the BufferedSubStream implementation in SharpCompress, particularly around resource management and stream disposal. The changes ensure that the underlying buffer is managed correctly, prevent double disposal issues, and add a new test to verify this behavior. Additionally, minor code improvements and test enhancements are included.

Resource management and disposal safety

  • Added an _isDisposed flag to BufferedSubStream to prevent double disposal and ensure the buffer is only returned to the pool once, avoiding pool corruption. The buffer (_cache) is now set to null after being returned. ([src/SharpCompress/IO/BufferedSubStream.csL32-R50](diffhunk://#diff-e05b040e70e38b0d116d8d268e407131b5ba8b6614af841bc9188cd7f368fee3L32-R50))
  • Updated all usages of _cache to use null-forgiving operator (_cache!) and added checks for _isDisposed in methods that access the buffer, throwing ObjectDisposedException if accessed after disposal. ([[1]](diffhunk://#diff-e05b040e70e38b0d116d8d268e407131b5ba8b6614af841bc9188cd7f368fee3L64-R116), [[2]](diffhunk://#diff-e05b040e70e38b0d116d8d268e407131b5ba8b6614af841bc9188cd7f368fee3L109-R139), [[3]](diffhunk://#diff-e05b040e70e38b0d116d8d268e407131b5ba8b6614af841bc9188cd7f368fee3L127-R157), [[4]](diffhunk://#diff-e05b040e70e38b0d116d8d268e407131b5ba8b6614af841bc9188cd7f368fee3L150-R180), [[5]](diffhunk://#diff-e05b040e70e38b0d116d8d268e407131b5ba8b6614af841bc9188cd7f368fee3L177-R207))

Test improvements

  • Added a new unit test BufferedSubStream_DoubleDispose_DoesNotCorruptArrayPool to verify that double disposal does not corrupt the array pool or throw exceptions. ([tests/SharpCompress.Test/Streams/SharpCompressStreamTest.csR100-R120](diffhunk://#diff-5203913d939d601de98a07fe1ef8384114c7cbb9de17ccc5a93297b51c301e6eR100-R120))
  • Modified the existing test to use a ForwardOnlyStream wrapper for more accurate stream behavior simulation. ([tests/SharpCompress.Test/Streams/SharpCompressStreamTest.csL67-R75](diffhunk://#diff-5203913d939d601de98a07fe1ef8384114c7cbb9de17ccc5a93297b51c301e6eL67-R75))
  • Added missing test mock import for completeness. ([tests/SharpCompress.Test/Streams/SharpCompressStreamTest.csR8](diffhunk://#diff-5203913d939d601de98a07fe1ef8384114c7cbb9de17ccc5a93297b51c301e6eR8))

Minor codebase cleanups

  • Removed an unused variable (orig) from the Seek method in SharpCompressStream. ([src/SharpCompress/IO/SharpCompressStream.csL258](diffhunk://#diff-d90e17bbeae23cfc33ba23708baa5342f5eb76666ee072503791fee3432a1c04L258))
**Original Pull Request:** https://github.com/adamhathcock/sharpcompress/pull/1162 **State:** closed **Merged:** Yes --- This pull request improves the safety and robustness of the `BufferedSubStream` implementation in `SharpCompress`, particularly around resource management and stream disposal. The changes ensure that the underlying buffer is managed correctly, prevent double disposal issues, and add a new test to verify this behavior. Additionally, minor code improvements and test enhancements are included. ### Resource management and disposal safety * Added an `_isDisposed` flag to `BufferedSubStream` to prevent double disposal and ensure the buffer is only returned to the pool once, avoiding pool corruption. The buffer (`_cache`) is now set to `null` after being returned. (`[src/SharpCompress/IO/BufferedSubStream.csL32-R50](diffhunk://#diff-e05b040e70e38b0d116d8d268e407131b5ba8b6614af841bc9188cd7f368fee3L32-R50)`) * Updated all usages of `_cache` to use null-forgiving operator (`_cache!`) and added checks for `_isDisposed` in methods that access the buffer, throwing `ObjectDisposedException` if accessed after disposal. (`[[1]](diffhunk://#diff-e05b040e70e38b0d116d8d268e407131b5ba8b6614af841bc9188cd7f368fee3L64-R116)`, `[[2]](diffhunk://#diff-e05b040e70e38b0d116d8d268e407131b5ba8b6614af841bc9188cd7f368fee3L109-R139)`, `[[3]](diffhunk://#diff-e05b040e70e38b0d116d8d268e407131b5ba8b6614af841bc9188cd7f368fee3L127-R157)`, `[[4]](diffhunk://#diff-e05b040e70e38b0d116d8d268e407131b5ba8b6614af841bc9188cd7f368fee3L150-R180)`, `[[5]](diffhunk://#diff-e05b040e70e38b0d116d8d268e407131b5ba8b6614af841bc9188cd7f368fee3L177-R207)`) ### Test improvements * Added a new unit test `BufferedSubStream_DoubleDispose_DoesNotCorruptArrayPool` to verify that double disposal does not corrupt the array pool or throw exceptions. (`[tests/SharpCompress.Test/Streams/SharpCompressStreamTest.csR100-R120](diffhunk://#diff-5203913d939d601de98a07fe1ef8384114c7cbb9de17ccc5a93297b51c301e6eR100-R120)`) * Modified the existing test to use a `ForwardOnlyStream` wrapper for more accurate stream behavior simulation. (`[tests/SharpCompress.Test/Streams/SharpCompressStreamTest.csL67-R75](diffhunk://#diff-5203913d939d601de98a07fe1ef8384114c7cbb9de17ccc5a93297b51c301e6eL67-R75)`) * Added missing test mock import for completeness. (`[tests/SharpCompress.Test/Streams/SharpCompressStreamTest.csR8](diffhunk://#diff-5203913d939d601de98a07fe1ef8384114c7cbb9de17ccc5a93297b51c301e6eR8)`) ### Minor codebase cleanups * Removed an unused variable (`orig`) from the `Seek` method in `SharpCompressStream`. (`[src/SharpCompress/IO/SharpCompressStream.csL258](diffhunk://#diff-d90e17bbeae23cfc33ba23708baa5342f5eb76666ee072503791fee3432a1c04L258)`)
claunia added the pull-request label 2026-01-29 22:21:19 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1601