From 89cc2e9fcd50dab2f82ff9452aa9eac4bc57da06 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 14 Nov 2019 00:59:16 +1100 Subject: [PATCH] Remove a few more bounds checks. --- src/ImageSharp/Formats/Png/Zlib/DeflaterHuffman.cs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/ImageSharp/Formats/Png/Zlib/DeflaterHuffman.cs b/src/ImageSharp/Formats/Png/Zlib/DeflaterHuffman.cs index 36cf0d559..b2eddc37b 100644 --- a/src/ImageSharp/Formats/Png/Zlib/DeflaterHuffman.cs +++ b/src/ImageSharp/Formats/Png/Zlib/DeflaterHuffman.cs @@ -4,17 +4,13 @@ using System; using System.Buffers; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using SixLabors.Memory; namespace SixLabors.ImageSharp.Formats.Png.Zlib { /// - /// This is the DeflaterHuffman class. - /// - /// This class is not thread safe. This is inherent in the API, due - /// to the split of Deflate and SetInput. - /// - /// author of the original java version : Jochen Hoenicke + /// Performs Deflate Huffman encoding. /// public sealed unsafe class DeflaterHuffman : IDisposable { @@ -264,14 +260,16 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib + this.extraBits; int static_len = this.extraBits; + ref byte staticLLengthRef = ref MemoryMarshal.GetReference(StaticLLength); for (int i = 0; i < LiteralNumber; i++) { - static_len += this.literalTree.Freqs[i] * StaticLLength[i]; + static_len += this.literalTree.Freqs[i] * Unsafe.Add(ref staticLLengthRef, i); } + ref byte staticDLengthRef = ref MemoryMarshal.GetReference(StaticDLength); for (int i = 0; i < DistanceNumber; i++) { - static_len += this.distTree.Freqs[i] * StaticDLength[i]; + static_len += this.distTree.Freqs[i] * Unsafe.Add(ref staticDLengthRef, i); } if (opt_len >= static_len)