From b41611ae6d43ff2e534ed0148169cdb13d6fd9e7 Mon Sep 17 00:00:00 2001 From: Stefan Nikolei Date: Tue, 24 Jan 2023 08:00:38 +0100 Subject: [PATCH] Remove nullable disable from zlib #2231 --- .../Compression/Zlib/DeflateThrowHelper.cs | 10 +++++++++ src/ImageSharp/Compression/Zlib/Deflater.cs | 2 -- .../Compression/Zlib/DeflaterEngine.cs | 12 ++++------ .../Compression/Zlib/DeflaterHuffman.cs | 9 -------- .../Compression/Zlib/DeflaterOutputStream.cs | 3 --- .../Compression/Zlib/DeflaterPendingBuffer.cs | 2 -- .../Compression/Zlib/ZlibDeflateStream.cs | 3 --- .../Compression/Zlib/ZlibInflateStream.cs | 6 +++-- .../Diagnostics/MemoryDiagnostics.cs | 7 +++--- .../Decompressors/DeflateTiffCompression.cs | 22 ++++++++++--------- 10 files changed, 33 insertions(+), 43 deletions(-) diff --git a/src/ImageSharp/Compression/Zlib/DeflateThrowHelper.cs b/src/ImageSharp/Compression/Zlib/DeflateThrowHelper.cs index ba22d5d85..30761328f 100644 --- a/src/ImageSharp/Compression/Zlib/DeflateThrowHelper.cs +++ b/src/ImageSharp/Compression/Zlib/DeflateThrowHelper.cs @@ -1,23 +1,33 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using System.Diagnostics.CodeAnalysis; + namespace SixLabors.ImageSharp.Compression.Zlib; internal static class DeflateThrowHelper { + [DoesNotReturn] public static void ThrowAlreadyFinished() => throw new InvalidOperationException("Finish() already called."); + [DoesNotReturn] public static void ThrowAlreadyClosed() => throw new InvalidOperationException("Deflator already closed."); + [DoesNotReturn] public static void ThrowUnknownCompression() => throw new InvalidOperationException("Unknown compression function."); + [DoesNotReturn] public static void ThrowNotProcessed() => throw new InvalidOperationException("Old input was not completely processed."); + [DoesNotReturn] public static void ThrowNull(string name) => throw new ArgumentNullException(name); + [DoesNotReturn] public static void ThrowOutOfRange(string name) => throw new ArgumentOutOfRangeException(name); + [DoesNotReturn] public static void ThrowHeapViolated() => throw new InvalidOperationException("Huffman heap invariant violated."); + [DoesNotReturn] public static void ThrowNoDeflate() => throw new ImageFormatException("Cannot deflate all input."); } diff --git a/src/ImageSharp/Compression/Zlib/Deflater.cs b/src/ImageSharp/Compression/Zlib/Deflater.cs index f001b8a67..f642ec85a 100644 --- a/src/ImageSharp/Compression/Zlib/Deflater.cs +++ b/src/ImageSharp/Compression/Zlib/Deflater.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Runtime.CompilerServices; using SixLabors.ImageSharp.Memory; @@ -285,7 +284,6 @@ internal sealed class Deflater : IDisposable if (!this.isDisposed) { this.engine.Dispose(); - this.engine = null; this.isDisposed = true; } } diff --git a/src/ImageSharp/Compression/Zlib/DeflaterEngine.cs b/src/ImageSharp/Compression/Zlib/DeflaterEngine.cs index 47bc43f52..31fa0238b 100644 --- a/src/ImageSharp/Compression/Zlib/DeflaterEngine.cs +++ b/src/ImageSharp/Compression/Zlib/DeflaterEngine.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Buffers; using System.Runtime.CompilerServices; @@ -87,7 +86,7 @@ internal sealed unsafe class DeflaterEngine : IDisposable /// /// The input data for compression. /// - private byte[] inputBuf; + private byte[]? inputBuf; /// /// The offset into inputBuf, where input data starts. @@ -222,7 +221,7 @@ internal sealed unsafe class DeflaterEngine : IDisposable /// The buffer containing input data. /// The offset of the first byte of data. /// The number of bytes of data to use as input. - public void SetInput(byte[] buffer, int offset, int count) + public void SetInput(byte[]? buffer, int offset, int count) { if (buffer is null) { @@ -362,6 +361,8 @@ internal sealed unsafe class DeflaterEngine : IDisposable more = this.inputEnd - this.inputOff; } + ArgumentNullException.ThrowIfNull(this.inputBuf); + Unsafe.CopyBlockUnaligned( ref this.window.Span[this.strstart + this.lookahead], ref this.inputBuf[this.inputOff], @@ -393,11 +394,6 @@ internal sealed unsafe class DeflaterEngine : IDisposable this.prevMemoryHandle.Dispose(); this.prevMemoryOwner.Dispose(); - this.windowMemoryOwner = null; - this.headMemoryOwner = null; - this.prevMemoryOwner = null; - this.huffman = null; - this.isDisposed = true; } } diff --git a/src/ImageSharp/Compression/Zlib/DeflaterHuffman.cs b/src/ImageSharp/Compression/Zlib/DeflaterHuffman.cs index 8c6769908..dc11de425 100644 --- a/src/ImageSharp/Compression/Zlib/DeflaterHuffman.cs +++ b/src/ImageSharp/Compression/Zlib/DeflaterHuffman.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Buffers; using System.Runtime.CompilerServices; @@ -427,10 +426,6 @@ internal sealed unsafe class DeflaterHuffman : IDisposable this.blTree.Dispose(); this.distTree.Dispose(); - this.Pending = null; - this.literalTree = null; - this.blTree = null; - this.distTree = null; this.isDisposed = true; } } @@ -977,10 +972,6 @@ internal sealed unsafe class DeflaterHuffman : IDisposable this.codesMemoryHandle.Dispose(); this.codesMemoryOwner.Dispose(); - this.frequenciesMemoryOwner = null; - this.lengthsMemoryOwner = null; - this.codesMemoryOwner = null; - this.isDisposed = true; } } diff --git a/src/ImageSharp/Compression/Zlib/DeflaterOutputStream.cs b/src/ImageSharp/Compression/Zlib/DeflaterOutputStream.cs index cea2fd272..de818fd8f 100644 --- a/src/ImageSharp/Compression/Zlib/DeflaterOutputStream.cs +++ b/src/ImageSharp/Compression/Zlib/DeflaterOutputStream.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Buffers; using SixLabors.ImageSharp.Memory; @@ -137,8 +136,6 @@ internal sealed class DeflaterOutputStream : Stream this.memoryOwner.Dispose(); } - this.deflater = null; - this.memoryOwner = null; this.isDisposed = true; base.Dispose(disposing); } diff --git a/src/ImageSharp/Compression/Zlib/DeflaterPendingBuffer.cs b/src/ImageSharp/Compression/Zlib/DeflaterPendingBuffer.cs index 28febdc11..37e7404e4 100644 --- a/src/ImageSharp/Compression/Zlib/DeflaterPendingBuffer.cs +++ b/src/ImageSharp/Compression/Zlib/DeflaterPendingBuffer.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Buffers; using System.Runtime.CompilerServices; @@ -180,7 +179,6 @@ internal sealed unsafe class DeflaterPendingBuffer : IDisposable { this.bufferMemoryHandle.Dispose(); this.bufferMemoryOwner.Dispose(); - this.bufferMemoryOwner = null; this.isDisposed = true; } } diff --git a/src/ImageSharp/Compression/Zlib/ZlibDeflateStream.cs b/src/ImageSharp/Compression/Zlib/ZlibDeflateStream.cs index 00c4aed3b..2e52f84d7 100644 --- a/src/ImageSharp/Compression/Zlib/ZlibDeflateStream.cs +++ b/src/ImageSharp/Compression/Zlib/ZlibDeflateStream.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Runtime.CompilerServices; using SixLabors.ImageSharp.Formats.Png; @@ -172,8 +171,6 @@ internal sealed class ZlibDeflateStream : Stream this.rawStream.WriteByte((byte)(crc & 0xFF)); } - this.deflateStream = null; - base.Dispose(disposing); this.isDisposed = true; } diff --git a/src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs b/src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs index f9e678605..06a7c3928 100644 --- a/src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs +++ b/src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs @@ -1,7 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable +using System.Diagnostics.CodeAnalysis; using System.IO.Compression; using SixLabors.ImageSharp.IO; @@ -90,7 +90,7 @@ internal sealed class ZlibInflateStream : Stream /// /// Gets the compressed stream over the deframed inner stream. /// - public DeflateStream CompressedStream { get; private set; } + public DeflateStream? CompressedStream { get; private set; } /// /// Adds new bytes from a frame found in the original stream. @@ -98,6 +98,7 @@ internal sealed class ZlibInflateStream : Stream /// The current remaining data according to the chunk length. /// Whether the chunk to be inflated is a critical chunk. /// The . + [MemberNotNullWhen(true, nameof(CompressedStream))] public bool AllocateNewBytes(int bytes, bool isCriticalChunk) { this.currentDataRemaining = bytes; @@ -210,6 +211,7 @@ internal sealed class ZlibInflateStream : Stream this.isDisposed = true; } + [MemberNotNullWhen(true, nameof(CompressedStream))] private bool InitializeInflateStream(bool isCriticalChunk) { // Read the zlib header : http://tools.ietf.org/html/rfc1950 diff --git a/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs b/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs index f00ee1b2a..8327daf23 100644 --- a/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs +++ b/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable namespace SixLabors.ImageSharp.Diagnostics; @@ -17,7 +16,7 @@ public static class MemoryDiagnostics { private static int totalUndisposedAllocationCount; - private static UndisposedAllocationDelegate undisposedAllocation; + private static UndisposedAllocationDelegate? undisposedAllocation; private static int undisposedAllocationSubscriptionCounter; private static readonly object SyncRoot = new(); @@ -50,12 +49,12 @@ public static class MemoryDiagnostics /// /// Fires when ImageSharp allocates memory from a MemoryAllocator /// - internal static event Action MemoryAllocated; + internal static event Action? MemoryAllocated; /// /// Fires when ImageSharp releases memory allocated from a MemoryAllocator /// - internal static event Action MemoryReleased; + internal static event Action? MemoryReleased; /// /// Gets a value indicating the total number of memory resource objects leaked to the finalizer. diff --git a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/DeflateTiffCompression.cs b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/DeflateTiffCompression.cs index 4c377c878..27c311009 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/DeflateTiffCompression.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/DeflateTiffCompression.cs @@ -50,19 +50,21 @@ internal sealed class DeflateTiffCompression : TiffBaseDecompressor return left > 0 ? left : 0; })) { - deframeStream.AllocateNewBytes(byteCount, true); - DeflateStream dataStream = deframeStream.CompressedStream; - - int totalRead = 0; - while (totalRead < buffer.Length) + if (deframeStream.AllocateNewBytes(byteCount, true)) { - int bytesRead = dataStream.Read(buffer, totalRead, buffer.Length - totalRead); - if (bytesRead <= 0) + DeflateStream? dataStream = deframeStream.CompressedStream; + + int totalRead = 0; + while (totalRead < buffer.Length) { - break; - } + int bytesRead = dataStream.Read(buffer, totalRead, buffer.Length - totalRead); + if (bytesRead <= 0) + { + break; + } - totalRead += bytesRead; + totalRead += bytesRead; + } } }