Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid IndexOutOfRangeException in ZipArchive #109168

Merged
merged 6 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,22 @@ public void ReadArchive_WithDiskStartNumberGreaterThanIntMax()
Assert.Null(exception);
}

[Theory]
[InlineData("HuffmanTreeException.zip")]
public static async Task ZipArchive_InvalidHuffmanData(string zipname)
{
string filename = bad(zipname);
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
Loading