Skip to content

Commit 1557bae

Browse files
committed
dispose the next chunk
1 parent af7d84a commit 1557bae

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/ImageSharp/Formats/Png/PngDecoderCore.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,16 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
227227

228228
return image;
229229
}
230+
catch
231+
{
232+
image?.Dispose();
233+
throw;
234+
}
230235
finally
231236
{
232237
this.scanline?.Dispose();
233238
this.previousScanline?.Dispose();
239+
this.nextChunk?.Data?.Dispose();
234240
}
235241
}
236242

@@ -472,6 +478,8 @@ private void InitializeImage<TPixel>(ImageMetadata metadata, out Image<TPixel> i
472478
this.bytesPerSample = this.header.BitDepth / 8;
473479
}
474480

481+
this.previousScanline?.Dispose();
482+
this.scanline?.Dispose();
475483
this.previousScanline = this.memoryAllocator.Allocate<byte>(this.bytesPerScanline, AllocationOptions.Clean);
476484
this.scanline = this.Configuration.MemoryAllocator.Allocate<byte>(this.bytesPerScanline, AllocationOptions.Clean);
477485
}
@@ -1359,6 +1367,7 @@ private int ReadNextDataChunk()
13591367
{
13601368
if (chunk.Type == PngChunkType.Data)
13611369
{
1370+
chunk.Data?.Dispose();
13621371
return chunk.Length;
13631372
}
13641373

@@ -1453,6 +1462,9 @@ private void ValidateChunk(in PngChunk chunk)
14531462
if (validCrc != inputCrc)
14541463
{
14551464
string chunkTypeName = Encoding.ASCII.GetString(chunkType);
1465+
1466+
// ensure when throwing we dispose the data back to the memory allocator
1467+
chunk.Data?.Dispose();
14561468
PngThrowHelper.ThrowInvalidChunkCrc(chunkTypeName);
14571469
}
14581470
}

0 commit comments

Comments
 (0)