Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

TestArchive doesn't handle invalid offsets correctly

未关闭
#856 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
csharp
领域
tooling

调研方向

从 src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs 中 TestArchive 循环附近以及 issue 所引用的 entry.Offset 处理开始。跟踪无效偏移量如何到达 System.IO,然后添加回归测试覆盖,表明 TestArchive 会报告无效条目,并在 TestStrategy.FindAllErrors 下继续测试其他条目。

由索引模型根据 Issue 内容生成。

描述

bug zip
Describe the bug

Entry header offsets are not checked for validity before trying to seek to them in the stream. Invalid offsets (i.e. <0) cause InvalidArgumentExceptions to be thrown from inside System.IO code, which are not caught in the existing ZipException handlers. They bubble up and cause the entire archive to fail validation with a cryptic "The parameter is incorrect - {filename}" message.

Reproduction Code
using ICSharpCode.SharpZipLib.Zip;

namespace TestArchive
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string zipPath = @"C:\temp\test.zip";
            using var stream = System.IO.File.OpenRead(zipPath);
            using var zip = new ZipFile(stream);
            bool isValid = zip.TestArchive(false, TestStrategy.FindAllErrors, ZipTestResultHandler); 
            if (!isValid)
            {
                throw new ArgumentException("Zip file is invalid!");
            }
        }

        static void ZipTestResultHandler(TestStatus status, string message)
        {
            if (status.Entry != null && !status.EntryValid && !string.IsNullOrEmpty(message))
            {
                Console.WriteLine(status.Entry.ZipFileIndex + ": " + status.Entry.Name);
                Console.WriteLine(message);
            }
        }
    }
}

Steps to reproduce
  1. Get a zip file with an entry whose Offset is negative (this is generally an invalid state and I don't know how to create one like this)
  2. Call TestArchive() and pass in a ZipTestResultHandler to get the test results
Expected behavior

I expect each individual entry with an invalid offset/header to be reported on (assuming TestStrategy.FindAllErrors is specified)

Operating System

Windows

Framework Version

Other

Tags

ZIP

Additional context

entry.Offset not being checked for > 0: https://github.com/icsharpcode/SharpZipLib/blob/master/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs#L1184

Where the InvalidArgumentException from deep in System.IO gets caught outside the while loop (additional entries are not tested): https://github.com/icsharpcode/SharpZipLib/blob/master/src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs#L1144

主要语言
C#
星标
3.9k
派生
1k
PR 合并指标
30 天内没有已合并 PR

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

icsharpcode/SharpZipLib 的其他 Issue

查看 icsharpcode/SharpZipLib 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。