< Summary

Information
Class: DotnetFuzzing.Fuzzers.ZipArchiveFuzzer
Assembly: DotnetFuzzing
File(s): D:\runner\runtime\src\libraries\Fuzzing\DotnetFuzzing\Fuzzers\ZipArchiveFuzzer.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 53
Coverable lines: 53
Total lines: 87
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
FuzzTarget(...)0%220%
CopyToRentedArray(...)100%110%
TestArchive()0%660%

File(s)

D:\runner\runtime\src\libraries\Fuzzing\DotnetFuzzing\Fuzzers\ZipArchiveFuzzer.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Buffers;
 5using System.IO.Compression;
 6using System.Threading.Tasks;
 7
 8namespace DotnetFuzzing.Fuzzers;
 9
 10internal sealed class ZipArchiveFuzzer : IFuzzer
 11{
 012    public string[] TargetAssemblies { get; } = ["System.IO.Compression"];
 013    public string[] TargetCoreLibPrefixes => [];
 014    public string Corpus => "ziparchive";
 15
 16    public void FuzzTarget(ReadOnlySpan<byte> bytes)
 017    {
 018        if (bytes.IsEmpty)
 019        {
 020            return;
 21        }
 22
 023        TestArchive(CopyToRentedArray(bytes), bytes.Length, async: false).GetAwaiter().GetResult();
 024        TestArchive(CopyToRentedArray(bytes), bytes.Length, async: true).GetAwaiter().GetResult();
 025    }
 26
 27    private byte[] CopyToRentedArray(ReadOnlySpan<byte> bytes)
 028    {
 029        byte[] buffer = ArrayPool<byte>.Shared.Rent(bytes.Length);
 30        try
 031        {
 032            bytes.CopyTo(buffer);
 033            return buffer;
 34        }
 035        catch
 036        {
 037            ArrayPool<byte>.Shared.Return(buffer);
 038            throw;
 39        }
 040    }
 41
 42    private async Task TestArchive(byte[] buffer, int length, bool async)
 043    {
 44        try
 045        {
 046            using var stream = new MemoryStream(buffer, 0, length);
 47
 48            ZipArchive archive;
 49
 050            if (async)
 051            {
 052                archive = await ZipArchive.CreateAsync(stream, ZipArchiveMode.Read, leaveOpen: false, entryNameEncoding:
 053            }
 54            else
 055            {
 056                archive = new ZipArchive(stream, ZipArchiveMode.Read, leaveOpen: false, entryNameEncoding: null);
 057            }
 58
 059            foreach (var entry in archive.Entries)
 060            {
 61                // Access entry properties to simulate usage
 062                _ = entry.FullName;
 063                _ = entry.Length;
 064                _ = entry.Comment;
 065                _ = entry.LastWriteTime;
 066                _ = entry.Crc32;
 067            }
 68
 069            if (async)
 070            {
 071                await archive.DisposeAsync();
 072            }
 73            else
 074            {
 075                archive.Dispose();
 076            }
 077        }
 078        catch (InvalidDataException)
 079        {
 80            // ignore, this exception is expected to be thrown for invalid/corrupted archives.
 081        }
 82        finally
 083        {
 084            ArrayPool<byte>.Shared.Return(buffer);
 085        }
 086    }
 87}