Skip to content

Commit af7d84a

Browse files
committed
check disposed buffers for decoders
1 parent 3872c60 commit af7d84a

File tree

15 files changed

+74
-40
lines changed

15 files changed

+74
-40
lines changed

src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,12 @@ public BmpDecoderCore(Configuration configuration, IBmpDecoderOptions options)
122122
public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken cancellationToken)
123123
where TPixel : unmanaged, IPixel<TPixel>
124124
{
125+
Image<TPixel> image = null;
125126
try
126127
{
127128
int bytesPerColorMapEntry = this.ReadImageHeaders(stream, out bool inverted, out byte[] palette);
128129

129-
var image = new Image<TPixel>(this.Configuration, this.infoHeader.Width, this.infoHeader.Height, this.metadata);
130+
image = new Image<TPixel>(this.Configuration, this.infoHeader.Width, this.infoHeader.Height, this.metadata);
130131

131132
Buffer2D<TPixel> pixels = image.GetRootFramePixelBuffer();
132133

@@ -193,8 +194,14 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
193194
}
194195
catch (IndexOutOfRangeException e)
195196
{
197+
image?.Dispose();
196198
throw new ImageFormatException("Bitmap does not have a valid format.", e);
197199
}
200+
catch
201+
{
202+
image?.Dispose();
203+
throw;
204+
}
198205
}
199206

200207
/// <inheritdoc />

src/ImageSharp/Formats/Gif/GifDecoderCore.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
151151
}
152152
}
153153
}
154+
//catch
155+
//{
156+
// image?.Dispose();
157+
// throw;
158+
//}
154159
finally
155160
{
156161
this.globalColorTable?.Dispose();

src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegTiffCompression.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ protected override void Decompress(BufferedReadStream stream, int byteCount, int
6565
jpegDecoder.ParseStream(stream, scanDecoderGray, CancellationToken.None);
6666

6767
// TODO: Should we pass through the CancellationToken from the tiff decoder?
68-
CopyImageBytesToBuffer(buffer, spectralConverterGray.GetPixelBuffer(CancellationToken.None));
68+
using var decompressedBuffer = spectralConverterGray.GetPixelBuffer(CancellationToken.None);
69+
CopyImageBytesToBuffer(buffer, decompressedBuffer);
6970
break;
7071
}
7172

@@ -81,7 +82,8 @@ protected override void Decompress(BufferedReadStream stream, int byteCount, int
8182
jpegDecoder.ParseStream(stream, scanDecoder, CancellationToken.None);
8283

8384
// TODO: Should we pass through the CancellationToken from the tiff decoder?
84-
CopyImageBytesToBuffer(buffer, spectralConverter.GetPixelBuffer(CancellationToken.None));
85+
using var decompressedBuffer = spectralConverter.GetPixelBuffer(CancellationToken.None);
86+
CopyImageBytesToBuffer(buffer, decompressedBuffer);
8587
break;
8688
}
8789

src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ private ImageFrame<TPixel> DecodeFrame<TPixel>(ExifProfile tags, CancellationTok
240240
var stripOffsetsArray = (Array)tags.GetValueInternal(ExifTag.StripOffsets).GetValue();
241241
var stripByteCountsArray = (Array)tags.GetValueInternal(ExifTag.StripByteCounts).GetValue();
242242

243-
IMemoryOwner<ulong> stripOffsetsMemory = this.ConvertNumbers(stripOffsetsArray, out Span<ulong> stripOffsets);
244-
IMemoryOwner<ulong> stripByteCountsMemory = this.ConvertNumbers(stripByteCountsArray, out Span<ulong> stripByteCounts);
243+
using IMemoryOwner<ulong> stripOffsetsMemory = this.ConvertNumbers(stripOffsetsArray, out Span<ulong> stripOffsets);
244+
using IMemoryOwner<ulong> stripByteCountsMemory = this.ConvertNumbers(stripByteCountsArray, out Span<ulong> stripByteCounts);
245245

246246
if (this.PlanarConfiguration == TiffPlanarConfiguration.Planar)
247247
{
@@ -262,8 +262,6 @@ private ImageFrame<TPixel> DecodeFrame<TPixel>(ExifProfile tags, CancellationTok
262262
cancellationToken);
263263
}
264264

265-
stripOffsetsMemory?.Dispose();
266-
stripByteCountsMemory?.Dispose();
267265
return frame;
268266
}
269267

src/ImageSharp/Formats/Webp/WebpDecoderCore.cs

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,38 +82,48 @@ public WebpDecoderCore(Configuration configuration, IWebpDecoderOptions options)
8282
public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken cancellationToken)
8383
where TPixel : unmanaged, IPixel<TPixel>
8484
{
85-
this.Metadata = new ImageMetadata();
86-
this.currentStream = stream;
85+
Image<TPixel> image = null;
86+
try
87+
{
8788

88-
uint fileSize = this.ReadImageHeader();
89+
this.Metadata = new ImageMetadata();
90+
this.currentStream = stream;
8991

90-
using (this.webImageInfo = this.ReadVp8Info())
91-
{
92-
if (this.webImageInfo.Features is { Animation: true })
93-
{
94-
WebpThrowHelper.ThrowNotSupportedException("Animations are not supported");
95-
}
92+
uint fileSize = this.ReadImageHeader();
9693

97-
var image = new Image<TPixel>(this.Configuration, (int)this.webImageInfo.Width, (int)this.webImageInfo.Height, this.Metadata);
98-
Buffer2D<TPixel> pixels = image.GetRootFramePixelBuffer();
99-
if (this.webImageInfo.IsLossless)
94+
using (this.webImageInfo = this.ReadVp8Info())
10095
{
101-
var losslessDecoder = new WebpLosslessDecoder(this.webImageInfo.Vp8LBitReader, this.memoryAllocator, this.Configuration);
102-
losslessDecoder.Decode(pixels, image.Width, image.Height);
103-
}
104-
else
105-
{
106-
var lossyDecoder = new WebpLossyDecoder(this.webImageInfo.Vp8BitReader, this.memoryAllocator, this.Configuration);
107-
lossyDecoder.Decode(pixels, image.Width, image.Height, this.webImageInfo);
108-
}
96+
if (this.webImageInfo.Features is { Animation: true })
97+
{
98+
WebpThrowHelper.ThrowNotSupportedException("Animations are not supported");
99+
}
109100

110-
// There can be optional chunks after the image data, like EXIF and XMP.
111-
if (this.webImageInfo.Features != null)
112-
{
113-
this.ParseOptionalChunks(this.webImageInfo.Features);
114-
}
101+
image = new Image<TPixel>(this.Configuration, (int)this.webImageInfo.Width, (int)this.webImageInfo.Height, this.Metadata);
102+
Buffer2D<TPixel> pixels = image.GetRootFramePixelBuffer();
103+
if (this.webImageInfo.IsLossless)
104+
{
105+
var losslessDecoder = new WebpLosslessDecoder(this.webImageInfo.Vp8LBitReader, this.memoryAllocator, this.Configuration);
106+
losslessDecoder.Decode(pixels, image.Width, image.Height);
107+
}
108+
else
109+
{
110+
var lossyDecoder = new WebpLossyDecoder(this.webImageInfo.Vp8BitReader, this.memoryAllocator, this.Configuration);
111+
lossyDecoder.Decode(pixels, image.Width, image.Height, this.webImageInfo);
112+
}
115113

116-
return image;
114+
// There can be optional chunks after the image data, like EXIF and XMP.
115+
if (this.webImageInfo.Features != null)
116+
{
117+
this.ParseOptionalChunks(this.webImageInfo.Features);
118+
}
119+
120+
return image;
121+
}
122+
}
123+
catch
124+
{
125+
image?.Dispose();
126+
throw;
117127
}
118128
}
119129

tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
namespace SixLabors.ImageSharp.Tests.Formats.Bmp
2121
{
2222
[Trait("Format", "Bmp")]
23+
[ValidateDisposedMemoryAllocations]
2324
public class BmpDecoderTests
2425
{
2526
public const PixelTypes CommonNonDefaultPixelTypes = PixelTypes.Rgba32 | PixelTypes.Bgra32 | PixelTypes.RgbaVector;

tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace SixLabors.ImageSharp.Tests.Formats.Gif
1919
{
2020
[Trait("Format", "Gif")]
21+
[ValidateDisposedMemoryAllocations]
2122
public class GifDecoderTests
2223
{
2324
private const PixelTypes TestPixelTypes = PixelTypes.Rgba32 | PixelTypes.RgbaVector | PixelTypes.Argb32;

tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,16 @@ private static void TestImageInfo(string imagePath, IImageDecoder decoder, bool
179179
var testFile = TestFile.Create(imagePath);
180180
using (var stream = new MemoryStream(testFile.Bytes, false))
181181
{
182-
IImageInfo imageInfo = useIdentify
183-
? ((IImageInfoDetector)decoder).Identify(Configuration.Default, stream, default)
184-
: decoder.Decode<Rgba32>(Configuration.Default, stream, default);
185-
186-
test(imageInfo);
182+
if (useIdentify)
183+
{
184+
IImageInfo imageInfo = ((IImageInfoDetector)decoder).Identify(Configuration.Default, stream, default);
185+
test(imageInfo);
186+
}
187+
else
188+
{
189+
using var img = decoder.Decode<Rgba32>(Configuration.Default, stream, default);
190+
test(img);
191+
}
187192
}
188193
}
189194

tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
2222
{
2323
// TODO: Scatter test cases into multiple test classes
2424
[Trait("Format", "Jpg")]
25+
[ValidateDisposedMemoryAllocations]
2526
public partial class JpegDecoderTests
2627
{
2728
public const PixelTypes CommonNonDefaultPixelTypes = PixelTypes.Rgba32 | PixelTypes.Argb32 | PixelTypes.Bgr24 | PixelTypes.RgbaVector;

tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace SixLabors.ImageSharp.Tests.Formats.Pbm
1212
{
1313
[Trait("Format", "Pbm")]
14+
[ValidateDisposedMemoryAllocations]
1415
public class PbmDecoderTests
1516
{
1617
[Theory]

0 commit comments

Comments
 (0)