Skip to content

Commit 1ce82e7

Browse files
peter15914ericstjViktorHofer
authored
Avoid IndexOutOfRangeException in ZipArchive (#109168)
* Avoid IndexOutOfRangeException in ZipArchive * Add Sys.IO.Compression test Add test to cover problem with invalid zip file. Currently an IndexOutOfRangeException is thrown when invalid zip file is opened with ZipArchive. * Update Add Sys.IO.Compression test Change "Theory" to "Fact" Co-authored-by: Eric StJohn <[email protected]> --------- Co-authored-by: Eric StJohn <[email protected]> Co-authored-by: Viktor Hofer <[email protected]>
1 parent 07e4c1b commit 1ce82e7

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateManaged/HuffmanTree.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,12 @@ private void CreateTable()
247247
}
248248
index = -value; // go to next node
249249

250+
if (index >= array.Length)
251+
{
252+
// prevent an IndexOutOfRangeException from array[index]
253+
throw new InvalidDataException(SR.InvalidHuffmanData);
254+
}
255+
250256
codeBitMask <<= 1;
251257
overflowBits--;
252258
} while (overflowBits != 0);

src/libraries/System.IO.Compression/tests/ZipArchive/zip_InvalidParametersAndStrangeFiles.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,24 @@ public void ReadArchive_WithDiskStartNumberGreaterThanIntMax()
871871
Assert.Null(exception);
872872
}
873873

874+
/// <summary>
875+
/// This test checks that an InvalidDataException will be thrown when consuming a zip with bad Huffman data.
876+
/// </summary>
877+
[Fact]
878+
public static async Task ZipArchive_InvalidHuffmanData()
879+
{
880+
string filename = bad("HuffmanTreeException.zip");
881+
using (ZipArchive archive = new ZipArchive(await StreamHelpers.CreateTempCopyStream(filename), ZipArchiveMode.Read))
882+
{
883+
ZipArchiveEntry e = archive.Entries[0];
884+
using (MemoryStream ms = new MemoryStream())
885+
using (Stream s = e.Open())
886+
{
887+
Assert.Throws<InvalidDataException>(() => s.CopyTo(ms)); //"Should throw on creating Huffman tree"
888+
}
889+
}
890+
}
891+
874892
private static readonly byte[] s_slightlyIncorrectZip64 =
875893
{
876894
// ===== Local file header signature 0x04034b50

0 commit comments

Comments
 (0)