From 7c96d04c5d1e5a0c8c110d71ab1640bd9bbe5b02 Mon Sep 17 00:00:00 2001 From: dacantu Date: Wed, 21 Dec 2022 21:52:26 -0600 Subject: [PATCH] TarReader should dispose underlying stream if leaveOpen is false --- .../src/System/Formats/Tar/TarReader.cs | 13 +++++++++---- .../tests/TarReader/TarReader.Tests.cs | 4 ++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarReader.cs b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarReader.cs index a8913696a9c00..22c7970644b0b 100644 --- a/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarReader.cs +++ b/src/libraries/System.Formats.Tar/src/System/Formats/Tar/TarReader.cs @@ -48,7 +48,7 @@ public TarReader(Stream archiveStream, bool leaveOpen = false) } /// - /// Disposes the current instance, and disposes the non-null instances of all the entries that were read from the archive. + /// Disposes the current instance, closes the archive stream, and disposes the non-null instances of all the entries that were read from the archive if the leaveOpen argument was set to in the constructor. /// /// The property of any entry can be replaced with a new stream. If the user decides to replace it on a instance that was obtained using a , the underlying stream gets disposed immediately, freeing the of origin from the responsibility of having to dispose it. public void Dispose() @@ -57,11 +57,16 @@ public void Dispose() { _isDisposed = true; - if (!_leaveOpen && _dataStreamsToDispose?.Count > 0) + if (!_leaveOpen) { - foreach (Stream s in _dataStreamsToDispose) + _archiveStream.Dispose(); + + if (_dataStreamsToDispose?.Count > 0) { - s.Dispose(); + foreach (Stream s in _dataStreamsToDispose) + { + s.Dispose(); + } } } } diff --git a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.Tests.cs b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.Tests.cs index 8b41a5e9bb13b..6de99107bd746 100644 --- a/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.Tests.cs +++ b/src/libraries/System.Formats.Tar/tests/TarReader/TarReader.Tests.cs @@ -38,6 +38,8 @@ public void TarReader_LeaveOpen_False() } } + Assert.Throws(() => ms.ReadByte()); + Assert.True(dataStreams.Any()); foreach (Stream ds in dataStreams) { @@ -62,6 +64,8 @@ public void TarReader_LeaveOpen_True() } } + ms.ReadByte(); // Should not throw + Assert.True(dataStreams.Any()); foreach (Stream ds in dataStreams) {