diff options
author | Matthew Orlando <cogwheel@offbaseproductions.com> | 2016-02-03 00:57:42 +0300 |
---|---|---|
committer | Matthew Orlando <cogwheel@offbaseproductions.com> | 2016-02-03 00:57:42 +0300 |
commit | 5d67d566a4b2d0c8df60613cc3fb6e7051eccd0f (patch) | |
tree | 146745fba7bb4c3fb800afe607bc5a8a5f859261 /mcs/class/System.IO.Compression | |
parent | a9f9e3505e2a3cef7ed9b3ec60de733002c87481 (diff) |
Fix stream leak in all cases
Diffstat (limited to 'mcs/class/System.IO.Compression')
-rw-r--r-- | mcs/class/System.IO.Compression/ZipArchive.cs | 85 |
1 files changed, 35 insertions, 50 deletions
diff --git a/mcs/class/System.IO.Compression/ZipArchive.cs b/mcs/class/System.IO.Compression/ZipArchive.cs index e261da5d766..f2ef892a910 100644 --- a/mcs/class/System.IO.Compression/ZipArchive.cs +++ b/mcs/class/System.IO.Compression/ZipArchive.cs @@ -70,18 +70,7 @@ namespace System.IO.Compression this.stream = stream; this.mode = mode; leaveStreamOpen = leaveOpen; - - try - { - CreateZip(stream, mode); - } - catch - { - if (!leaveOpen) - stream.Dispose(); - - throw; - } + CreateZip(stream, mode); } public ZipArchive (Stream stream, ZipArchiveMode mode, bool leaveOpen, Encoding entryNameEncoding) @@ -93,52 +82,48 @@ namespace System.IO.Compression this.mode = mode; leaveStreamOpen = leaveOpen; this.entryNameEncoding = entryNameEncoding; - - try - { - CreateZip(stream, mode); - } - catch - { - if (!leaveOpen) - stream.Dispose(); - - throw; - } + CreateZip(stream, mode); } private void CreateZip(Stream stream, ZipArchiveMode mode) { - if (mode != ZipArchiveMode.Read && mode != ZipArchiveMode.Create && mode != ZipArchiveMode.Update) - throw new ArgumentOutOfRangeException("mode"); - - // If the mode parameter is set to Read, the stream must support reading. - if (mode == ZipArchiveMode.Read && !stream.CanRead) - throw new ArgumentException("Stream must support reading for Read archive mode"); - - // If the mode parameter is set to Create, the stream must support writing. - if (mode == ZipArchiveMode.Create && !stream.CanWrite) - throw new ArgumentException("Stream must support writing for Create archive mode"); - - // If the mode parameter is set to Update, the stream must support reading, writing, and seeking. - if (mode == ZipArchiveMode.Update && (!stream.CanRead || !stream.CanWrite || !stream.CanSeek)) - throw new ArgumentException("Stream must support reading, writing and seeking for Update archive mode"); - try { - zipFile = mode != ZipArchiveMode.Create && stream.Length != 0 - ? SharpCompress.Archive.Zip.ZipArchive.Open(stream) - : SharpCompress.Archive.Zip.ZipArchive.Create(); - } catch (Exception e) { - throw new InvalidDataException("The contents of the stream are not in the zip archive format.", e); - } + if (mode != ZipArchiveMode.Read && mode != ZipArchiveMode.Create && mode != ZipArchiveMode.Update) + throw new ArgumentOutOfRangeException("mode"); + + // If the mode parameter is set to Read, the stream must support reading. + if (mode == ZipArchiveMode.Read && !stream.CanRead) + throw new ArgumentException("Stream must support reading for Read archive mode"); + + // If the mode parameter is set to Create, the stream must support writing. + if (mode == ZipArchiveMode.Create && !stream.CanWrite) + throw new ArgumentException("Stream must support writing for Create archive mode"); + + // If the mode parameter is set to Update, the stream must support reading, writing, and seeking. + if (mode == ZipArchiveMode.Update && (!stream.CanRead || !stream.CanWrite || !stream.CanSeek)) + throw new ArgumentException("Stream must support reading, writing and seeking for Update archive mode"); + + try { + zipFile = mode != ZipArchiveMode.Create && stream.Length != 0 + ? SharpCompress.Archive.Zip.ZipArchive.Open(stream) + : SharpCompress.Archive.Zip.ZipArchive.Create(); + } catch (Exception e) { + throw new InvalidDataException("The contents of the stream are not in the zip archive format.", e); + } - entries = new Dictionary<string, ZipArchiveEntry>(); - if (Mode != ZipArchiveMode.Create) { - foreach (var entry in zipFile.Entries) { - var zipEntry = new ZipArchiveEntry(this, entry); - entries[entry.Key] = zipEntry; + entries = new Dictionary<string, ZipArchiveEntry>(); + if (Mode != ZipArchiveMode.Create) { + foreach (var entry in zipFile.Entries) { + var zipEntry = new ZipArchiveEntry(this, entry); + entries[entry.Key] = zipEntry; + } } } + catch { + if (!leaveStreamOpen) + stream.Dispose(); + throw; + } } public ReadOnlyCollection<ZipArchiveEntry> Entries { |