Skip to content

Commit

Permalink
Avoid IndexOutOfRangeException in ZipArchive (#109168)
Browse files Browse the repository at this point in the history
* 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 <ericstj@microsoft.com>

---------

Co-authored-by: Eric StJohn <ericstj@microsoft.com>
Co-authored-by: Viktor Hofer <viktor.hofer@microsoft.com>
  • Loading branch information
3 people authored Jan 15, 2025
1 parent 07e4c1b commit 1ce82e7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ private void CreateTable()
}
index = -value; // go to next node

if (index >= array.Length)
{
// prevent an IndexOutOfRangeException from array[index]
throw new InvalidDataException(SR.InvalidHuffmanData);
}

codeBitMask <<= 1;
overflowBits--;
} while (overflowBits != 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,24 @@ public void ReadArchive_WithDiskStartNumberGreaterThanIntMax()
Assert.Null(exception);
}

/// <summary>
/// This test checks that an InvalidDataException will be thrown when consuming a zip with bad Huffman data.
/// </summary>
[Fact]
public static async Task ZipArchive_InvalidHuffmanData()
{
string filename = bad("HuffmanTreeException.zip");
using (ZipArchive archive = new ZipArchive(await StreamHelpers.CreateTempCopyStream(filename), ZipArchiveMode.Read))
{
ZipArchiveEntry e = archive.Entries[0];
using (MemoryStream ms = new MemoryStream())
using (Stream s = e.Open())
{
Assert.Throws<InvalidDataException>(() => s.CopyTo(ms)); //"Should throw on creating Huffman tree"
}
}
}

private static readonly byte[] s_slightlyIncorrectZip64 =
{
// ===== Local file header signature 0x04034b50
Expand Down

0 comments on commit 1ce82e7

Please sign in to comment.