Skip to content

System.InvalidOperationException in Deflate.Compress #182

@MerijnHendriks

Description

@MerijnHendriks

Describe the bug

When calling ZlibStream.Finish(); in deflate mode, System.InvalidOperationException is thrown.
This issue is not present in release 1.1.4.0 when using ZOutputStream.

Full stack trace:

System.InvalidOperationException: Operation is not valid due to the current state of the object.
  Stack Trace:
   at Elskom.Generic.Libs.Deflate.Compress(ZlibStream stream, ZlibFlushStrategy flush) in .\Haru.Compression\zlib.managed\Deflate.cs:line 416
   at Elskom.Generic.Libs.ZlibStream.Deflate(ZlibFlushStrategy flush) in .\Haru.Compression\zlib.managed\ZlibStream.cs:line 371
   at Elskom.Generic.Libs.ZlibStream.Finish() in .\Haru.Compression\zlib.managed\ZlibStream.cs:line 286
   at Haru.Compression.Zlib.Deflate(Byte[] data, ZlibCompression level) in .\Haru.Compression\Zlib.cs:line 17
   at Haru.Compression.Tests.ZlibTest.TestDeflate() in .\Haru.Compression.Tests\ZlibTest.cs:line 15

To Reproduce

I made a reproduction repo here.

If you do not wish to use the reproduction repo, you can use this instead:

using System.IO;
using System.Linq;
using Elskom.Generic.Libs;

class Program
{
    static readonly byte[] Inflated = new byte[]
    {
        // {"err":0,"errmsg":null,"data":{"utc_time":1692290139.1451}}
        0x7B, 0x22, 0x65, 0x72, 0x72, 0x22, 0x3A, 0x30, 0x2C, 0x22,
        0x65, 0x72, 0x72, 0x6D, 0x73, 0x67, 0x22, 0x3A, 0x6E, 0x75,
        0x6C, 0x6C, 0x2C, 0x22, 0x64, 0x61, 0x74, 0x61, 0x22, 0x3A,
        0x7B, 0x22, 0x75, 0x74, 0x63, 0x5F, 0x74, 0x69, 0x6D, 0x65,
        0x22, 0x3A, 0x31, 0x36, 0x39, 0x32, 0x32, 0x39, 0x30, 0x31,
        0x33, 0x39, 0x2E, 0x31, 0x34, 0x35, 0x31, 0x7D, 0x7D
    };

    static readonly byte[] Result = new byte[]
    {
        // [0x78, 0x9C]: [zlib, level 6]
        0x78, 0x9C, 0xAB, 0x56, 0x4A, 0x2D, 0x2A, 0x52, 0xB2, 0x32,
        0xD0, 0x01, 0xD1, 0xB9, 0xC5, 0xE9, 0x4A, 0x56, 0x79, 0xA5,
        0x39, 0x39, 0x3A, 0x4A, 0x29, 0x89, 0x25, 0x89, 0x4A, 0x56,
        0xD5, 0x4A, 0xA5, 0x25, 0xC9, 0xF1, 0x25, 0x99, 0xB9, 0xA9,
        0x4A, 0x56, 0x86, 0x66, 0x96, 0x46, 0x46, 0x96, 0x06, 0x86,
        0xC6, 0x96, 0x7A, 0x86, 0x26, 0xA6, 0x86, 0xB5, 0xB5, 0x00,
        0x36, 0x12, 0x11, 0xFC
    };

    static void Main()
    {
        using var msout = new MemoryStream();
        using var msin = new MemoryStream(Inflated);
        using var zs = new ZlibStream(msout, ZlibCompression.Level6, true);

        msin.CopyTo(zs);
        zs.Flush();
        zs.Finish();

        if (!msout.ToArray().SequenceEqual(Result))
        {
            throw new NotPackableException("Compression Failed.");
        }
    }
}

Expected behavior

ZlibStream to succeed deflating the data from the code above without issues.

Actual behavior

ZlibStream.Finish() throws System.InvalidOperationException.

Version used

  • Branch main
  • Commit d1fa75f.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions