From d3936e2d3ff5342df8723244bc89479048a598e3 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 27 Mar 2022 22:47:44 +1100 Subject: [PATCH 01/34] Add check for App1 XMP marker length --- src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index ef4e3ffac2..51e0675754 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -722,7 +722,14 @@ namespace SixLabors.ImageSharp.Formats.Jpeg if (ProfileResolver.IsProfile(this.temp, ProfileResolver.XmpMarker.Slice(0, ExifMarkerLength))) { - int remainingXmpMarkerBytes = XmpMarkerLength - ExifMarkerLength; + const int remainingXmpMarkerBytes = XmpMarkerLength - ExifMarkerLength; + if (remaining < remainingXmpMarkerBytes || this.IgnoreMetadata) + { + // Skip the application header length. + stream.Skip(remaining); + return; + } + stream.Read(this.temp, ExifMarkerLength, remainingXmpMarkerBytes); remaining -= remainingXmpMarkerBytes; if (ProfileResolver.IsProfile(this.temp, ProfileResolver.XmpMarker)) From 96f6fd45a316e33b78026c8c40868928643775ef Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 27 Mar 2022 14:17:32 +0200 Subject: [PATCH 02/34] Add check, if enough data is read in LoadTables --- .../Formats/Jpeg/JpegDecoderCore.cs | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 51e0675754..41effc865d 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -232,7 +232,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg using var stream = new BufferedReadStream(this.Configuration, ms); // Check for the Start Of Image marker. - stream.Read(this.markerBuffer, 0, 2); + int bytesRead = stream.Read(this.markerBuffer, 0, 2); + if (bytesRead != 2) + { + JpegThrowHelper.ThrowInvalidImageContentException("Not enough data to read the SOI marker"); + } + var fileMarker = new JpegFileMarker(this.markerBuffer[1], 0); if (fileMarker.Marker != JpegConstants.Markers.SOI) { @@ -240,7 +245,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg } // Read next marker. - stream.Read(this.markerBuffer, 0, 2); + bytesRead = stream.Read(this.markerBuffer, 0, 2); + if (bytesRead != 2) + { + JpegThrowHelper.ThrowInvalidImageContentException("Not enough data to read marker"); + } + byte marker = this.markerBuffer[1]; fileMarker = new JpegFileMarker(marker, (int)stream.Position - 2); @@ -273,7 +283,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg } // Read next marker. - stream.Read(this.markerBuffer, 0, 2); + bytesRead = stream.Read(this.markerBuffer, 0, 2); + if (bytesRead != 2) + { + JpegThrowHelper.ThrowInvalidImageContentException("Not enough data to read marker"); + } + fileMarker = new JpegFileMarker(this.markerBuffer[1], 0); } } From 588a6d444a2aec33e4d93b65d2def63cd8a7ba2c Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 27 Mar 2022 15:18:59 +0200 Subject: [PATCH 03/34] Fix lossy image with not enough exif bytes --- tests/Images/Input/Webp/exif_lossy.webp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Images/Input/Webp/exif_lossy.webp b/tests/Images/Input/Webp/exif_lossy.webp index 35e454b96f..5d6db3800f 100644 --- a/tests/Images/Input/Webp/exif_lossy.webp +++ b/tests/Images/Input/Webp/exif_lossy.webp @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:fdf4e9b20af4168f4177d33f7f502906343bbaaae2af9b90e1531bd4452b317b -size 40765 +oid sha256:5c53967bfefcfece8cd4411740c1c394e75864ca61a7a9751df3b28e727c0205 +size 68646 From f820b0b965716de398974a23a02ac95616580522 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 27 Mar 2022 15:21:30 +0200 Subject: [PATCH 04/34] ParseOptional chunks now checks, if enough data is read --- .../Formats/Webp/WebpDecoderCore.cs | 29 ++++++++++++++++--- .../Formats/Webp/WebpThrowHelper.cs | 7 +++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs index 9d18e5d821..412ec2760a 100644 --- a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs +++ b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs @@ -426,6 +426,7 @@ namespace SixLabors.ImageSharp.Formats.Webp /// The webp image features. private void ParseOptionalExtendedChunks(WebpChunkType chunkType, WebpFeatures features) { + int bytesRead; switch (chunkType) { case WebpChunkType.Iccp: @@ -437,7 +438,12 @@ namespace SixLabors.ImageSharp.Formats.Webp else { byte[] iccpData = new byte[iccpChunkSize]; - this.currentStream.Read(iccpData, 0, (int)iccpChunkSize); + bytesRead = this.currentStream.Read(iccpData, 0, (int)iccpChunkSize); + if (bytesRead != iccpChunkSize) + { + WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the iccp chunk"); + } + var profile = new IccProfile(iccpData); if (profile.CheckIsValid()) { @@ -456,7 +462,12 @@ namespace SixLabors.ImageSharp.Formats.Webp else { byte[] exifData = new byte[exifChunkSize]; - this.currentStream.Read(exifData, 0, (int)exifChunkSize); + bytesRead = this.currentStream.Read(exifData, 0, (int)exifChunkSize); + if (bytesRead != exifChunkSize) + { + WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the exif chunk"); + } + var profile = new ExifProfile(exifData); this.Metadata.ExifProfile = profile; } @@ -472,7 +483,12 @@ namespace SixLabors.ImageSharp.Formats.Webp else { byte[] xmpData = new byte[xmpChunkSize]; - this.currentStream.Read(xmpData, 0, (int)xmpChunkSize); + bytesRead = this.currentStream.Read(xmpData, 0, (int)xmpChunkSize); + if (bytesRead != xmpChunkSize) + { + WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the xmp chunk"); + } + var profile = new XmpProfile(xmpData); this.Metadata.XmpProfile = profile; } @@ -488,7 +504,12 @@ namespace SixLabors.ImageSharp.Formats.Webp features.AlphaChunkHeader = (byte)this.currentStream.ReadByte(); int alphaDataSize = (int)(alphaChunkSize - 1); features.AlphaData = this.memoryAllocator.Allocate(alphaDataSize); - this.currentStream.Read(features.AlphaData.Memory.Span, 0, alphaDataSize); + bytesRead = this.currentStream.Read(features.AlphaData.Memory.Span, 0, alphaDataSize); + if (bytesRead != alphaDataSize) + { + WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the alpha chunk"); + } + break; default: WebpThrowHelper.ThrowImageFormatException("Unexpected chunk followed VP8X header"); diff --git a/src/ImageSharp/Formats/Webp/WebpThrowHelper.cs b/src/ImageSharp/Formats/Webp/WebpThrowHelper.cs index fffdd34101..5f063fd11a 100644 --- a/src/ImageSharp/Formats/Webp/WebpThrowHelper.cs +++ b/src/ImageSharp/Formats/Webp/WebpThrowHelper.cs @@ -8,6 +8,13 @@ namespace SixLabors.ImageSharp.Formats.Webp { internal static class WebpThrowHelper { + /// + /// Cold path optimization for throwing 's. + /// + /// The error message for the exception. + [MethodImpl(InliningOptions.ColdPath)] + public static void ThrowInvalidImageContentException(string errorMessage) => throw new InvalidImageContentException(errorMessage); + /// /// Cold path optimization for throwing -s /// From 853b222af50851a9d46f73df0042ecc88b467bba Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 27 Mar 2022 15:30:15 +0200 Subject: [PATCH 05/34] Add test for lossy with exif which does not contain enough data --- .../Formats/WebP/WebpMetaDataTests.cs | 12 ++++++++++++ tests/ImageSharp.Tests/TestImages.cs | 1 + .../Input/Webp/exif_lossy_not_enough_data.webp | 3 +++ 3 files changed, 16 insertions(+) create mode 100644 tests/Images/Input/Webp/exif_lossy_not_enough_data.webp diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs index eaa7fb5646..499b0579b6 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs @@ -150,5 +150,17 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp Assert.NotNull(actualExif); Assert.Equal(expectedExif.Values.Count, actualExif.Values.Count); } + + [Theory] + [WithFile(TestImages.Webp.Lossy.WithExifNotEnoughData, PixelTypes.Rgba32)] + public void WebpDecoder_ThrowInvalidImageContentException_OnWithInvalidExifData(TestImageProvider provider) + where TPixel : unmanaged, IPixel => + Assert.Throws( + () => + { + using (provider.GetImage(WebpDecoder)) + { + } + }); } } diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 07aff6fc12..ac060fd99f 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -635,6 +635,7 @@ namespace SixLabors.ImageSharp.Tests { public const string Earth = "Webp/earth_lossy.webp"; public const string WithExif = "Webp/exif_lossy.webp"; + public const string WithExifNotEnoughData = "Webp/exif_lossy_not_enough_data.webp"; public const string WithIccp = "Webp/lossy_with_iccp.webp"; public const string WithXmp = "Webp/xmp_lossy.webp"; public const string BikeSmall = "Webp/bike_lossless_small.webp"; diff --git a/tests/Images/Input/Webp/exif_lossy_not_enough_data.webp b/tests/Images/Input/Webp/exif_lossy_not_enough_data.webp new file mode 100644 index 0000000000..35e454b96f --- /dev/null +++ b/tests/Images/Input/Webp/exif_lossy_not_enough_data.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fdf4e9b20af4168f4177d33f7f502906343bbaaae2af9b90e1531bd4452b317b +size 40765 From 5d04734c324a0930ff43f90b9f236a5e8d39e247 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 27 Mar 2022 18:59:04 +0200 Subject: [PATCH 06/34] Add checks in ReadVp8Header() to make sure enough data is read --- .../Formats/Webp/WebpDecoderCore.cs | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs index 412ec2760a..9d83591c36 100644 --- a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs +++ b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs @@ -272,7 +272,12 @@ namespace SixLabors.ImageSharp.Formats.Webp this.webpMetadata.FileFormat = WebpFileFormatType.Lossy; // VP8 data size (not including this 4 bytes). - this.currentStream.Read(this.buffer, 0, 4); + int bytesRead = this.currentStream.Read(this.buffer, 0, 4); + if (bytesRead != 4) + { + WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the VP8 data size"); + } + uint dataSize = BinaryPrimitives.ReadUInt32LittleEndian(this.buffer); // remaining counts the available image data payload. @@ -284,7 +289,12 @@ namespace SixLabors.ImageSharp.Formats.Webp // - A 3-bit version number. // - A 1-bit show_frame flag. // - A 19-bit field containing the size of the first data partition in bytes. - this.currentStream.Read(this.buffer, 0, 3); + bytesRead = this.currentStream.Read(this.buffer, 0, 3); + if (bytesRead != 3) + { + WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the VP8 frame tag"); + } + uint frameTag = (uint)(this.buffer[0] | (this.buffer[1] << 8) | (this.buffer[2] << 16)); remaining -= 3; bool isNoKeyFrame = (frameTag & 0x1) == 1; @@ -312,13 +322,23 @@ namespace SixLabors.ImageSharp.Formats.Webp } // Check for VP8 magic bytes. - this.currentStream.Read(this.buffer, 0, 3); + bytesRead = this.currentStream.Read(this.buffer, 0, 3); + if (bytesRead != 3) + { + WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the VP8 magic bytes"); + } + if (!this.buffer.AsSpan(0, 3).SequenceEqual(WebpConstants.Vp8HeaderMagicBytes)) { WebpThrowHelper.ThrowImageFormatException("VP8 magic bytes not found"); } - this.currentStream.Read(this.buffer, 0, 4); + bytesRead = this.currentStream.Read(this.buffer, 0, 4); + if (bytesRead != 4) + { + WebpThrowHelper.ThrowInvalidImageContentException("VP8 header does not contain enough data to read the image width and height"); + } + uint tmp = (uint)BinaryPrimitives.ReadInt16LittleEndian(this.buffer); uint width = tmp & 0x3fff; sbyte xScale = (sbyte)(tmp >> 6); From 8cc5a6b6cad3bccd6b7a04855428b8f78ce0a517 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 27 Mar 2022 19:14:42 +0200 Subject: [PATCH 07/34] Remove code duplication when reading the profile data --- .../Formats/Webp/WebpDecoderCore.cs | 145 ++++++++++-------- 1 file changed, 85 insertions(+), 60 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs index 9d83591c36..2a840ba709 100644 --- a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs +++ b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs @@ -450,68 +450,17 @@ namespace SixLabors.ImageSharp.Formats.Webp switch (chunkType) { case WebpChunkType.Iccp: - uint iccpChunkSize = this.ReadChunkSize(); - if (this.IgnoreMetadata) - { - this.currentStream.Skip((int)iccpChunkSize); - } - else - { - byte[] iccpData = new byte[iccpChunkSize]; - bytesRead = this.currentStream.Read(iccpData, 0, (int)iccpChunkSize); - if (bytesRead != iccpChunkSize) - { - WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the iccp chunk"); - } - - var profile = new IccProfile(iccpData); - if (profile.CheckIsValid()) - { - this.Metadata.IccProfile = profile; - } - } + this.ReadIccProfile(); break; case WebpChunkType.Exif: - uint exifChunkSize = this.ReadChunkSize(); - if (this.IgnoreMetadata) - { - this.currentStream.Skip((int)exifChunkSize); - } - else - { - byte[] exifData = new byte[exifChunkSize]; - bytesRead = this.currentStream.Read(exifData, 0, (int)exifChunkSize); - if (bytesRead != exifChunkSize) - { - WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the exif chunk"); - } - - var profile = new ExifProfile(exifData); - this.Metadata.ExifProfile = profile; - } + this.ReadExifProfile(); break; case WebpChunkType.Xmp: - uint xmpChunkSize = this.ReadChunkSize(); - if (this.IgnoreMetadata) - { - this.currentStream.Skip((int)xmpChunkSize); - } - else - { - byte[] xmpData = new byte[xmpChunkSize]; - bytesRead = this.currentStream.Read(xmpData, 0, (int)xmpChunkSize); - if (bytesRead != xmpChunkSize) - { - WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the xmp chunk"); - } - - var profile = new XmpProfile(xmpData); - this.Metadata.XmpProfile = profile; - } + this.ReadXmpProfile(); break; @@ -555,22 +504,98 @@ namespace SixLabors.ImageSharp.Formats.Webp { // Read chunk header. WebpChunkType chunkType = this.ReadChunkType(); - uint chunkLength = this.ReadChunkSize(); - if (chunkType == WebpChunkType.Exif && this.Metadata.ExifProfile == null) { - byte[] exifData = new byte[chunkLength]; - this.currentStream.Read(exifData, 0, (int)chunkLength); - this.Metadata.ExifProfile = new ExifProfile(exifData); + this.ReadExifProfile(); + } + else if (chunkType == WebpChunkType.Xmp && this.Metadata.XmpProfile == null) + { + this.ReadXmpProfile(); } else { - // Skip XMP chunk data or any duplicate EXIF chunk. + // Skip duplicate XMP or EXIF chunk. + uint chunkLength = this.ReadChunkSize(); this.currentStream.Skip((int)chunkLength); } } } + /// + /// Reads the EXIF profile from the stream. + /// + private void ReadExifProfile() + { + uint exifChunkSize = this.ReadChunkSize(); + if (this.IgnoreMetadata) + { + this.currentStream.Skip((int)exifChunkSize); + } + else + { + byte[] exifData = new byte[exifChunkSize]; + int bytesRead = this.currentStream.Read(exifData, 0, (int)exifChunkSize); + if (bytesRead != exifChunkSize) + { + WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the exif chunk"); + } + + var profile = new ExifProfile(exifData); + this.Metadata.ExifProfile = profile; + } + } + + /// + /// Reads the XMP profile the stream. + /// + private void ReadXmpProfile() + { + uint xmpChunkSize = this.ReadChunkSize(); + if (this.IgnoreMetadata) + { + this.currentStream.Skip((int)xmpChunkSize); + } + else + { + byte[] xmpData = new byte[xmpChunkSize]; + int bytesRead = this.currentStream.Read(xmpData, 0, (int)xmpChunkSize); + if (bytesRead != xmpChunkSize) + { + WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the xmp chunk"); + } + + var profile = new XmpProfile(xmpData); + this.Metadata.XmpProfile = profile; + } + } + + /// + /// Reads the ICCP chunk from the stream. + /// + private void ReadIccProfile() + { + uint iccpChunkSize = this.ReadChunkSize(); + if (this.IgnoreMetadata) + { + this.currentStream.Skip((int)iccpChunkSize); + } + else + { + byte[] iccpData = new byte[iccpChunkSize]; + int bytesRead = this.currentStream.Read(iccpData, 0, (int)iccpChunkSize); + if (bytesRead != iccpChunkSize) + { + WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the iccp chunk"); + } + + var profile = new IccProfile(iccpData); + if (profile.CheckIsValid()) + { + this.Metadata.IccProfile = profile; + } + } + } + /// /// Identifies the chunk type from the chunk. /// From 8e4129a7335ebb2a1a1f977654eeb7db6389cd1c Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 27 Mar 2022 19:20:14 +0200 Subject: [PATCH 08/34] Make sure enough data is read in ReadVp8XHeader() --- .../Formats/Webp/WebpDecoderCore.cs | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs index 2a840ba709..91594a6740 100644 --- a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs +++ b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs @@ -190,7 +190,11 @@ namespace SixLabors.ImageSharp.Formats.Webp uint fileSize = this.ReadChunkSize(); // The first byte contains information about the image features used. - byte imageFeatures = (byte)this.currentStream.ReadByte(); + int imageFeatures = this.currentStream.ReadByte(); + if (imageFeatures == -1) + { + WebpThrowHelper.ThrowInvalidImageContentException("VP8X header doe not contain enough data"); + } // The first two bit of it are reserved and should be 0. if (imageFeatures >> 6 != 0) @@ -214,19 +218,34 @@ namespace SixLabors.ImageSharp.Formats.Webp features.Animation = (imageFeatures & (1 << 1)) != 0; // 3 reserved bytes should follow which are supposed to be zero. - this.currentStream.Read(this.buffer, 0, 3); + int bytesRead = this.currentStream.Read(this.buffer, 0, 3); + if (bytesRead != 3) + { + WebpThrowHelper.ThrowInvalidImageContentException("VP8X header does not contain enough data"); + } + if (this.buffer[0] != 0 || this.buffer[1] != 0 || this.buffer[2] != 0) { WebpThrowHelper.ThrowImageFormatException("reserved bytes should be zero"); } // 3 bytes for the width. - this.currentStream.Read(this.buffer, 0, 3); + bytesRead = this.currentStream.Read(this.buffer, 0, 3); + if (bytesRead != 3) + { + WebpThrowHelper.ThrowInvalidImageContentException("VP8 header does not contain enough data to read the width"); + } + this.buffer[3] = 0; uint width = (uint)BinaryPrimitives.ReadInt32LittleEndian(this.buffer) + 1; // 3 bytes for the height. - this.currentStream.Read(this.buffer, 0, 3); + bytesRead = this.currentStream.Read(this.buffer, 0, 3); + if (bytesRead != 3) + { + WebpThrowHelper.ThrowInvalidImageContentException("VP8 header does not contain enough data to read the height"); + } + this.buffer[3] = 0; uint height = (uint)BinaryPrimitives.ReadInt32LittleEndian(this.buffer) + 1; From aac680fea60968320220c09492d8aa6216910295 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 27 Mar 2022 19:28:03 +0200 Subject: [PATCH 09/34] Add check, if enough data was read for progressive scan decoding data --- src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 41effc865d..56bdca5f8d 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -1334,8 +1334,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg component.ACHuffmanTableId = acTableIndex; } - // 3 bytes: Progressive scan decoding data - stream.Read(this.temp, 0, 3); + // 3 bytes: Progressive scan decoding data. + int bytesRead = stream.Read(this.temp, 0, 3); + if (bytesRead != 3) + { + JpegThrowHelper.ThrowInvalidImageContentException("Not enough data to read progressive scan decoding data"); + } int spectralStart = this.temp[0]; this.scanDecoder.SpectralStart = spectralStart; From e6ad467503ed31af90d7a55920a021e0c8c723ef Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 27 Mar 2022 19:29:12 +0200 Subject: [PATCH 10/34] Add check in ReadUint16 for enough data --- src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 56bdca5f8d..e2d6cfcd29 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -1362,7 +1362,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg [MethodImpl(InliningOptions.ShortMethod)] private ushort ReadUint16(BufferedReadStream stream) { - stream.Read(this.markerBuffer, 0, 2); + int bytesRead = stream.Read(this.markerBuffer, 0, 2); + if (bytesRead != 2) + { + JpegThrowHelper.ThrowInvalidImageContentException("jpeg stream does not contain enough data, could not read ushort."); + } + return BinaryPrimitives.ReadUInt16BigEndian(this.markerBuffer); } } From d22e50c0d0b01b75ca02396d745617f7ad81d256 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Mon, 28 Mar 2022 12:22:58 +0200 Subject: [PATCH 11/34] Ignore invalid EXIF or XMP chunks --- .../Formats/Webp/WebpDecoderCore.cs | 12 +++++------- .../Formats/WebP/WebpMetaDataTests.cs | 19 ++++++++++--------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs index 91594a6740..9e709236c3 100644 --- a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs +++ b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs @@ -465,22 +465,18 @@ namespace SixLabors.ImageSharp.Formats.Webp /// The webp image features. private void ParseOptionalExtendedChunks(WebpChunkType chunkType, WebpFeatures features) { - int bytesRead; switch (chunkType) { case WebpChunkType.Iccp: this.ReadIccProfile(); - break; case WebpChunkType.Exif: this.ReadExifProfile(); - break; case WebpChunkType.Xmp: this.ReadXmpProfile(); - break; case WebpChunkType.Animation: @@ -492,7 +488,7 @@ namespace SixLabors.ImageSharp.Formats.Webp features.AlphaChunkHeader = (byte)this.currentStream.ReadByte(); int alphaDataSize = (int)(alphaChunkSize - 1); features.AlphaData = this.memoryAllocator.Allocate(alphaDataSize); - bytesRead = this.currentStream.Read(features.AlphaData.Memory.Span, 0, alphaDataSize); + int bytesRead = this.currentStream.Read(features.AlphaData.Memory.Span, 0, alphaDataSize); if (bytesRead != alphaDataSize) { WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the alpha chunk"); @@ -556,7 +552,8 @@ namespace SixLabors.ImageSharp.Formats.Webp int bytesRead = this.currentStream.Read(exifData, 0, (int)exifChunkSize); if (bytesRead != exifChunkSize) { - WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the exif chunk"); + // Ignore invalid chunk. + return; } var profile = new ExifProfile(exifData); @@ -580,7 +577,8 @@ namespace SixLabors.ImageSharp.Formats.Webp int bytesRead = this.currentStream.Read(xmpData, 0, (int)xmpChunkSize); if (bytesRead != xmpChunkSize) { - WebpThrowHelper.ThrowInvalidImageContentException("Not enough data to read the xmp chunk"); + // Ignore invalid chunk. + return; } var profile = new XmpProfile(xmpData); diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs index 499b0579b6..456b9a3f52 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpMetaDataTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. +using System; using System.IO; using System.Threading.Tasks; using SixLabors.ImageSharp.Formats.Webp; @@ -153,14 +154,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Webp [Theory] [WithFile(TestImages.Webp.Lossy.WithExifNotEnoughData, PixelTypes.Rgba32)] - public void WebpDecoder_ThrowInvalidImageContentException_OnWithInvalidExifData(TestImageProvider provider) - where TPixel : unmanaged, IPixel => - Assert.Throws( - () => - { - using (provider.GetImage(WebpDecoder)) - { - } - }); + public void WebpDecoder_IgnoresInvalidExifChunk(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + Exception ex = Record.Exception(() => + { + using Image image = provider.GetImage(); + }); + Assert.Null(ex); + } } } From 77bb28711613023cea454820b8f0598aae8644ed Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sat, 9 Apr 2022 20:20:36 +0100 Subject: [PATCH 12/34] xunit helper to track undisposed memory --- .../Diagnostics/MemoryDiagnostics.cs | 110 +++++++++++++----- .../Formats/Png/PngDecoderTests.cs | 4 + .../Image/ImageTests.LoadPixelData.cs | 1 + .../Image/LargeImageIntegrationTests.cs | 2 + .../MemoryAllocatorValidator.cs | 43 +++++++ .../ImageProviders/FileProvider.cs | 8 +- ...idateDisposedMemoryAllocationsAttribute.cs | 36 ++++++ 7 files changed, 177 insertions(+), 27 deletions(-) create mode 100644 tests/ImageSharp.Tests/MemoryAllocatorValidator.cs create mode 100644 tests/ImageSharp.Tests/ValidateDisposedMemoryAllocationsAttribute.cs diff --git a/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs b/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs index d83e737f12..6af2544138 100644 --- a/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs +++ b/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs @@ -15,10 +15,8 @@ namespace SixLabors.ImageSharp.Diagnostics /// public static class MemoryDiagnostics { - private static int totalUndisposedAllocationCount; - - private static UndisposedAllocationDelegate undisposedAllocation; - private static int undisposedAllocationSubscriptionCounter; + internal static readonly InteralMemoryDiagnostics Default = new(); + private static AsyncLocal localInstance = null; private static readonly object SyncRoot = new(); /// @@ -28,56 +26,116 @@ namespace SixLabors.ImageSharp.Diagnostics /// public static event UndisposedAllocationDelegate UndisposedAllocation { - add + add => Current.UndisposedAllocation += value; + remove => Current.UndisposedAllocation -= value; + } + + internal static InteralMemoryDiagnostics Current + { + get { - lock (SyncRoot) + if (localInstance != null && localInstance.Value != null) { - undisposedAllocationSubscriptionCounter++; - undisposedAllocation += value; + return localInstance.Value; } + + return Default; } - remove + set { - lock (SyncRoot) + if (localInstance == null) { - undisposedAllocation -= value; - undisposedAllocationSubscriptionCounter--; + lock (SyncRoot) + { + localInstance ??= new AsyncLocal(); + } } + + localInstance.Value = value; } } /// /// Gets a value indicating the total number of memory resource objects leaked to the finalizer. /// - public static int TotalUndisposedAllocationCount => totalUndisposedAllocationCount; + public static int TotalUndisposedAllocationCount => Current.TotalUndisposedAllocationCount; - internal static bool UndisposedAllocationSubscribed => Volatile.Read(ref undisposedAllocationSubscriptionCounter) > 0; + internal static bool UndisposedAllocationSubscribed => Current.UndisposedAllocationSubscribed; - internal static void IncrementTotalUndisposedAllocationCount() => - Interlocked.Increment(ref totalUndisposedAllocationCount); + internal static void IncrementTotalUndisposedAllocationCount() => Current.IncrementTotalUndisposedAllocationCount(); - internal static void DecrementTotalUndisposedAllocationCount() => - Interlocked.Decrement(ref totalUndisposedAllocationCount); + internal static void DecrementTotalUndisposedAllocationCount() => Current.DecrementTotalUndisposedAllocationCount(); internal static void RaiseUndisposedMemoryResource(string allocationStackTrace) + => Current.RaiseUndisposedMemoryResource(allocationStackTrace); + + internal class InteralMemoryDiagnostics { - if (undisposedAllocation is null) + private int totalUndisposedAllocationCount; + + private UndisposedAllocationDelegate undisposedAllocation; + private int undisposedAllocationSubscriptionCounter; + private readonly object syncRoot = new(); + + /// + /// Fires when an ImageSharp object's undisposed memory resource leaks to the finalizer. + /// The event brings significant overhead, and is intended to be used for troubleshooting only. + /// For production diagnostics, use . + /// + public event UndisposedAllocationDelegate UndisposedAllocation { - return; + add + { + lock (this.syncRoot) + { + this.undisposedAllocationSubscriptionCounter++; + this.undisposedAllocation += value; + } + } + + remove + { + lock (this.syncRoot) + { + this.undisposedAllocation -= value; + this.undisposedAllocationSubscriptionCounter--; + } + } } - // Schedule on the ThreadPool, to avoid user callback messing up the finalizer thread. + /// + /// Gets a value indicating the total number of memory resource objects leaked to the finalizer. + /// + public int TotalUndisposedAllocationCount => this.totalUndisposedAllocationCount; + + internal bool UndisposedAllocationSubscribed => Volatile.Read(ref this.undisposedAllocationSubscriptionCounter) > 0; + + internal void IncrementTotalUndisposedAllocationCount() => + Interlocked.Increment(ref this.totalUndisposedAllocationCount); + + internal void DecrementTotalUndisposedAllocationCount() => + Interlocked.Decrement(ref this.totalUndisposedAllocationCount); + + internal void RaiseUndisposedMemoryResource(string allocationStackTrace) + { + if (this.undisposedAllocation is null) + { + return; + } + + // Schedule on the ThreadPool, to avoid user callback messing up the finalizer thread. #if NETSTANDARD2_1 || NETCOREAPP2_1_OR_GREATER - ThreadPool.QueueUserWorkItem( - stackTrace => undisposedAllocation?.Invoke(stackTrace), - allocationStackTrace, - preferLocal: false); + ThreadPool.QueueUserWorkItem( + stackTrace => this.undisposedAllocation?.Invoke(stackTrace), + allocationStackTrace, + preferLocal: false); #else ThreadPool.QueueUserWorkItem( - stackTrace => undisposedAllocation?.Invoke((string)stackTrace), + stackTrace => this.undisposedAllocation?.Invoke((string)stackTrace), allocationStackTrace); #endif + } } } } diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index 752036126f..0a796b5143 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -103,10 +103,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png [Theory] [WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32)] + [ValidateDisposedMemoryAllocations] public void Decode(TestImageProvider provider) where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(PngDecoder); + //var testFile = TestFile.Create(provider.SourceFileOrDescription); + //using Image image = Image.Load(provider.Configuration, testFile.Bytes, PngDecoder); + image.DebugSave(provider); image.CompareToOriginal(provider, ImageComparer.Exact); } diff --git a/tests/ImageSharp.Tests/Image/ImageTests.LoadPixelData.cs b/tests/ImageSharp.Tests/Image/ImageTests.LoadPixelData.cs index a4044b906c..7683ee6889 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.LoadPixelData.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.LoadPixelData.cs @@ -14,6 +14,7 @@ namespace SixLabors.ImageSharp.Tests [Theory] [InlineData(false)] [InlineData(true)] + [ValidateDisposedMemoryAllocations] public void FromPixels(bool useSpan) { Rgba32[] data = { Color.Black, Color.White, Color.White, Color.Black, }; diff --git a/tests/ImageSharp.Tests/Image/LargeImageIntegrationTests.cs b/tests/ImageSharp.Tests/Image/LargeImageIntegrationTests.cs index 357d02be4b..ce1f902e59 100644 --- a/tests/ImageSharp.Tests/Image/LargeImageIntegrationTests.cs +++ b/tests/ImageSharp.Tests/Image/LargeImageIntegrationTests.cs @@ -55,6 +55,8 @@ namespace SixLabors.ImageSharp.Tests static void RunTest(string formatInner) { + using IDisposable mem = MemoryAllocatorValidator.MonitorAllocations(); + Configuration configuration = Configuration.Default.Clone(); configuration.PreferContiguousImageBuffers = true; IImageEncoder encoder = configuration.ImageFormatsManager.FindEncoder( diff --git a/tests/ImageSharp.Tests/MemoryAllocatorValidator.cs b/tests/ImageSharp.Tests/MemoryAllocatorValidator.cs new file mode 100644 index 0000000000..2c3d98eb13 --- /dev/null +++ b/tests/ImageSharp.Tests/MemoryAllocatorValidator.cs @@ -0,0 +1,43 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Diagnostics; +using SixLabors.ImageSharp.Diagnostics; +using Xunit; + +namespace SixLabors.ImageSharp.Tests +{ + public static class MemoryAllocatorValidator + { + public static IDisposable MonitorAllocations(int max = 0) + { + MemoryDiagnostics.Current = new(); + return new TestMemoryAllocatorDisposable(max); + } + + public static void ValidateAllocation(int max = 0) + { + var count = MemoryDiagnostics.TotalUndisposedAllocationCount; + var pass = count <= max; + Assert.True(pass, $"Expected a max of {max} undisposed buffers but found {count}"); + + if (count > 0) + { + Debug.WriteLine("We should have Zero undisposed memory allocations."); + } + + MemoryDiagnostics.Current = null; + } + + public struct TestMemoryAllocatorDisposable : IDisposable + { + private readonly int max; + + public TestMemoryAllocatorDisposable(int max) => this.max = max; + + public void Dispose() + => ValidateAllocation(this.max); + } + } +} diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs index e2e7d73bc6..3675356c38 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; using System.IO; using System.Reflection; using System.Threading.Tasks; +using SixLabors.ImageSharp.Diagnostics; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; @@ -158,8 +159,13 @@ namespace SixLabors.ImageSharp.Tests return this.LoadImage(decoder); } - var key = new Key(this.PixelType, this.FilePath, decoder); + // do cache so we can track allocation correctly when validating memory + if (MemoryDiagnostics.Current != MemoryDiagnostics.Default) + { + return this.LoadImage(decoder); + } + var key = new Key(this.PixelType, this.FilePath, decoder); Image cachedImage = Cache.GetOrAdd(key, _ => this.LoadImage(decoder)); return cachedImage.Clone(this.Configuration); diff --git a/tests/ImageSharp.Tests/ValidateDisposedMemoryAllocationsAttribute.cs b/tests/ImageSharp.Tests/ValidateDisposedMemoryAllocationsAttribute.cs new file mode 100644 index 0000000000..95d13b5954 --- /dev/null +++ b/tests/ImageSharp.Tests/ValidateDisposedMemoryAllocationsAttribute.cs @@ -0,0 +1,36 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.Diagnostics; +using System.Reflection; +using Xunit.Sdk; + +namespace SixLabors.ImageSharp.Tests +{ + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] + public class ValidateDisposedMemoryAllocationsAttribute : BeforeAfterTestAttribute + { + private readonly int max = 0; + + public ValidateDisposedMemoryAllocationsAttribute() + : this(0) + { + } + + public ValidateDisposedMemoryAllocationsAttribute(int max) + { + this.max = max; + if (max > 0) + { + Debug.WriteLine("Needs fixing, we shoudl have Zero undisposed memory allocations."); + } + } + + public override void Before(MethodInfo methodUnderTest) + => MemoryAllocatorValidator.MonitorAllocations(this.max); // the disposable isn't important cause the validate below does the same thing + + public override void After(MethodInfo methodUnderTest) + => MemoryAllocatorValidator.ValidateAllocation(this.max); + } +} From 3872c6000c74f29cb6069e40e2a3fe09fce3d3a0 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sat, 9 Apr 2022 20:21:37 +0100 Subject: [PATCH 13/34] revert loader change --- tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index 0a796b5143..eadbf138a6 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -108,9 +108,6 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png where TPixel : unmanaged, IPixel { using Image image = provider.GetImage(PngDecoder); - //var testFile = TestFile.Create(provider.SourceFileOrDescription); - //using Image image = Image.Load(provider.Configuration, testFile.Bytes, PngDecoder); - image.DebugSave(provider); image.CompareToOriginal(provider, ImageComparer.Exact); } From af7d84a376379afd9f7214ff5f34f12f5785ff2f Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sat, 9 Apr 2022 21:06:37 +0100 Subject: [PATCH 14/34] check disposed buffers for decoders --- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 9 ++- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 5 ++ .../Decompressors/JpegTiffCompression.cs | 6 +- .../Formats/Tiff/TiffDecoderCore.cs | 6 +- .../Formats/Webp/WebpDecoderCore.cs | 62 +++++++++++-------- .../Formats/Bmp/BmpDecoderTests.cs | 1 + .../Formats/Gif/GifDecoderTests.cs | 1 + .../Formats/Jpg/JpegDecoderTests.Metadata.cs | 15 +++-- .../Formats/Jpg/JpegDecoderTests.cs | 1 + .../Formats/Pbm/PbmDecoderTests.cs | 1 + .../Formats/Png/PngDecoderTests.cs | 2 +- .../Formats/Tga/TgaDecoderTests.cs | 1 + .../Formats/Tiff/TiffDecoderTests.cs | 1 + .../Formats/WebP/WebpDecoderTests.cs | 1 + .../ImageComparison/ImageComparingUtils.cs | 2 +- 15 files changed, 74 insertions(+), 40 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index 962c9f0d67..ee0a312803 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -122,11 +122,12 @@ namespace SixLabors.ImageSharp.Formats.Bmp public Image Decode(BufferedReadStream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { + Image image = null; try { int bytesPerColorMapEntry = this.ReadImageHeaders(stream, out bool inverted, out byte[] palette); - var image = new Image(this.Configuration, this.infoHeader.Width, this.infoHeader.Height, this.metadata); + image = new Image(this.Configuration, this.infoHeader.Width, this.infoHeader.Height, this.metadata); Buffer2D pixels = image.GetRootFramePixelBuffer(); @@ -193,8 +194,14 @@ namespace SixLabors.ImageSharp.Formats.Bmp } catch (IndexOutOfRangeException e) { + image?.Dispose(); throw new ImageFormatException("Bitmap does not have a valid format.", e); } + catch + { + image?.Dispose(); + throw; + } } /// diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 16dca3324f..ab857a404a 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -151,6 +151,11 @@ namespace SixLabors.ImageSharp.Formats.Gif } } } + //catch + //{ + // image?.Dispose(); + // throw; + //} finally { this.globalColorTable?.Dispose(); diff --git a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegTiffCompression.cs b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegTiffCompression.cs index cfbc32f4f6..4e788c76af 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegTiffCompression.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegTiffCompression.cs @@ -65,7 +65,8 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors jpegDecoder.ParseStream(stream, scanDecoderGray, CancellationToken.None); // TODO: Should we pass through the CancellationToken from the tiff decoder? - CopyImageBytesToBuffer(buffer, spectralConverterGray.GetPixelBuffer(CancellationToken.None)); + using var decompressedBuffer = spectralConverterGray.GetPixelBuffer(CancellationToken.None); + CopyImageBytesToBuffer(buffer, decompressedBuffer); break; } @@ -81,7 +82,8 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors jpegDecoder.ParseStream(stream, scanDecoder, CancellationToken.None); // TODO: Should we pass through the CancellationToken from the tiff decoder? - CopyImageBytesToBuffer(buffer, spectralConverter.GetPixelBuffer(CancellationToken.None)); + using var decompressedBuffer = spectralConverter.GetPixelBuffer(CancellationToken.None); + CopyImageBytesToBuffer(buffer, decompressedBuffer); break; } diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs index 1198c519a2..5dd99fafc1 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs @@ -240,8 +240,8 @@ namespace SixLabors.ImageSharp.Formats.Tiff var stripOffsetsArray = (Array)tags.GetValueInternal(ExifTag.StripOffsets).GetValue(); var stripByteCountsArray = (Array)tags.GetValueInternal(ExifTag.StripByteCounts).GetValue(); - IMemoryOwner stripOffsetsMemory = this.ConvertNumbers(stripOffsetsArray, out Span stripOffsets); - IMemoryOwner stripByteCountsMemory = this.ConvertNumbers(stripByteCountsArray, out Span stripByteCounts); + using IMemoryOwner stripOffsetsMemory = this.ConvertNumbers(stripOffsetsArray, out Span stripOffsets); + using IMemoryOwner stripByteCountsMemory = this.ConvertNumbers(stripByteCountsArray, out Span stripByteCounts); if (this.PlanarConfiguration == TiffPlanarConfiguration.Planar) { @@ -262,8 +262,6 @@ namespace SixLabors.ImageSharp.Formats.Tiff cancellationToken); } - stripOffsetsMemory?.Dispose(); - stripByteCountsMemory?.Dispose(); return frame; } diff --git a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs index 9d18e5d821..5646f71fac 100644 --- a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs +++ b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs @@ -82,38 +82,48 @@ namespace SixLabors.ImageSharp.Formats.Webp public Image Decode(BufferedReadStream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { - this.Metadata = new ImageMetadata(); - this.currentStream = stream; + Image image = null; + try + { - uint fileSize = this.ReadImageHeader(); + this.Metadata = new ImageMetadata(); + this.currentStream = stream; - using (this.webImageInfo = this.ReadVp8Info()) - { - if (this.webImageInfo.Features is { Animation: true }) - { - WebpThrowHelper.ThrowNotSupportedException("Animations are not supported"); - } + uint fileSize = this.ReadImageHeader(); - var image = new Image(this.Configuration, (int)this.webImageInfo.Width, (int)this.webImageInfo.Height, this.Metadata); - Buffer2D pixels = image.GetRootFramePixelBuffer(); - if (this.webImageInfo.IsLossless) + using (this.webImageInfo = this.ReadVp8Info()) { - var losslessDecoder = new WebpLosslessDecoder(this.webImageInfo.Vp8LBitReader, this.memoryAllocator, this.Configuration); - losslessDecoder.Decode(pixels, image.Width, image.Height); - } - else - { - var lossyDecoder = new WebpLossyDecoder(this.webImageInfo.Vp8BitReader, this.memoryAllocator, this.Configuration); - lossyDecoder.Decode(pixels, image.Width, image.Height, this.webImageInfo); - } + if (this.webImageInfo.Features is { Animation: true }) + { + WebpThrowHelper.ThrowNotSupportedException("Animations are not supported"); + } - // There can be optional chunks after the image data, like EXIF and XMP. - if (this.webImageInfo.Features != null) - { - this.ParseOptionalChunks(this.webImageInfo.Features); - } + image = new Image(this.Configuration, (int)this.webImageInfo.Width, (int)this.webImageInfo.Height, this.Metadata); + Buffer2D pixels = image.GetRootFramePixelBuffer(); + if (this.webImageInfo.IsLossless) + { + var losslessDecoder = new WebpLosslessDecoder(this.webImageInfo.Vp8LBitReader, this.memoryAllocator, this.Configuration); + losslessDecoder.Decode(pixels, image.Width, image.Height); + } + else + { + var lossyDecoder = new WebpLossyDecoder(this.webImageInfo.Vp8BitReader, this.memoryAllocator, this.Configuration); + lossyDecoder.Decode(pixels, image.Width, image.Height, this.webImageInfo); + } - return image; + // There can be optional chunks after the image data, like EXIF and XMP. + if (this.webImageInfo.Features != null) + { + this.ParseOptionalChunks(this.webImageInfo.Features); + } + + return image; + } + } + catch + { + image?.Dispose(); + throw; } } diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs index 71d0ab34a4..43ec45a34f 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs @@ -20,6 +20,7 @@ using static SixLabors.ImageSharp.Tests.TestImages.Bmp; namespace SixLabors.ImageSharp.Tests.Formats.Bmp { [Trait("Format", "Bmp")] + [ValidateDisposedMemoryAllocations] public class BmpDecoderTests { public const PixelTypes CommonNonDefaultPixelTypes = PixelTypes.Rgba32 | PixelTypes.Bgra32 | PixelTypes.RgbaVector; diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs index 1922b161f2..7a5241c5a8 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs @@ -18,6 +18,7 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Formats.Gif { [Trait("Format", "Gif")] + [ValidateDisposedMemoryAllocations] public class GifDecoderTests { private const PixelTypes TestPixelTypes = PixelTypes.Rgba32 | PixelTypes.RgbaVector | PixelTypes.Argb32; diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs index 9864d62b8f..d9915f17d6 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs @@ -179,11 +179,16 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg var testFile = TestFile.Create(imagePath); using (var stream = new MemoryStream(testFile.Bytes, false)) { - IImageInfo imageInfo = useIdentify - ? ((IImageInfoDetector)decoder).Identify(Configuration.Default, stream, default) - : decoder.Decode(Configuration.Default, stream, default); - - test(imageInfo); + if (useIdentify) + { + IImageInfo imageInfo = ((IImageInfoDetector)decoder).Identify(Configuration.Default, stream, default); + test(imageInfo); + } + else + { + using var img = decoder.Decode(Configuration.Default, stream, default); + test(img); + } } } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index faf40ccf7d..1faa6f0f4c 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -22,6 +22,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg { // TODO: Scatter test cases into multiple test classes [Trait("Format", "Jpg")] + [ValidateDisposedMemoryAllocations] public partial class JpegDecoderTests { public const PixelTypes CommonNonDefaultPixelTypes = PixelTypes.Rgba32 | PixelTypes.Argb32 | PixelTypes.Bgr24 | PixelTypes.RgbaVector; diff --git a/tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs index 97237bca59..eb3bc8c9a5 100644 --- a/tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs @@ -11,6 +11,7 @@ using static SixLabors.ImageSharp.Tests.TestImages.Pbm; namespace SixLabors.ImageSharp.Tests.Formats.Pbm { [Trait("Format", "Pbm")] + [ValidateDisposedMemoryAllocations] public class PbmDecoderTests { [Theory] diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index eadbf138a6..a4fcf63baf 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -19,6 +19,7 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Formats.Png { [Trait("Format", "Png")] + [ValidateDisposedMemoryAllocations] public partial class PngDecoderTests { private const PixelTypes TestPixelTypes = PixelTypes.Rgba32 | PixelTypes.RgbaVector | PixelTypes.Argb32; @@ -103,7 +104,6 @@ namespace SixLabors.ImageSharp.Tests.Formats.Png [Theory] [WithFileCollection(nameof(CommonTestImages), PixelTypes.Rgba32)] - [ValidateDisposedMemoryAllocations] public void Decode(TestImageProvider provider) where TPixel : unmanaged, IPixel { diff --git a/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs index 55dc2ecdd8..e83c5a98cc 100644 --- a/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tga/TgaDecoderTests.cs @@ -16,6 +16,7 @@ using static SixLabors.ImageSharp.Tests.TestImages.Tga; namespace SixLabors.ImageSharp.Tests.Formats.Tga { [Trait("Format", "Tga")] + [ValidateDisposedMemoryAllocations] public class TgaDecoderTests { private static TgaDecoder TgaDecoder => new(); diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs index 39fa50c93c..9460f3a351 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs @@ -14,6 +14,7 @@ using static SixLabors.ImageSharp.Tests.TestImages.Tiff; namespace SixLabors.ImageSharp.Tests.Formats.Tiff { [Trait("Format", "Tiff")] + [ValidateDisposedMemoryAllocations] public class TiffDecoderTests : TiffDecoderBaseTester { public static readonly string[] MultiframeTestImages = Multiframes; diff --git a/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs b/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs index 1c92fdf335..f29fa5d793 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/WebpDecoderTests.cs @@ -13,6 +13,7 @@ using static SixLabors.ImageSharp.Tests.TestImages.Webp; namespace SixLabors.ImageSharp.Tests.Formats.Webp { [Trait("Format", "Webp")] + [ValidateDisposedMemoryAllocations] public class WebpDecoderTests { private static WebpDecoder WebpDecoder => new(); diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparingUtils.cs b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparingUtils.cs index 1801d6b590..fa25846748 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparingUtils.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ImageComparingUtils.cs @@ -26,7 +26,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison } var testFile = TestFile.Create(path); - Image magickImage = DecodeWithMagick(new FileInfo(testFile.FullPath)); + using Image magickImage = DecodeWithMagick(new FileInfo(testFile.FullPath)); if (useExactComparer) { ImageComparer.Exact.VerifySimilarity(magickImage, image); From 1557baea5932d87f050febe7a443c81a83db1376 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sat, 9 Apr 2022 21:30:59 +0100 Subject: [PATCH 15/34] dispose the next chunk --- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 497dc39674..12770bc521 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -227,10 +227,16 @@ namespace SixLabors.ImageSharp.Formats.Png return image; } + catch + { + image?.Dispose(); + throw; + } finally { this.scanline?.Dispose(); this.previousScanline?.Dispose(); + this.nextChunk?.Data?.Dispose(); } } @@ -472,6 +478,8 @@ namespace SixLabors.ImageSharp.Formats.Png this.bytesPerSample = this.header.BitDepth / 8; } + this.previousScanline?.Dispose(); + this.scanline?.Dispose(); this.previousScanline = this.memoryAllocator.Allocate(this.bytesPerScanline, AllocationOptions.Clean); this.scanline = this.Configuration.MemoryAllocator.Allocate(this.bytesPerScanline, AllocationOptions.Clean); } @@ -1359,6 +1367,7 @@ namespace SixLabors.ImageSharp.Formats.Png { if (chunk.Type == PngChunkType.Data) { + chunk.Data?.Dispose(); return chunk.Length; } @@ -1453,6 +1462,9 @@ namespace SixLabors.ImageSharp.Formats.Png if (validCrc != inputCrc) { string chunkTypeName = Encoding.ASCII.GetString(chunkType); + + // ensure when throwing we dispose the data back to the memory allocator + chunk.Data?.Dispose(); PngThrowHelper.ThrowInvalidChunkCrc(chunkTypeName); } } From 2a1ae5cc4ccef9da590d44387ead974c78004845 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sat, 9 Apr 2022 22:21:19 +0100 Subject: [PATCH 16/34] ensure we dispose beffers when decoders throw --- src/Directory.Build.props | 1 + .../Decoder/SpectralConverter{TPixel}.cs | 5 +- .../Formats/Jpeg/JpegDecoderCore.cs | 35 ++++++----- .../Formats/Tiff/TiffDecoderCore.cs | 58 +++++++++++-------- 4 files changed, 61 insertions(+), 38 deletions(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index d211992a94..faa29865f2 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -27,6 +27,7 @@ + diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index 430adeb21d..532892e060 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -95,7 +95,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder } } - return this.pixelBuffer; + var buffer = this.pixelBuffer; + this.pixelBuffer = null; + return buffer; } /// @@ -210,6 +212,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder this.rgbBuffer?.Dispose(); this.paddedProxyPixelRow?.Dispose(); + this.pixelBuffer?.Dispose(); } } } diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index ef4e3ffac2..a4b42b994d 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -187,20 +187,27 @@ namespace SixLabors.ImageSharp.Formats.Jpeg where TPixel : unmanaged, IPixel { using var spectralConverter = new SpectralConverter(this.Configuration); - - var scanDecoder = new HuffmanScanDecoder(stream, spectralConverter, cancellationToken); - - this.ParseStream(stream, scanDecoder, cancellationToken); - this.InitExifProfile(); - this.InitIccProfile(); - this.InitIptcProfile(); - this.InitXmpProfile(); - this.InitDerivedMetadataProperties(); - - return new Image( - this.Configuration, - spectralConverter.GetPixelBuffer(cancellationToken), - this.Metadata); + try + { + var scanDecoder = new HuffmanScanDecoder(stream, spectralConverter, cancellationToken); + + this.ParseStream(stream, scanDecoder, cancellationToken); + this.InitExifProfile(); + this.InitIccProfile(); + this.InitIptcProfile(); + this.InitXmpProfile(); + this.InitDerivedMetadataProperties(); + + return new Image( + this.Configuration, + spectralConverter.GetPixelBuffer(cancellationToken), + this.Metadata); + } + catch + { + this.Frame?.Dispose(); + throw; + } } /// diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs index 5dd99fafc1..1cd3d2c0c1 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs @@ -157,40 +157,52 @@ namespace SixLabors.ImageSharp.Formats.Tiff public Image Decode(BufferedReadStream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { - this.inputStream = stream; - var reader = new DirectoryReader(stream, this.Configuration.MemoryAllocator); - - IEnumerable directories = reader.Read(); - this.byteOrder = reader.ByteOrder; - this.isBigTiff = reader.IsBigTiff; - var frames = new List>(); - foreach (ExifProfile ifd in directories) + try { - cancellationToken.ThrowIfCancellationRequested(); - ImageFrame frame = this.DecodeFrame(ifd, cancellationToken); - frames.Add(frame); + this.inputStream = stream; + var reader = new DirectoryReader(stream, this.Configuration.MemoryAllocator); + + IEnumerable directories = reader.Read(); + this.byteOrder = reader.ByteOrder; + this.isBigTiff = reader.IsBigTiff; - if (this.decodingMode is FrameDecodingMode.First) + foreach (ExifProfile ifd in directories) { - break; + cancellationToken.ThrowIfCancellationRequested(); + ImageFrame frame = this.DecodeFrame(ifd, cancellationToken); + frames.Add(frame); + + if (this.decodingMode is FrameDecodingMode.First) + { + break; + } } - } - ImageMetadata metadata = TiffDecoderMetadataCreator.Create(frames, this.ignoreMetadata, reader.ByteOrder, reader.IsBigTiff); + ImageMetadata metadata = TiffDecoderMetadataCreator.Create(frames, this.ignoreMetadata, reader.ByteOrder, reader.IsBigTiff); - // TODO: Tiff frames can have different sizes. - ImageFrame root = frames[0]; - this.Dimensions = root.Size(); - foreach (ImageFrame frame in frames) - { - if (frame.Size() != root.Size()) + // TODO: Tiff frames can have different sizes. + ImageFrame root = frames[0]; + this.Dimensions = root.Size(); + foreach (ImageFrame frame in frames) { - TiffThrowHelper.ThrowNotSupported("Images with different sizes are not supported"); + if (frame.Size() != root.Size()) + { + TiffThrowHelper.ThrowNotSupported("Images with different sizes are not supported"); + } } + + return new Image(this.Configuration, metadata, frames); } + catch + { + foreach (ImageFrame f in frames) + { + f.Dispose(); + } - return new Image(this.Configuration, metadata, frames); + throw; + } } /// From a30c468f2757906c581e95adb7e6750fffdad0fe Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sat, 9 Apr 2022 22:35:21 +0100 Subject: [PATCH 17/34] drop commented code --- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index ab857a404a..16dca3324f 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -151,11 +151,6 @@ namespace SixLabors.ImageSharp.Formats.Gif } } } - //catch - //{ - // image?.Dispose(); - // throw; - //} finally { this.globalColorTable?.Dispose(); From 399d71ddf16c77ec660294c5a1142d5ecc736afd Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sat, 9 Apr 2022 22:39:42 +0100 Subject: [PATCH 18/34] stylecopped --- src/ImageSharp/Formats/Webp/WebpDecoderCore.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs index 5646f71fac..7052be4ea6 100644 --- a/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs +++ b/src/ImageSharp/Formats/Webp/WebpDecoderCore.cs @@ -85,7 +85,6 @@ namespace SixLabors.ImageSharp.Formats.Webp Image image = null; try { - this.Metadata = new ImageMetadata(); this.currentStream = stream; From d9af2395ee7fd6b3cc5b7032a13d0f68689eacc2 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sat, 9 Apr 2022 22:48:48 +0100 Subject: [PATCH 19/34] indent for full framwork --- src/ImageSharp/Diagnostics/MemoryDiagnostics.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs b/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs index 6af2544138..f2a433ef1b 100644 --- a/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs +++ b/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs @@ -131,9 +131,9 @@ namespace SixLabors.ImageSharp.Diagnostics allocationStackTrace, preferLocal: false); #else - ThreadPool.QueueUserWorkItem( - stackTrace => this.undisposedAllocation?.Invoke((string)stackTrace), - allocationStackTrace); + ThreadPool.QueueUserWorkItem( + stackTrace => this.undisposedAllocation?.Invoke((string)stackTrace), + allocationStackTrace); #endif } } From 99d12664a2cc558510cf97ecebe67f869dfb60b1 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sun, 10 Apr 2022 09:32:50 +0100 Subject: [PATCH 20/34] Async local doesn't actually work for the finalizers, revert that piece. --- .../Diagnostics/MemoryDiagnostics.cs | 96 ++++++++----------- 1 file changed, 40 insertions(+), 56 deletions(-) diff --git a/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs b/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs index f2a433ef1b..057242bae8 100644 --- a/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs +++ b/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs @@ -17,6 +17,10 @@ namespace SixLabors.ImageSharp.Diagnostics { internal static readonly InteralMemoryDiagnostics Default = new(); private static AsyncLocal localInstance = null; + + // the async local end up out of scope during finalizers so putting into thte internal class is useless + private static UndisposedAllocationDelegate undisposedAllocation; + private static int undisposedAllocationSubscriptionCounter; private static readonly object SyncRoot = new(); /// @@ -26,8 +30,23 @@ namespace SixLabors.ImageSharp.Diagnostics /// public static event UndisposedAllocationDelegate UndisposedAllocation { - add => Current.UndisposedAllocation += value; - remove => Current.UndisposedAllocation -= value; + add + { + lock (SyncRoot) + { + undisposedAllocationSubscriptionCounter++; + undisposedAllocation += value; + } + } + + remove + { + lock (SyncRoot) + { + undisposedAllocation -= value; + undisposedAllocationSubscriptionCounter--; + } + } } internal static InteralMemoryDiagnostics Current @@ -61,81 +80,46 @@ namespace SixLabors.ImageSharp.Diagnostics /// public static int TotalUndisposedAllocationCount => Current.TotalUndisposedAllocationCount; - internal static bool UndisposedAllocationSubscribed => Current.UndisposedAllocationSubscribed; + internal static bool UndisposedAllocationSubscribed => Volatile.Read(ref undisposedAllocationSubscriptionCounter) > 0; internal static void IncrementTotalUndisposedAllocationCount() => Current.IncrementTotalUndisposedAllocationCount(); internal static void DecrementTotalUndisposedAllocationCount() => Current.DecrementTotalUndisposedAllocationCount(); internal static void RaiseUndisposedMemoryResource(string allocationStackTrace) - => Current.RaiseUndisposedMemoryResource(allocationStackTrace); + { + if (undisposedAllocation is null) + { + return; + } + + // Schedule on the ThreadPool, to avoid user callback messing up the finalizer thread. +#if NETSTANDARD2_1 || NETCOREAPP2_1_OR_GREATER + ThreadPool.QueueUserWorkItem( + stackTrace => undisposedAllocation?.Invoke(stackTrace), + allocationStackTrace, + preferLocal: false); +#else + ThreadPool.QueueUserWorkItem( + stackTrace => undisposedAllocation?.Invoke((string)stackTrace), + allocationStackTrace); +#endif + } internal class InteralMemoryDiagnostics { private int totalUndisposedAllocationCount; - private UndisposedAllocationDelegate undisposedAllocation; - private int undisposedAllocationSubscriptionCounter; - private readonly object syncRoot = new(); - - /// - /// Fires when an ImageSharp object's undisposed memory resource leaks to the finalizer. - /// The event brings significant overhead, and is intended to be used for troubleshooting only. - /// For production diagnostics, use . - /// - public event UndisposedAllocationDelegate UndisposedAllocation - { - add - { - lock (this.syncRoot) - { - this.undisposedAllocationSubscriptionCounter++; - this.undisposedAllocation += value; - } - } - - remove - { - lock (this.syncRoot) - { - this.undisposedAllocation -= value; - this.undisposedAllocationSubscriptionCounter--; - } - } - } - /// /// Gets a value indicating the total number of memory resource objects leaked to the finalizer. /// public int TotalUndisposedAllocationCount => this.totalUndisposedAllocationCount; - internal bool UndisposedAllocationSubscribed => Volatile.Read(ref this.undisposedAllocationSubscriptionCounter) > 0; - internal void IncrementTotalUndisposedAllocationCount() => Interlocked.Increment(ref this.totalUndisposedAllocationCount); internal void DecrementTotalUndisposedAllocationCount() => Interlocked.Decrement(ref this.totalUndisposedAllocationCount); - - internal void RaiseUndisposedMemoryResource(string allocationStackTrace) - { - if (this.undisposedAllocation is null) - { - return; - } - - // Schedule on the ThreadPool, to avoid user callback messing up the finalizer thread. -#if NETSTANDARD2_1 || NETCOREAPP2_1_OR_GREATER - ThreadPool.QueueUserWorkItem( - stackTrace => this.undisposedAllocation?.Invoke(stackTrace), - allocationStackTrace, - preferLocal: false); -#else - ThreadPool.QueueUserWorkItem( - stackTrace => this.undisposedAllocation?.Invoke((string)stackTrace), - allocationStackTrace); -#endif - } } } } From 558ff99b08d9a316b8a3f6502b0dc465677a9b16 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sun, 10 Apr 2022 09:42:48 +0100 Subject: [PATCH 21/34] revert Frame Dispose --- .../Formats/Jpeg/JpegDecoderCore.cs | 35 ++++++++----------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index a4b42b994d..ef4e3ffac2 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -187,27 +187,20 @@ namespace SixLabors.ImageSharp.Formats.Jpeg where TPixel : unmanaged, IPixel { using var spectralConverter = new SpectralConverter(this.Configuration); - try - { - var scanDecoder = new HuffmanScanDecoder(stream, spectralConverter, cancellationToken); - - this.ParseStream(stream, scanDecoder, cancellationToken); - this.InitExifProfile(); - this.InitIccProfile(); - this.InitIptcProfile(); - this.InitXmpProfile(); - this.InitDerivedMetadataProperties(); - - return new Image( - this.Configuration, - spectralConverter.GetPixelBuffer(cancellationToken), - this.Metadata); - } - catch - { - this.Frame?.Dispose(); - throw; - } + + var scanDecoder = new HuffmanScanDecoder(stream, spectralConverter, cancellationToken); + + this.ParseStream(stream, scanDecoder, cancellationToken); + this.InitExifProfile(); + this.InitIccProfile(); + this.InitIptcProfile(); + this.InitXmpProfile(); + this.InitDerivedMetadataProperties(); + + return new Image( + this.Configuration, + spectralConverter.GetPixelBuffer(cancellationToken), + this.Metadata); } /// From ced98879ddb048860732aafb1f0678a51e9df1d5 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Mon, 11 Apr 2022 14:49:41 +0200 Subject: [PATCH 22/34] Add checks, if enough data was read --- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 16dca3324f..d17e89cd45 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -221,7 +221,11 @@ namespace SixLabors.ImageSharp.Formats.Gif /// private void ReadGraphicalControlExtension() { - this.stream.Read(this.buffer, 0, 6); + int bytesRead = this.stream.Read(this.buffer, 0, 6); + if (bytesRead != 6) + { + GifThrowHelper.ThrowInvalidImageContentException("Not enough data to read the graphic control extension"); + } this.graphicsControlExtension = GifGraphicControlExtension.Parse(this.buffer); } @@ -231,7 +235,11 @@ namespace SixLabors.ImageSharp.Formats.Gif /// private void ReadImageDescriptor() { - this.stream.Read(this.buffer, 0, 9); + int bytesRead = this.stream.Read(this.buffer, 0, 9); + if (bytesRead != 9) + { + GifThrowHelper.ThrowInvalidImageContentException("Not enough data to read the image descriptor"); + } this.imageDescriptor = GifImageDescriptor.Parse(this.buffer); if (this.imageDescriptor.Height == 0 || this.imageDescriptor.Width == 0) @@ -245,7 +253,11 @@ namespace SixLabors.ImageSharp.Formats.Gif /// private void ReadLogicalScreenDescriptor() { - this.stream.Read(this.buffer, 0, 7); + int bytesRead = this.stream.Read(this.buffer, 0, 7); + if (bytesRead != 7) + { + GifThrowHelper.ThrowInvalidImageContentException("Not enough data to read the logical screen descriptor"); + } this.logicalScreenDescriptor = GifLogicalScreenDescriptor.Parse(this.buffer); } From cd7c91ccdf5a55e24fc62e63a7f4fca803341210 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Mon, 11 Apr 2022 16:20:30 +0100 Subject: [PATCH 23/34] move AsyncLocal out of core lib and into testing --- .../Diagnostics/MemoryDiagnostics.cs | 67 ++++++------------- .../MemoryAllocatorValidator.cs | 64 +++++++++++++----- .../ImageProviders/FileProvider.cs | 4 +- ...idateDisposedMemoryAllocationsAttribute.cs | 19 +++--- 4 files changed, 80 insertions(+), 74 deletions(-) diff --git a/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs b/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs index 057242bae8..237f85b0f9 100644 --- a/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs +++ b/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. +using System; using System.Threading; namespace SixLabors.ImageSharp.Diagnostics @@ -15,10 +16,8 @@ namespace SixLabors.ImageSharp.Diagnostics /// public static class MemoryDiagnostics { - internal static readonly InteralMemoryDiagnostics Default = new(); - private static AsyncLocal localInstance = null; + private static int totalUndisposedAllocationCount; - // the async local end up out of scope during finalizers so putting into thte internal class is useless private static UndisposedAllocationDelegate undisposedAllocation; private static int undisposedAllocationSubscriptionCounter; private static readonly object SyncRoot = new(); @@ -49,42 +48,34 @@ namespace SixLabors.ImageSharp.Diagnostics } } - internal static InteralMemoryDiagnostics Current - { - get - { - if (localInstance != null && localInstance.Value != null) - { - return localInstance.Value; - } - - return Default; - } - - set - { - if (localInstance == null) - { - lock (SyncRoot) - { - localInstance ??= new AsyncLocal(); - } - } + /// + /// Fires when ImageSharp allocates memory from a MemoryAllocator + /// + internal static event Action MemoryAllocated; - localInstance.Value = value; - } - } + /// + /// Fires when ImageSharp releases allocated from a MemoryAllocator + /// + internal static event Action MemoryReleased; /// /// Gets a value indicating the total number of memory resource objects leaked to the finalizer. /// - public static int TotalUndisposedAllocationCount => Current.TotalUndisposedAllocationCount; + public static int TotalUndisposedAllocationCount => totalUndisposedAllocationCount; internal static bool UndisposedAllocationSubscribed => Volatile.Read(ref undisposedAllocationSubscriptionCounter) > 0; - internal static void IncrementTotalUndisposedAllocationCount() => Current.IncrementTotalUndisposedAllocationCount(); + internal static void IncrementTotalUndisposedAllocationCount() + { + Interlocked.Increment(ref totalUndisposedAllocationCount); + MemoryAllocated?.Invoke(); + } - internal static void DecrementTotalUndisposedAllocationCount() => Current.DecrementTotalUndisposedAllocationCount(); + internal static void DecrementTotalUndisposedAllocationCount() + { + Interlocked.Decrement(ref totalUndisposedAllocationCount); + MemoryReleased?.Invoke(); + } internal static void RaiseUndisposedMemoryResource(string allocationStackTrace) { @@ -105,21 +96,5 @@ namespace SixLabors.ImageSharp.Diagnostics allocationStackTrace); #endif } - - internal class InteralMemoryDiagnostics - { - private int totalUndisposedAllocationCount; - - /// - /// Gets a value indicating the total number of memory resource objects leaked to the finalizer. - /// - public int TotalUndisposedAllocationCount => this.totalUndisposedAllocationCount; - - internal void IncrementTotalUndisposedAllocationCount() => - Interlocked.Increment(ref this.totalUndisposedAllocationCount); - - internal void DecrementTotalUndisposedAllocationCount() => - Interlocked.Decrement(ref this.totalUndisposedAllocationCount); - } } } diff --git a/tests/ImageSharp.Tests/MemoryAllocatorValidator.cs b/tests/ImageSharp.Tests/MemoryAllocatorValidator.cs index 2c3d98eb13..13664ee9b2 100644 --- a/tests/ImageSharp.Tests/MemoryAllocatorValidator.cs +++ b/tests/ImageSharp.Tests/MemoryAllocatorValidator.cs @@ -2,7 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; -using System.Diagnostics; +using System.Threading; using SixLabors.ImageSharp.Diagnostics; using Xunit; @@ -10,34 +10,68 @@ namespace SixLabors.ImageSharp.Tests { public static class MemoryAllocatorValidator { - public static IDisposable MonitorAllocations(int max = 0) + private static readonly AsyncLocal LocalInstance = new(); + + public static bool MonitoringAllocations => LocalInstance.Value != null; + + static MemoryAllocatorValidator() { - MemoryDiagnostics.Current = new(); - return new TestMemoryAllocatorDisposable(max); + MemoryDiagnostics.MemoryAllocated += MemoryDiagnostics_MemoryAllocated; + MemoryDiagnostics.MemoryReleased += MemoryDiagnostics_MemoryReleased; } - public static void ValidateAllocation(int max = 0) + private static void MemoryDiagnostics_MemoryReleased() { - var count = MemoryDiagnostics.TotalUndisposedAllocationCount; - var pass = count <= max; - Assert.True(pass, $"Expected a max of {max} undisposed buffers but found {count}"); + TestMemoryDiagnostics backing = LocalInstance.Value; + if (backing != null) + { + backing.TotalRemainingAllocated--; + } + } - if (count > 0) + private static void MemoryDiagnostics_MemoryAllocated() + { + TestMemoryDiagnostics backing = LocalInstance.Value; + if (backing != null) { - Debug.WriteLine("We should have Zero undisposed memory allocations."); + backing.TotalAllocated++; + backing.TotalRemainingAllocated++; } + } - MemoryDiagnostics.Current = null; + public static TestMemoryDiagnostics MonitorAllocations() + { + var diag = new TestMemoryDiagnostics(); + LocalInstance.Value = diag; + return diag; } - public struct TestMemoryAllocatorDisposable : IDisposable + public static void StopMonitoringAllocations() => LocalInstance.Value = null; + + public static void ValidateAllocations(int expectedAllocationCount = 0) + => LocalInstance.Value?.Validate(expectedAllocationCount); + + public class TestMemoryDiagnostics : IDisposable { - private readonly int max; + public int TotalAllocated { get; set; } - public TestMemoryAllocatorDisposable(int max) => this.max = max; + public int TotalRemainingAllocated { get; set; } + + public void Validate(int expectedAllocationCount) + { + var count = this.TotalRemainingAllocated; + var pass = expectedAllocationCount == count; + Assert.True(pass, $"Expected a {expectedAllocationCount} undisposed buffers but found {count}"); + } public void Dispose() - => ValidateAllocation(this.max); + { + this.Validate(0); + if (LocalInstance.Value == this) + { + StopMonitoringAllocations(); + } + } } } } diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs index 3675356c38..63c5ce31a2 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs @@ -159,8 +159,8 @@ namespace SixLabors.ImageSharp.Tests return this.LoadImage(decoder); } - // do cache so we can track allocation correctly when validating memory - if (MemoryDiagnostics.Current != MemoryDiagnostics.Default) + // do not cache so we can track allocation correctly when validating memory + if (MemoryAllocatorValidator.MonitoringAllocations) { return this.LoadImage(decoder); } diff --git a/tests/ImageSharp.Tests/ValidateDisposedMemoryAllocationsAttribute.cs b/tests/ImageSharp.Tests/ValidateDisposedMemoryAllocationsAttribute.cs index 95d13b5954..65ed990dd7 100644 --- a/tests/ImageSharp.Tests/ValidateDisposedMemoryAllocationsAttribute.cs +++ b/tests/ImageSharp.Tests/ValidateDisposedMemoryAllocationsAttribute.cs @@ -11,26 +11,23 @@ namespace SixLabors.ImageSharp.Tests [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class ValidateDisposedMemoryAllocationsAttribute : BeforeAfterTestAttribute { - private readonly int max = 0; + private readonly int expected = 0; public ValidateDisposedMemoryAllocationsAttribute() : this(0) { } - public ValidateDisposedMemoryAllocationsAttribute(int max) - { - this.max = max; - if (max > 0) - { - Debug.WriteLine("Needs fixing, we shoudl have Zero undisposed memory allocations."); - } - } + public ValidateDisposedMemoryAllocationsAttribute(int expected) + => this.expected = expected; public override void Before(MethodInfo methodUnderTest) - => MemoryAllocatorValidator.MonitorAllocations(this.max); // the disposable isn't important cause the validate below does the same thing + => MemoryAllocatorValidator.MonitorAllocations(); public override void After(MethodInfo methodUnderTest) - => MemoryAllocatorValidator.ValidateAllocation(this.max); + { + MemoryAllocatorValidator.ValidateAllocations(this.expected); + MemoryAllocatorValidator.StopMonitoringAllocations(); + } } } From f5975075006934c25afea64868b99ab9d598e9bc Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Mon, 11 Apr 2022 16:23:59 +0100 Subject: [PATCH 24/34] fix comment --- src/ImageSharp/Diagnostics/MemoryDiagnostics.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs b/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs index 237f85b0f9..89f18cff61 100644 --- a/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs +++ b/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs @@ -54,7 +54,7 @@ namespace SixLabors.ImageSharp.Diagnostics internal static event Action MemoryAllocated; /// - /// Fires when ImageSharp releases allocated from a MemoryAllocator + /// Fires when ImageSharp releases memory allocated from a MemoryAllocator /// internal static event Action MemoryReleased; From f4f521cc5beccaa3b1ecf4f41dfacc6a0cb298a8 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Tue, 12 Apr 2022 17:56:08 +0100 Subject: [PATCH 25/34] revert to allow easier merge --- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 12770bc521..05ce8d92e4 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -1367,7 +1367,6 @@ namespace SixLabors.ImageSharp.Formats.Png { if (chunk.Type == PngChunkType.Data) { - chunk.Data?.Dispose(); return chunk.Length; } From 03d0b63c7c5ddf487f351238395f771976624544 Mon Sep 17 00:00:00 2001 From: miere43 Date: Sat, 9 Apr 2022 17:57:06 +0300 Subject: [PATCH 26/34] Fix chunk data memory leak when decoding PNG. Fix #2080 --- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 05ce8d92e4..12770bc521 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -1367,6 +1367,7 @@ namespace SixLabors.ImageSharp.Formats.Png { if (chunk.Type == PngChunkType.Data) { + chunk.Data?.Dispose(); return chunk.Length; } From 8cdcda343b5004b9f41da9884ffbce162e361400 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Wed, 13 Apr 2022 15:56:23 +0300 Subject: [PATCH 27/34] Added sanity check for every jpeg marker --- .../Formats/Jpeg/JpegDecoderCore.cs | 40 +++++++++++-------- .../Formats/Jpeg/JpegThrowHelper.cs | 3 ++ src/ImageSharp/IO/BufferedReadStream.cs | 9 +++++ 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index ef4e3ffac2..52cd411916 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -315,14 +315,22 @@ namespace SixLabors.ImageSharp.Formats.Jpeg if (!fileMarker.Invalid) { // Get the marker length. - int remaining = this.ReadUint16(stream) - 2; + int markerContentByteSize = this.ReadUint16(stream) - 2; + + // Check whether stream actually has enought bytes to read + // markerContentByteSize is always positive so we cast + // to uint to avoid sign extension + if (stream.RemainingBytes < (uint)markerContentByteSize) + { + JpegThrowHelper.ThrowNotEnoughBytesForMarker(fileMarker.Marker); + } switch (fileMarker.Marker) { case JpegConstants.Markers.SOF0: case JpegConstants.Markers.SOF1: case JpegConstants.Markers.SOF2: - this.ProcessStartOfFrameMarker(stream, remaining, fileMarker, metadataOnly); + this.ProcessStartOfFrameMarker(stream, markerContentByteSize, fileMarker, metadataOnly); break; case JpegConstants.Markers.SOF5: @@ -350,7 +358,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg case JpegConstants.Markers.SOS: if (!metadataOnly) { - this.ProcessStartOfScanMarker(stream, remaining); + this.ProcessStartOfScanMarker(stream, markerContentByteSize); break; } else @@ -364,41 +372,41 @@ namespace SixLabors.ImageSharp.Formats.Jpeg if (metadataOnly) { - stream.Skip(remaining); + stream.Skip(markerContentByteSize); } else { - this.ProcessDefineHuffmanTablesMarker(stream, remaining); + this.ProcessDefineHuffmanTablesMarker(stream, markerContentByteSize); } break; case JpegConstants.Markers.DQT: - this.ProcessDefineQuantizationTablesMarker(stream, remaining); + this.ProcessDefineQuantizationTablesMarker(stream, markerContentByteSize); break; case JpegConstants.Markers.DRI: if (metadataOnly) { - stream.Skip(remaining); + stream.Skip(markerContentByteSize); } else { - this.ProcessDefineRestartIntervalMarker(stream, remaining); + this.ProcessDefineRestartIntervalMarker(stream, markerContentByteSize); } break; case JpegConstants.Markers.APP0: - this.ProcessApplicationHeaderMarker(stream, remaining); + this.ProcessApplicationHeaderMarker(stream, markerContentByteSize); break; case JpegConstants.Markers.APP1: - this.ProcessApp1Marker(stream, remaining); + this.ProcessApp1Marker(stream, markerContentByteSize); break; case JpegConstants.Markers.APP2: - this.ProcessApp2Marker(stream, remaining); + this.ProcessApp2Marker(stream, markerContentByteSize); break; case JpegConstants.Markers.APP3: @@ -411,20 +419,20 @@ namespace SixLabors.ImageSharp.Formats.Jpeg case JpegConstants.Markers.APP10: case JpegConstants.Markers.APP11: case JpegConstants.Markers.APP12: - stream.Skip(remaining); + stream.Skip(markerContentByteSize); break; case JpegConstants.Markers.APP13: - this.ProcessApp13Marker(stream, remaining); + this.ProcessApp13Marker(stream, markerContentByteSize); break; case JpegConstants.Markers.APP14: - this.ProcessApp14Marker(stream, remaining); + this.ProcessApp14Marker(stream, markerContentByteSize); break; case JpegConstants.Markers.APP15: case JpegConstants.Markers.COM: - stream.Skip(remaining); + stream.Skip(markerContentByteSize); break; case JpegConstants.Markers.DAC: @@ -1260,7 +1268,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg int selectorsBytes = selectorsCount * 2; if (remaining != 4 + selectorsBytes) { - JpegThrowHelper.ThrowBadMarker("SOS", remaining); + JpegThrowHelper.ThrowBadMarker(nameof(JpegConstants.Markers.SOS), remaining); } // selectorsCount*2 bytes: component index + huffman tables indices diff --git a/src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs b/src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs index b238e45ef3..1073ffff78 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs @@ -25,6 +25,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg [MethodImpl(InliningOptions.ColdPath)] public static void ThrowBadMarker(string marker, int length) => throw new InvalidImageContentException($"Marker {marker} has bad length {length}."); + [MethodImpl(InliningOptions.ColdPath)] + public static void ThrowNotEnoughBytesForMarker(byte marker) => throw new InvalidImageContentException($"Input stream does not have enough bytes to parse declared contents of the {marker:X2} marker."); + [MethodImpl(InliningOptions.ColdPath)] public static void ThrowBadQuantizationTableIndex(int index) => throw new InvalidImageContentException($"Bad Quantization Table index {index}."); diff --git a/src/ImageSharp/IO/BufferedReadStream.cs b/src/ImageSharp/IO/BufferedReadStream.cs index 4ab7f312b2..2823b8ed6f 100644 --- a/src/ImageSharp/IO/BufferedReadStream.cs +++ b/src/ImageSharp/IO/BufferedReadStream.cs @@ -114,6 +114,15 @@ namespace SixLabors.ImageSharp.IO /// public override bool CanWrite { get; } = false; + /// + /// Gets remaining byte count available to read. + /// + public long RemainingBytes + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => this.Length - this.Position; + } + /// /// Gets the underlying stream. /// From 64d9146a8d6def56c5970c774193e36aeb202f90 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Wed, 13 Apr 2022 22:00:40 +0300 Subject: [PATCH 28/34] Added malformed image --- tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs | 1 + tests/ImageSharp.Tests/TestImages.cs | 1 + .../Input/Jpg/issues/fuzz/Issue2085-NullReferenceException.jpg | 3 +++ 3 files changed, 5 insertions(+) create mode 100644 tests/Images/Input/Jpg/issues/fuzz/Issue2085-NullReferenceException.jpg diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs index 70cbc3af72..d5e0f081bf 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs @@ -105,6 +105,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg TestImages.Jpeg.Issues.Fuzz.IndexOutOfRangeException1693A, TestImages.Jpeg.Issues.Fuzz.IndexOutOfRangeException1693B, TestImages.Jpeg.Issues.Fuzz.IndexOutOfRangeException824C, + TestImages.Jpeg.Issues.Fuzz.NullReferenceException2085, }; private static readonly Dictionary CustomToleranceValues = new() diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 07aff6fc12..bbc0b1e3db 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -292,6 +292,7 @@ namespace SixLabors.ImageSharp.Tests public const string AccessViolationException922 = "Jpg/issues/fuzz/Issue922-AccessViolationException.jpg"; public const string IndexOutOfRangeException1693A = "Jpg/issues/fuzz/Issue1693-IndexOutOfRangeException-A.jpg"; public const string IndexOutOfRangeException1693B = "Jpg/issues/fuzz/Issue1693-IndexOutOfRangeException-B.jpg"; + public const string NullReferenceException2085 = "Jpg/issues/fuzz/Issue2085-NullReferenceException.jpg"; } } diff --git a/tests/Images/Input/Jpg/issues/fuzz/Issue2085-NullReferenceException.jpg b/tests/Images/Input/Jpg/issues/fuzz/Issue2085-NullReferenceException.jpg new file mode 100644 index 0000000000..8a680ff6a6 --- /dev/null +++ b/tests/Images/Input/Jpg/issues/fuzz/Issue2085-NullReferenceException.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d478beff34179fda26238a44434607c276f55438ee96824c5af8c0188d358d8d +size 234 From c7f9d54705f617e14a415346a2c8f7ce199091c7 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Thu, 14 Apr 2022 14:01:01 +0300 Subject: [PATCH 29/34] Fixed string exif value corner case null ref exception --- .../Profiles/Exif/ExifEncodedStringHelpers.cs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs b/src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs index 5fd613b1f0..6e697029eb 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs @@ -79,16 +79,8 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif return CharacterCodeBytesLength + count; } - public static unsafe int Write(Encoding encoding, string value, Span destination) - { - fixed (char* c = value) - { - fixed (byte* b = destination) - { - return encoding.GetBytes(c, value.Length, b, destination.Length); - } - } - } + public static unsafe int Write(Encoding encoding, string value, Span destination) => + encoding.GetBytes(value.AsSpan(), destination); private static bool TryDetect(ReadOnlySpan buffer, out CharacterCode code) { From 210945f93909ab903e08456ef51a964841127b43 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Thu, 14 Apr 2022 15:01:54 +0300 Subject: [PATCH 30/34] Fixed compilation error for older frameworks --- .../Profiles/Exif/ExifEncodedStringHelpers.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs b/src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs index 6e697029eb..4dc7f83985 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs @@ -79,8 +79,20 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif return CharacterCodeBytesLength + count; } - public static unsafe int Write(Encoding encoding, string value, Span destination) => - encoding.GetBytes(value.AsSpan(), destination); + public static unsafe int Write(Encoding encoding, string value, Span destination) +#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER || NET + => encoding.GetBytes(value.AsSpan(), destination); +#else + { + fixed (char* c = value) + { + fixed (byte* b = destination) + { + return encoding.GetBytes(c, value.Length, b, destination.Length); + } + } + } +#endif private static bool TryDetect(ReadOnlySpan buffer, out CharacterCode code) { From 55d01f231f840f3d57e997be43040e85fb3dc205 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Thu, 14 Apr 2022 15:03:59 +0300 Subject: [PATCH 31/34] Added guard clause of empty exif strings --- .../Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs b/src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs index 4dc7f83985..4ec9b3267e 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs @@ -84,6 +84,11 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif => encoding.GetBytes(value.AsSpan(), destination); #else { + if (value.Length == 0) + { + return 0; + } + fixed (char* c = value) { fixed (byte* b = destination) From d1c6e5042900dbbe8668e82e3213764453506b76 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Fri, 15 Apr 2022 12:08:58 +0300 Subject: [PATCH 32/34] Added test image --- .../Formats/Jpg/JpegDecoderTests.Images.cs | 1 + .../Formats/Jpg/JpegEncoderTests.Metadata.cs | 33 +++++++++++++++++++ .../Formats/Jpg/JpegEncoderTests.cs | 2 +- tests/ImageSharp.Tests/TestImages.cs | 1 + ...ssue2087-exif-null-reference-on-encode.jpg | 3 ++ 5 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.Metadata.cs create mode 100644 tests/Images/Input/Jpg/issues/Issue2087-exif-null-reference-on-encode.jpg diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs index 70cbc3af72..60439bc3d7 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs @@ -33,6 +33,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg TestImages.Jpeg.Issues.ExifGetString750Load, TestImages.Jpeg.Issues.ExifGetString750Transform, TestImages.Jpeg.Issues.BadSubSampling1076, + TestImages.Jpeg.Issues.ValidExifArgumentNullExceptionOnEncode, // LibJpeg can open this despite the invalid density units. TestImages.Jpeg.Issues.Fuzz.ArgumentOutOfRangeException825B, diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.Metadata.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.Metadata.cs new file mode 100644 index 0000000000..46ac23be12 --- /dev/null +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.Metadata.cs @@ -0,0 +1,33 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. + +using System; +using System.IO; +using SixLabors.ImageSharp.Formats.Jpeg; +using SixLabors.ImageSharp.PixelFormats; + +using Xunit; + +namespace SixLabors.ImageSharp.Tests.Formats.Jpg +{ + [Trait("Format", "Jpg")] + public partial class JpegEncoderTests + { + [Theory] + [WithFile(TestImages.Jpeg.Issues.ValidExifArgumentNullExceptionOnEncode, PixelTypes.Rgba32)] + public void Encode_WithValidExifProfile_DoesNotThrowException(TestImageProvider provider) + where TPixel : unmanaged, IPixel + { + Exception exception = Record.Exception(() => + { + var encoder = new JpegEncoder(); + var stream = new MemoryStream(); + + using Image image = provider.GetImage(JpegDecoder); + image.Save(stream, encoder); + }); + + Assert.Null(exception); + } + } +} diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs index 18eae9fbd3..d860836e08 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs @@ -20,7 +20,7 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Formats.Jpg { [Trait("Format", "Jpg")] - public class JpegEncoderTests + public partial class JpegEncoderTests { private static JpegEncoder JpegEncoder => new(); diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 07aff6fc12..4ea740266a 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -264,6 +264,7 @@ namespace SixLabors.ImageSharp.Tests public const string InvalidIptcTag = "Jpg/issues/Issue1942InvalidIptcTag.jpg"; public const string Issue2057App1Parsing = "Jpg/issues/Issue2057-App1Parsing.jpg"; public const string ExifNullArrayTag = "Jpg/issues/issue-2056-exif-null-array.jpg"; + public const string ValidExifArgumentNullExceptionOnEncode = "Jpg/issues/Issue2087-exif-null-reference-on-encode.jpg"; public static class Fuzz { diff --git a/tests/Images/Input/Jpg/issues/Issue2087-exif-null-reference-on-encode.jpg b/tests/Images/Input/Jpg/issues/Issue2087-exif-null-reference-on-encode.jpg new file mode 100644 index 0000000000..e95ef7a73d --- /dev/null +++ b/tests/Images/Input/Jpg/issues/Issue2087-exif-null-reference-on-encode.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4d41a41180a3371d0c4a724b40a4c86f6f975dab6be9da96964a484818770394 +size 30715 From cde4a88c22d461375aabc4dd632dd96c154b88e5 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Fri, 15 Apr 2022 12:13:57 +0300 Subject: [PATCH 33/34] Removed invalid image from test suite --- tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs | 1 - .../ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.Metadata.cs | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs index 60439bc3d7..70cbc3af72 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Images.cs @@ -33,7 +33,6 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg TestImages.Jpeg.Issues.ExifGetString750Load, TestImages.Jpeg.Issues.ExifGetString750Transform, TestImages.Jpeg.Issues.BadSubSampling1076, - TestImages.Jpeg.Issues.ValidExifArgumentNullExceptionOnEncode, // LibJpeg can open this despite the invalid density units. TestImages.Jpeg.Issues.Fuzz.ArgumentOutOfRangeException825B, diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.Metadata.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.Metadata.cs index 46ac23be12..2bbce6cb1b 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.Metadata.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.Metadata.cs @@ -18,7 +18,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg public void Encode_WithValidExifProfile_DoesNotThrowException(TestImageProvider provider) where TPixel : unmanaged, IPixel { - Exception exception = Record.Exception(() => + Exception ex = Record.Exception(() => { var encoder = new JpegEncoder(); var stream = new MemoryStream(); @@ -27,7 +27,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg image.Save(stream, encoder); }); - Assert.Null(exception); + Assert.Null(ex); } } } From 2ac18d816f0074f51a19983662594090516a0a7a Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 16 Apr 2022 18:39:49 +1000 Subject: [PATCH 34/34] Optimize tiff/jpeg checks --- .../Formats/Jpeg/JpegDecoderCore.cs | 35 ++++++++++--------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index a7f93b0e85..533ffa719c 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -228,16 +228,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg this.Metadata = new ImageMetadata(); this.QuantizationTables = new Block8x8F[4]; this.scanDecoder = huffmanScanDecoder; + + if (tableBytes.Length < 4) + { + JpegThrowHelper.ThrowInvalidImageContentException("Not enough data to read marker"); + } + using var ms = new MemoryStream(tableBytes); using var stream = new BufferedReadStream(this.Configuration, ms); // Check for the Start Of Image marker. int bytesRead = stream.Read(this.markerBuffer, 0, 2); - if (bytesRead != 2) - { - JpegThrowHelper.ThrowInvalidImageContentException("Not enough data to read the SOI marker"); - } - var fileMarker = new JpegFileMarker(this.markerBuffer[1], 0); if (fileMarker.Marker != JpegConstants.Markers.SOI) { @@ -246,20 +247,22 @@ namespace SixLabors.ImageSharp.Formats.Jpeg // Read next marker. bytesRead = stream.Read(this.markerBuffer, 0, 2); - if (bytesRead != 2) - { - JpegThrowHelper.ThrowInvalidImageContentException("Not enough data to read marker"); - } - - byte marker = this.markerBuffer[1]; - fileMarker = new JpegFileMarker(marker, (int)stream.Position - 2); + fileMarker = new JpegFileMarker(this.markerBuffer[1], (int)stream.Position - 2); while (fileMarker.Marker != JpegConstants.Markers.EOI || (fileMarker.Marker == JpegConstants.Markers.EOI && fileMarker.Invalid)) { if (!fileMarker.Invalid) { // Get the marker length. - int remaining = this.ReadUint16(stream) - 2; + int markerContentByteSize = this.ReadUint16(stream) - 2; + + // Check whether stream actually has enought bytes to read + // markerContentByteSize is always positive so we cast + // to uint to avoid sign extension + if (stream.RemainingBytes < (uint)markerContentByteSize) + { + JpegThrowHelper.ThrowNotEnoughBytesForMarker(fileMarker.Marker); + } switch (fileMarker.Marker) { @@ -269,13 +272,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg case JpegConstants.Markers.RST7: break; case JpegConstants.Markers.DHT: - this.ProcessDefineHuffmanTablesMarker(stream, remaining); + this.ProcessDefineHuffmanTablesMarker(stream, markerContentByteSize); break; case JpegConstants.Markers.DQT: - this.ProcessDefineQuantizationTablesMarker(stream, remaining); + this.ProcessDefineQuantizationTablesMarker(stream, markerContentByteSize); break; case JpegConstants.Markers.DRI: - this.ProcessDefineRestartIntervalMarker(stream, remaining); + this.ProcessDefineRestartIntervalMarker(stream, markerContentByteSize); break; case JpegConstants.Markers.EOI: return;