diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml
index b2179fbce5..b20878b18f 100644
--- a/.github/workflows/build-and-test.yml
+++ b/.github/workflows/build-and-test.yml
@@ -192,9 +192,15 @@ jobs:
shell: pwsh
run: ./ci-pack.ps1
- - name: MyGet Publish
+ - name: Feedz Publish
shell: pwsh
run: |
- dotnet nuget push .\artifacts\*.nupkg -k ${{secrets.MYGET_TOKEN}} -s https://www.myget.org/F/sixlabors/api/v2/package
- dotnet nuget push .\artifacts\*.snupkg -k ${{secrets.MYGET_TOKEN}} -s https://www.myget.org/F/sixlabors/api/v3/index.json
- # TODO: If github.ref starts with 'refs/tags' then it was tag push and we can optionally push out package to nuget.org
+ dotnet nuget push .\artifacts\*.nupkg -k ${{secrets.FEEDZ_TOKEN}} -s https://f.feedz.io/sixlabors/sixlabors/nuget/index.json --skip-duplicate
+ dotnet nuget push .\artifacts\*.snupkg -k ${{secrets.FEEDZ_TOKEN}} -s https://f.feedz.io/sixlabors/sixlabors/symbols --skip-duplicate
+ - name: NuGet Publish
+ if: ${{ startsWith(github.ref, 'refs/tags/') }}
+ shell: pwsh
+ run: |
+ dotnet nuget push .\artifacts\*.nupkg -k ${{secrets.NUGET_TOKEN}} -s https://api.nuget.org/v3/index.json --skip-duplicate
+ dotnet nuget push .\artifacts\*.snupkg -k ${{secrets.NUGET_TOKEN}} -s https://api.nuget.org/v3/index.json --skip-duplicate
+
diff --git a/src/Directory.Build.props b/src/Directory.Build.props
index faa29865f2..904d404f50 100644
--- a/src/Directory.Build.props
+++ b/src/Directory.Build.props
@@ -18,7 +18,7 @@
- true
+
diff --git a/src/ImageSharp/Advanced/ParallelRowIterator.cs b/src/ImageSharp/Advanced/ParallelRowIterator.cs
index e787b7cfc5..06fbe731d1 100644
--- a/src/ImageSharp/Advanced/ParallelRowIterator.cs
+++ b/src/ImageSharp/Advanced/ParallelRowIterator.cs
@@ -52,7 +52,7 @@ public static void IterateRows(
int width = rectangle.Width;
int height = rectangle.Height;
- int maxSteps = DivideCeil(width * height, parallelSettings.MinimumPixelsProcessedPerTask);
+ int maxSteps = DivideCeil(width * (long)height, parallelSettings.MinimumPixelsProcessedPerTask);
int numOfSteps = Math.Min(parallelSettings.MaxDegreeOfParallelism, maxSteps);
// Avoid TPL overhead in this trivial case:
@@ -117,7 +117,7 @@ public static void IterateRows(
int width = rectangle.Width;
int height = rectangle.Height;
- int maxSteps = DivideCeil(width * height, parallelSettings.MinimumPixelsProcessedPerTask);
+ int maxSteps = DivideCeil(width * (long)height, parallelSettings.MinimumPixelsProcessedPerTask);
int numOfSteps = Math.Min(parallelSettings.MaxDegreeOfParallelism, maxSteps);
MemoryAllocator allocator = parallelSettings.MemoryAllocator;
@@ -181,7 +181,7 @@ public static void IterateRowIntervals(
int width = rectangle.Width;
int height = rectangle.Height;
- int maxSteps = DivideCeil(width * height, parallelSettings.MinimumPixelsProcessedPerTask);
+ int maxSteps = DivideCeil(width * (long)height, parallelSettings.MinimumPixelsProcessedPerTask);
int numOfSteps = Math.Min(parallelSettings.MaxDegreeOfParallelism, maxSteps);
// Avoid TPL overhead in this trivial case:
@@ -243,7 +243,7 @@ public static void IterateRowIntervals(
int width = rectangle.Width;
int height = rectangle.Height;
- int maxSteps = DivideCeil(width * height, parallelSettings.MinimumPixelsProcessedPerTask);
+ int maxSteps = DivideCeil(width * (long)height, parallelSettings.MinimumPixelsProcessedPerTask);
int numOfSteps = Math.Min(parallelSettings.MaxDegreeOfParallelism, maxSteps);
MemoryAllocator allocator = parallelSettings.MemoryAllocator;
@@ -270,7 +270,7 @@ public static void IterateRowIntervals(
}
[MethodImpl(InliningOptions.ShortMethod)]
- private static int DivideCeil(int dividend, int divisor) => 1 + ((dividend - 1) / divisor);
+ private static int DivideCeil(long dividend, int divisor) => (int)Math.Min(1 + ((dividend - 1) / divisor), int.MaxValue);
private static void ValidateRectangle(Rectangle rectangle)
{
diff --git a/src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs b/src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs
index f4b0543b84..bb97ff79eb 100644
--- a/src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs
+++ b/src/ImageSharp/Compression/Zlib/ZlibInflateStream.cs
@@ -161,6 +161,11 @@ public override int Read(byte[] buffer, int offset, int count)
bytesToRead = Math.Min(count - totalBytesRead, this.currentDataRemaining);
this.currentDataRemaining -= bytesToRead;
bytesRead = this.innerStream.Read(buffer, offset, bytesToRead);
+ if (bytesRead == 0)
+ {
+ return totalBytesRead;
+ }
+
totalBytesRead += bytesRead;
}
@@ -168,22 +173,13 @@ public override int Read(byte[] buffer, int offset, int count)
}
///
- public override long Seek(long offset, SeekOrigin origin)
- {
- throw new NotSupportedException();
- }
+ public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
///
- public override void SetLength(long value)
- {
- throw new NotSupportedException();
- }
+ public override void SetLength(long value) => throw new NotSupportedException();
///
- public override void Write(byte[] buffer, int offset, int count)
- {
- throw new NotSupportedException();
- }
+ public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
///
protected override void Dispose(bool disposing)
@@ -245,22 +241,17 @@ private bool InitializeInflateStream(bool isCriticalChunk)
// CINFO is not defined in this specification for CM not equal to 8.
throw new ImageFormatException($"Invalid window size for ZLIB header: cinfo={cinfo}");
}
- else
- {
- return false;
- }
+
+ return false;
}
}
+ else if (isCriticalChunk)
+ {
+ throw new ImageFormatException($"Bad method for ZLIB header: cmf={cmf}");
+ }
else
{
- if (isCriticalChunk)
- {
- throw new ImageFormatException($"Bad method for ZLIB header: cmf={cmf}");
- }
- else
- {
- return false;
- }
+ return false;
}
// The preset dictionary.
@@ -269,7 +260,11 @@ private bool InitializeInflateStream(bool isCriticalChunk)
{
// We don't need this for inflate so simply skip by the next four bytes.
// https://tools.ietf.org/html/rfc1950#page-6
- this.innerStream.Read(ChecksumBuffer, 0, 4);
+ if (this.innerStream.Read(ChecksumBuffer, 0, 4) != 4)
+ {
+ return false;
+ }
+
this.currentDataRemaining -= 4;
}
diff --git a/src/ImageSharp/Formats/ImageDecoderUtilities.cs b/src/ImageSharp/Formats/ImageDecoderUtilities.cs
index 71ecda8938..a2bbe34058 100644
--- a/src/ImageSharp/Formats/ImageDecoderUtilities.cs
+++ b/src/ImageSharp/Formats/ImageDecoderUtilities.cs
@@ -46,7 +46,8 @@ public static Image Decode(
CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel
{
- using var bufferedReadStream = new BufferedReadStream(configuration, stream);
+ // Test may pass a BufferedReadStream in order to monitor EOF hits, if so, use the existing instance.
+ BufferedReadStream bufferedReadStream = stream as BufferedReadStream ?? new BufferedReadStream(configuration, stream);
try
{
@@ -56,6 +57,13 @@ public static Image Decode(
{
throw largeImageExceptionFactory(ex, decoder.Dimensions);
}
+ finally
+ {
+ if (bufferedReadStream != stream)
+ {
+ bufferedReadStream.Dispose();
+ }
+ }
}
private static InvalidImageContentException DefaultLargeImageExceptionFactory(
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanBuffer.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanBuffer.cs
index 3664cb4eb3..39e606fd39 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanBuffer.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanScanBuffer.cs
@@ -211,7 +211,12 @@ public bool FindNextMarker()
private int ReadStream()
{
int value = this.badData ? 0 : this.stream.ReadByte();
- if (value == -1)
+
+ // We've encountered the end of the file stream which means there's no EOI marker or the marker has been read
+ // during decoding of the SOS marker.
+ // When reading individual bits 'badData' simply means we have hit a marker, When data is '0' and the stream is exhausted
+ // we know we have hit the EOI and completed decoding the scan buffer.
+ if (value == -1 || (this.badData && this.data == 0 && this.stream.Position >= this.stream.Length))
{
// We've encountered the end of the file stream which means there's no EOI marker
// in the image or the SOS marker has the wrong dimensions set.
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs
index 532892e060..220bc16798 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs
@@ -116,7 +116,8 @@ public override void InjectFrameData(JpegFrame frame, IRawJpegData jpegData)
this.pixelBuffer = allocator.Allocate2D(
frame.PixelWidth,
frame.PixelHeight,
- this.configuration.PreferContiguousImageBuffers);
+ this.configuration.PreferContiguousImageBuffers,
+ AllocationOptions.Clean);
this.paddedProxyPixelRow = allocator.Allocate(frame.PixelWidth + 3);
// component processors from spectral to Rgba32
@@ -215,4 +216,4 @@ public void Dispose()
this.pixelBuffer?.Dispose();
}
}
-}
+}
\ No newline at end of file
diff --git a/src/ImageSharp/Formats/Pbm/BinaryDecoder.cs b/src/ImageSharp/Formats/Pbm/BinaryDecoder.cs
index 33af30434c..25563d2d95 100644
--- a/src/ImageSharp/Formats/Pbm/BinaryDecoder.cs
+++ b/src/ImageSharp/Formats/Pbm/BinaryDecoder.cs
@@ -72,7 +72,11 @@ private static void ProcessGrayscale(Configuration configuration, Buffer
for (int y = 0; y < height; y++)
{
- stream.Read(rowSpan);
+ if (stream.Read(rowSpan) < rowSpan.Length)
+ {
+ return;
+ }
+
Span pixelSpan = pixels.DangerousGetRowSpan(y);
PixelOperations.Instance.FromL8Bytes(
configuration,
@@ -94,7 +98,11 @@ private static void ProcessWideGrayscale(Configuration configuration, Bu
for (int y = 0; y < height; y++)
{
- stream.Read(rowSpan);
+ if (stream.Read(rowSpan) < rowSpan.Length)
+ {
+ return;
+ }
+
Span pixelSpan = pixels.DangerousGetRowSpan(y);
PixelOperations.Instance.FromL16Bytes(
configuration,
@@ -116,7 +124,11 @@ private static void ProcessRgb(Configuration configuration, Buffer2D pixelSpan = pixels.DangerousGetRowSpan(y);
PixelOperations.Instance.FromRgb24Bytes(
configuration,
@@ -138,7 +150,11 @@ private static void ProcessWideRgb(Configuration configuration, Buffer2D
for (int y = 0; y < height; y++)
{
- stream.Read(rowSpan);
+ if (stream.Read(rowSpan) < rowSpan.Length)
+ {
+ return;
+ }
+
Span pixelSpan = pixels.DangerousGetRowSpan(y);
PixelOperations.Instance.FromRgb48Bytes(
configuration,
@@ -153,7 +169,6 @@ private static void ProcessBlackAndWhite(Configuration configuration, Bu
{
int width = pixels.Width;
int height = pixels.Height;
- int startBit = 0;
MemoryAllocator allocator = configuration.MemoryAllocator;
using IMemoryOwner row = allocator.Allocate(width);
Span rowSpan = row.GetSpan();
@@ -163,23 +178,17 @@ private static void ProcessBlackAndWhite(Configuration configuration, Bu
for (int x = 0; x < width;)
{
int raw = stream.ReadByte();
- int bit = startBit;
- startBit = 0;
- for (; bit < 8; bit++)
+ if (raw < 0)
+ {
+ return;
+ }
+
+ int stopBit = Math.Min(8, width - x);
+ for (int bit = 0; bit < stopBit; bit++)
{
bool bitValue = (raw & (0x80 >> bit)) != 0;
rowSpan[x] = bitValue ? black : white;
x++;
- if (x == width)
- {
- startBit = (bit + 1) & 7; // Round off to below 8.
- if (startBit != 0)
- {
- stream.Seek(-1, System.IO.SeekOrigin.Current);
- }
-
- break;
- }
}
}
diff --git a/src/ImageSharp/Formats/Pbm/BufferedReadStreamExtensions.cs b/src/ImageSharp/Formats/Pbm/BufferedReadStreamExtensions.cs
index 581d3e592b..94468f90aa 100644
--- a/src/ImageSharp/Formats/Pbm/BufferedReadStreamExtensions.cs
+++ b/src/ImageSharp/Formats/Pbm/BufferedReadStreamExtensions.cs
@@ -12,14 +12,20 @@ namespace SixLabors.ImageSharp.Formats.Pbm
internal static class BufferedReadStreamExtensions
{
///
- /// Skip over any whitespace or any comments.
+ /// Skip over any whitespace or any comments and signal if EOF has been reached.
///
- public static void SkipWhitespaceAndComments(this BufferedReadStream stream)
+ /// The buffered read stream.
+ /// if EOF has been reached while reading the stream; see langword="true"/> otherwise.
+ public static bool SkipWhitespaceAndComments(this BufferedReadStream stream)
{
bool isWhitespace;
do
{
int val = stream.ReadByte();
+ if (val < 0)
+ {
+ return false;
+ }
// Comments start with '#' and end at the next new-line.
if (val == 0x23)
@@ -28,8 +34,12 @@ public static void SkipWhitespaceAndComments(this BufferedReadStream stream)
do
{
innerValue = stream.ReadByte();
+ if (innerValue < 0)
+ {
+ return false;
+ }
}
- while (innerValue != 0x0a);
+ while (innerValue is not 0x0a);
// Continue searching for whitespace.
val = innerValue;
@@ -39,18 +49,31 @@ public static void SkipWhitespaceAndComments(this BufferedReadStream stream)
}
while (isWhitespace);
stream.Seek(-1, SeekOrigin.Current);
+ return true;
}
///
- /// Read a decimal text value.
+ /// Read a decimal text value and signal if EOF has been reached.
///
- /// The integer value of the decimal.
- public static int ReadDecimal(this BufferedReadStream stream)
+ /// The buffered read stream.
+ /// The read value.
+ /// if EOF has been reached while reading the stream; otherwise.
+ ///
+ /// A 'false' return value doesn't mean that the parsing has been failed, since it's possible to reach EOF while reading the last decimal in the file.
+ /// It's up to the call site to handle such a situation.
+ ///
+ public static bool ReadDecimal(this BufferedReadStream stream, out int value)
{
- int value = 0;
+ value = 0;
while (true)
{
- int current = stream.ReadByte() - 0x30;
+ int current = stream.ReadByte();
+ if (current < 0)
+ {
+ return false;
+ }
+
+ current -= 0x30;
if ((uint)current > 9)
{
break;
@@ -59,7 +82,7 @@ public static int ReadDecimal(this BufferedReadStream stream)
value = (value * 10) + current;
}
- return value;
+ return true;
}
}
}
diff --git a/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs b/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs
index 749fc0292b..ccd5041239 100644
--- a/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs
+++ b/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs
@@ -90,6 +90,7 @@ public IImageInfo Identify(BufferedReadStream stream, CancellationToken cancella
/// Processes the ppm header.
///
/// The input stream.
+ /// An EOF marker has been read before the image has been decoded.
private void ProcessHeader(BufferedReadStream stream)
{
Span buffer = stackalloc byte[2];
@@ -139,14 +140,22 @@ private void ProcessHeader(BufferedReadStream stream)
throw new InvalidImageContentException("Unknown of not implemented image type encountered.");
}
- stream.SkipWhitespaceAndComments();
- int width = stream.ReadDecimal();
- stream.SkipWhitespaceAndComments();
- int height = stream.ReadDecimal();
- stream.SkipWhitespaceAndComments();
+ if (!stream.SkipWhitespaceAndComments() ||
+ !stream.ReadDecimal(out int width) ||
+ !stream.SkipWhitespaceAndComments() ||
+ !stream.ReadDecimal(out int height) ||
+ !stream.SkipWhitespaceAndComments())
+ {
+ ThrowPrematureEof();
+ }
+
if (this.ColorType != PbmColorType.BlackAndWhite)
{
- this.maxPixelValue = stream.ReadDecimal();
+ if (!stream.ReadDecimal(out this.maxPixelValue))
+ {
+ ThrowPrematureEof();
+ }
+
if (this.maxPixelValue > 255)
{
this.ComponentType = PbmComponentType.Short;
@@ -169,6 +178,8 @@ private void ProcessHeader(BufferedReadStream stream)
meta.Encoding = this.Encoding;
meta.ColorType = this.ColorType;
meta.ComponentType = this.ComponentType;
+
+ static void ThrowPrematureEof() => throw new InvalidImageContentException("Reached EOF while reading the header.");
}
private void ProcessPixels(BufferedReadStream stream, Buffer2D pixels)
diff --git a/src/ImageSharp/Formats/Pbm/PlainDecoder.cs b/src/ImageSharp/Formats/Pbm/PlainDecoder.cs
index aeb527dd20..f5e0378cea 100644
--- a/src/ImageSharp/Formats/Pbm/PlainDecoder.cs
+++ b/src/ImageSharp/Formats/Pbm/PlainDecoder.cs
@@ -66,13 +66,18 @@ private static void ProcessGrayscale(Configuration configuration, Buffer
using IMemoryOwner row = allocator.Allocate(width);
Span rowSpan = row.GetSpan();
+ bool eofReached = false;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
- byte value = (byte)stream.ReadDecimal();
- stream.SkipWhitespaceAndComments();
- rowSpan[x] = new L8(value);
+ stream.ReadDecimal(out int value);
+ rowSpan[x] = new L8((byte)value);
+ eofReached = !stream.SkipWhitespaceAndComments();
+ if (eofReached)
+ {
+ break;
+ }
}
Span pixelSpan = pixels.DangerousGetRowSpan(y);
@@ -80,6 +85,11 @@ private static void ProcessGrayscale(Configuration configuration, Buffer
configuration,
rowSpan,
pixelSpan);
+
+ if (eofReached)
+ {
+ return;
+ }
}
}
@@ -92,13 +102,18 @@ private static void ProcessWideGrayscale(Configuration configuration, Bu
using IMemoryOwner row = allocator.Allocate(width);
Span rowSpan = row.GetSpan();
+ bool eofReached = false;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
- ushort value = (ushort)stream.ReadDecimal();
- stream.SkipWhitespaceAndComments();
- rowSpan[x] = new L16(value);
+ stream.ReadDecimal(out int value);
+ rowSpan[x] = new L16((ushort)value);
+ eofReached = !stream.SkipWhitespaceAndComments();
+ if (eofReached)
+ {
+ break;
+ }
}
Span pixelSpan = pixels.DangerousGetRowSpan(y);
@@ -106,6 +121,11 @@ private static void ProcessWideGrayscale(Configuration configuration, Bu
configuration,
rowSpan,
pixelSpan);
+
+ if (eofReached)
+ {
+ return;
+ }
}
}
@@ -118,17 +138,29 @@ private static void ProcessRgb(Configuration configuration, Buffer2D row = allocator.Allocate(width);
Span rowSpan = row.GetSpan();
+ bool eofReached = false;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
- byte red = (byte)stream.ReadDecimal();
- stream.SkipWhitespaceAndComments();
- byte green = (byte)stream.ReadDecimal();
- stream.SkipWhitespaceAndComments();
- byte blue = (byte)stream.ReadDecimal();
- stream.SkipWhitespaceAndComments();
- rowSpan[x] = new Rgb24(red, green, blue);
+ if (!stream.ReadDecimal(out int red) ||
+ !stream.SkipWhitespaceAndComments() ||
+ !stream.ReadDecimal(out int green) ||
+ !stream.SkipWhitespaceAndComments())
+ {
+ // Reached EOF before reading a full RGB value
+ eofReached = true;
+ break;
+ }
+
+ stream.ReadDecimal(out int blue);
+
+ rowSpan[x] = new Rgb24((byte)red, (byte)green, (byte)blue);
+ eofReached = !stream.SkipWhitespaceAndComments();
+ if (eofReached)
+ {
+ break;
+ }
}
Span pixelSpan = pixels.DangerousGetRowSpan(y);
@@ -136,6 +168,11 @@ private static void ProcessRgb(Configuration configuration, Buffer2D(Configuration configuration, Buffer2D
using IMemoryOwner row = allocator.Allocate(width);
Span rowSpan = row.GetSpan();
+ bool eofReached = false;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
- ushort red = (ushort)stream.ReadDecimal();
- stream.SkipWhitespaceAndComments();
- ushort green = (ushort)stream.ReadDecimal();
- stream.SkipWhitespaceAndComments();
- ushort blue = (ushort)stream.ReadDecimal();
- stream.SkipWhitespaceAndComments();
- rowSpan[x] = new Rgb48(red, green, blue);
+ if (!stream.ReadDecimal(out int red) ||
+ !stream.SkipWhitespaceAndComments() ||
+ !stream.ReadDecimal(out int green) ||
+ !stream.SkipWhitespaceAndComments())
+ {
+ // Reached EOF before reading a full RGB value
+ eofReached = true;
+ break;
+ }
+
+ stream.ReadDecimal(out int blue);
+
+ rowSpan[x] = new Rgb48((ushort)red, (ushort)green, (ushort)blue);
+ eofReached = !stream.SkipWhitespaceAndComments();
+ if (eofReached)
+ {
+ break;
+ }
}
Span pixelSpan = pixels.DangerousGetRowSpan(y);
@@ -166,6 +215,11 @@ private static void ProcessWideRgb(Configuration configuration, Buffer2D
configuration,
rowSpan,
pixelSpan);
+
+ if (eofReached)
+ {
+ return;
+ }
}
}
@@ -178,13 +232,19 @@ private static void ProcessBlackAndWhite(Configuration configuration, Bu
using IMemoryOwner row = allocator.Allocate(width);
Span rowSpan = row.GetSpan();
+ bool eofReached = false;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
- int value = stream.ReadDecimal();
- stream.SkipWhitespaceAndComments();
+ stream.ReadDecimal(out int value);
+
rowSpan[x] = value == 0 ? White : Black;
+ eofReached = !stream.SkipWhitespaceAndComments();
+ if (eofReached)
+ {
+ break;
+ }
}
Span pixelSpan = pixels.DangerousGetRowSpan(y);
@@ -192,6 +252,11 @@ private static void ProcessBlackAndWhite(Configuration configuration, Bu
configuration,
rowSpan,
pixelSpan);
+
+ if (eofReached)
+ {
+ return;
+ }
}
}
}
diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs
index 12770bc521..d6c3256e06 100644
--- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs
+++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs
@@ -464,7 +464,7 @@ private void ReadGammaChunk(PngMetadata pngMetadata, ReadOnlySpan data)
private void InitializeImage(ImageMetadata metadata, out Image image)
where TPixel : unmanaged, IPixel
{
- image = Image.CreateUninitialized(
+ image = new Image(
this.Configuration,
this.header.Width,
this.header.Height,
@@ -1504,6 +1504,9 @@ private void SkipChunkDataAndCrc(in PngChunk chunk)
private IMemoryOwner ReadChunkData(int length)
{
// We rent the buffer here to return it afterwards in Decode()
+ // We don't want to throw a degenerated memory exception here as we want to allow partial decoding
+ // so limit the length.
+ length = (int)Math.Min(length, this.currentStream.Length - this.currentStream.Position);
IMemoryOwner buffer = this.Configuration.MemoryAllocator.Allocate(length, AllocationOptions.Clean);
this.currentStream.Read(buffer.GetSpan(), 0, length);
diff --git a/src/ImageSharp/Formats/Png/PngScanlineProcessor.cs b/src/ImageSharp/Formats/Png/PngScanlineProcessor.cs
index 26bc566d65..afb89836c0 100644
--- a/src/ImageSharp/Formats/Png/PngScanlineProcessor.cs
+++ b/src/ImageSharp/Formats/Png/PngScanlineProcessor.cs
@@ -250,6 +250,7 @@ public static void ProcessPaletteScanline(
ref TPixel rowSpanRef = ref MemoryMarshal.GetReference(rowSpan);
ReadOnlySpan palettePixels = MemoryMarshal.Cast(palette);
ref Rgb24 palettePixelsRef = ref MemoryMarshal.GetReference(palettePixels);
+ int maxIndex = palettePixels.Length - 1;
if (paletteAlpha?.Length > 0)
{
@@ -260,7 +261,7 @@ public static void ProcessPaletteScanline(
for (int x = 0; x < header.Width; x++)
{
- int index = Unsafe.Add(ref scanlineSpanRef, x);
+ int index = Numerics.Clamp(Unsafe.Add(ref scanlineSpanRef, x), 0, maxIndex);
rgba.Rgb = Unsafe.Add(ref palettePixelsRef, index);
rgba.A = paletteAlpha.Length > index ? Unsafe.Add(ref paletteAlphaRef, index) : byte.MaxValue;
@@ -272,8 +273,8 @@ public static void ProcessPaletteScanline(
{
for (int x = 0; x < header.Width; x++)
{
- int index = Unsafe.Add(ref scanlineSpanRef, x);
- Rgb24 rgb = Unsafe.Add(ref palettePixelsRef, index);
+ uint index = Unsafe.Add(ref scanlineSpanRef, x);
+ Rgb24 rgb = Unsafe.Add(ref palettePixelsRef, (int)Math.Min(index, maxIndex));
pixel.FromRgb24(rgb);
Unsafe.Add(ref rowSpanRef, x) = pixel;
diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs
index d101ccd94a..523ead871e 100644
--- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs
+++ b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs
@@ -97,7 +97,7 @@ public Image Decode(BufferedReadStream stream, CancellationToken
throw new UnknownImageFormatException("Width or height cannot be 0");
}
- var image = Image.CreateUninitialized(this.Configuration, this.fileHeader.Width, this.fileHeader.Height, this.metadata);
+ Image image = new(this.Configuration, this.fileHeader.Width, this.fileHeader.Height, this.metadata);
Buffer2D pixels = image.GetRootFramePixelBuffer();
if (this.fileHeader.ColorMapType == 1)
diff --git a/src/ImageSharp/Formats/Tiff/Ifd/DirectoryReader.cs b/src/ImageSharp/Formats/Tiff/Ifd/DirectoryReader.cs
index cce1d278cc..1d7592679c 100644
--- a/src/ImageSharp/Formats/Tiff/Ifd/DirectoryReader.cs
+++ b/src/ImageSharp/Formats/Tiff/Ifd/DirectoryReader.cs
@@ -43,7 +43,7 @@ public DirectoryReader(Stream stream, MemoryAllocator allocator)
public IEnumerable Read()
{
this.ByteOrder = ReadByteOrder(this.stream);
- var headerReader = new HeaderReader(this.stream, this.ByteOrder);
+ HeaderReader headerReader = new(this.stream, this.ByteOrder);
headerReader.ReadFileHeader();
this.nextIfdOffset = headerReader.FirstIfdOffset;
@@ -55,7 +55,12 @@ public IEnumerable Read()
private static ByteOrder ReadByteOrder(Stream stream)
{
Span headerBytes = stackalloc byte[2];
- stream.Read(headerBytes);
+
+ if (stream.Read(headerBytes) != 2)
+ {
+ throw TiffThrowHelper.ThrowInvalidHeader();
+ }
+
if (headerBytes[0] == TiffConstants.ByteOrderLittleEndian && headerBytes[1] == TiffConstants.ByteOrderLittleEndian)
{
return ByteOrder.LittleEndian;
@@ -74,7 +79,7 @@ private IEnumerable ReadIfds(bool isBigTiff)
var readers = new List();
while (this.nextIfdOffset != 0 && this.nextIfdOffset < (ulong)this.stream.Length)
{
- var reader = new EntryReader(this.stream, this.ByteOrder, this.allocator);
+ EntryReader reader = new(this.stream, this.ByteOrder, this.allocator);
reader.ReadTags(isBigTiff, this.nextIfdOffset);
if (reader.BigValues.Count > 0)
@@ -88,6 +93,11 @@ private IEnumerable ReadIfds(bool isBigTiff)
}
}
+ if (this.nextIfdOffset >= reader.NextIfdOffset && reader.NextIfdOffset != 0)
+ {
+ TiffThrowHelper.ThrowImageFormatException("TIFF image contains circular directory offsets");
+ }
+
this.nextIfdOffset = reader.NextIfdOffset;
readers.Add(reader);
diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs
index 695359e5ea..37cb9a4fc4 100644
--- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs
+++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs
@@ -329,60 +329,68 @@ public void Encode(Image image, Stream stream)
bool alphaCompressionSucceeded = false;
using var alphaEncoder = new AlphaEncoder();
Span alphaData = Span.Empty;
- if (hasAlpha)
+ IMemoryOwner encodedAlphaData = null;
+ try
{
- // TODO: This can potentially run in an separate task.
- IMemoryOwner encodedAlphaData = alphaEncoder.EncodeAlpha(image, this.configuration, this.memoryAllocator, this.alphaCompression, out alphaDataSize);
- alphaData = encodedAlphaData.GetSpan();
- if (alphaDataSize < pixelCount)
+ if (hasAlpha)
{
- // Only use compressed data, if the compressed data is actually smaller then the uncompressed data.
- alphaCompressionSucceeded = true;
+ // TODO: This can potentially run in an separate task.
+ encodedAlphaData = alphaEncoder.EncodeAlpha(image, this.configuration, this.memoryAllocator, this.alphaCompression, out alphaDataSize);
+ alphaData = encodedAlphaData.GetSpan();
+ if (alphaDataSize < pixelCount)
+ {
+ // Only use compressed data, if the compressed data is actually smaller then the uncompressed data.
+ alphaCompressionSucceeded = true;
+ }
}
- }
- // Stats-collection loop.
- this.StatLoop(width, height, yStride, uvStride);
- it.Init();
- it.InitFilter();
- var info = new Vp8ModeScore();
- var residual = new Vp8Residual();
- do
- {
- bool dontUseSkip = !this.Proba.UseSkipProba;
- info.Clear();
- it.Import(y, u, v, yStride, uvStride, width, height, false);
-
- // Warning! order is important: first call VP8Decimate() and
- // *then* decide how to code the skip decision if there's one.
- if (!this.Decimate(it, ref info, this.rdOptLevel) || dontUseSkip)
- {
- this.CodeResiduals(it, info, residual);
- }
- else
+ // Stats-collection loop.
+ this.StatLoop(width, height, yStride, uvStride);
+ it.Init();
+ it.InitFilter();
+ var info = new Vp8ModeScore();
+ var residual = new Vp8Residual();
+ do
{
- it.ResetAfterSkip();
+ bool dontUseSkip = !this.Proba.UseSkipProba;
+ info.Clear();
+ it.Import(y, u, v, yStride, uvStride, width, height, false);
+
+ // Warning! order is important: first call VP8Decimate() and
+ // *then* decide how to code the skip decision if there's one.
+ if (!this.Decimate(it, ref info, this.rdOptLevel) || dontUseSkip)
+ {
+ this.CodeResiduals(it, info, residual);
+ }
+ else
+ {
+ it.ResetAfterSkip();
+ }
+
+ it.SaveBoundary();
}
+ while (it.Next());
- it.SaveBoundary();
+ // Store filter stats.
+ this.AdjustFilterStrength();
+
+ // Write bytes from the bitwriter buffer to the stream.
+ ImageMetadata metadata = image.Metadata;
+ metadata.SyncProfiles();
+ this.bitWriter.WriteEncodedImageToStream(
+ stream,
+ metadata.ExifProfile,
+ metadata.XmpProfile,
+ (uint)width,
+ (uint)height,
+ hasAlpha,
+ alphaData.Slice(0, alphaDataSize),
+ this.alphaCompression && alphaCompressionSucceeded);
+ }
+ finally
+ {
+ encodedAlphaData?.Dispose();
}
- while (it.Next());
-
- // Store filter stats.
- this.AdjustFilterStrength();
-
- // Write bytes from the bitwriter buffer to the stream.
- ImageMetadata metadata = image.Metadata;
- metadata.SyncProfiles();
- this.bitWriter.WriteEncodedImageToStream(
- stream,
- metadata.ExifProfile,
- metadata.XmpProfile,
- (uint)width,
- (uint)height,
- hasAlpha,
- alphaData,
- this.alphaCompression && alphaCompressionSucceeded);
}
///
diff --git a/src/ImageSharp/IO/BufferedReadStream.cs b/src/ImageSharp/IO/BufferedReadStream.cs
index 2823b8ed6f..28103359e6 100644
--- a/src/ImageSharp/IO/BufferedReadStream.cs
+++ b/src/ImageSharp/IO/BufferedReadStream.cs
@@ -65,6 +65,11 @@ public BufferedReadStream(Configuration configuration, Stream stream)
this.readBufferIndex = this.BufferSize;
}
+ ///
+ /// Gets the number indicating the EOF hits occured while reading from this instance.
+ ///
+ public int EofHitCount { get; private set; }
+
///
/// Gets the size, in bytes, of the underlying buffer.
///
@@ -138,6 +143,7 @@ public override int ReadByte()
{
if (this.readerPosition >= this.Length)
{
+ this.EofHitCount++;
return -1;
}
@@ -303,7 +309,7 @@ private int ReadToBufferViaCopyFast(Span buffer)
this.readerPosition += n;
this.readBufferIndex += n;
-
+ this.CheckEof(n);
return n;
}
@@ -361,6 +367,7 @@ private int ReadToBufferDirectSlow(Span buffer)
this.Position += n;
+ this.CheckEof(n);
return n;
}
@@ -427,5 +434,14 @@ private unsafe void CopyBytes(byte[] buffer, int offset, int count)
Buffer.BlockCopy(this.readBuffer, this.readBufferIndex, buffer, offset, count);
}
}
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private void CheckEof(int read)
+ {
+ if (read == 0)
+ {
+ this.EofHitCount++;
+ }
+ }
}
}
diff --git a/src/ImageSharp/Memory/Allocators/Internals/UnmanagedMemoryHandle.cs b/src/ImageSharp/Memory/Allocators/Internals/UnmanagedMemoryHandle.cs
index 59d4d5bda4..df8dfbe0a0 100644
--- a/src/ImageSharp/Memory/Allocators/Internals/UnmanagedMemoryHandle.cs
+++ b/src/ImageSharp/Memory/Allocators/Internals/UnmanagedMemoryHandle.cs
@@ -14,7 +14,7 @@ namespace SixLabors.ImageSharp.Memory.Internals
internal struct UnmanagedMemoryHandle : IEquatable
{
// Number of allocation re-attempts when detecting OutOfMemoryException.
- private const int MaxAllocationAttempts = 1000;
+ private const int MaxAllocationAttempts = 10;
// Track allocations for testing purposes:
private static int totalOutstandingHandles;
diff --git a/src/ImageSharp/Memory/Allocators/MemoryAllocator.cs b/src/ImageSharp/Memory/Allocators/MemoryAllocator.cs
index 4df78d9d93..d064c1fc45 100644
--- a/src/ImageSharp/Memory/Allocators/MemoryAllocator.cs
+++ b/src/ImageSharp/Memory/Allocators/MemoryAllocator.cs
@@ -3,6 +3,8 @@
using System;
using System.Buffers;
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
namespace SixLabors.ImageSharp.Memory
{
@@ -11,6 +13,8 @@ namespace SixLabors.ImageSharp.Memory
///
public abstract class MemoryAllocator
{
+ private const int OneGigabyte = 1 << 30;
+
///
/// Gets the default platform-specific global instance that
/// serves as the default value for .
@@ -21,6 +25,10 @@ public abstract class MemoryAllocator
///
public static MemoryAllocator Default { get; } = Create();
+ internal long MemoryGroupAllocationLimitBytes { get; private set; } = Environment.Is64BitProcess ? 4L * OneGigabyte : OneGigabyte;
+
+ internal int SingleBufferAllocationLimitBytes { get; private set; } = OneGigabyte;
+
///
/// Gets the length of the largest contiguous buffer that can be handled by this allocator instance in bytes.
///
@@ -31,16 +39,24 @@ public abstract class MemoryAllocator
/// Creates a default instance of a optimized for the executing platform.
///
/// The .
- public static MemoryAllocator Create() =>
- new UniformUnmanagedMemoryPoolMemoryAllocator(null);
+ public static MemoryAllocator Create() => Create(default);
///
/// Creates the default using the provided options.
///
/// The .
/// The .
- public static MemoryAllocator Create(MemoryAllocatorOptions options) =>
- new UniformUnmanagedMemoryPoolMemoryAllocator(options.MaximumPoolSizeMegabytes);
+ public static MemoryAllocator Create(MemoryAllocatorOptions options)
+ {
+ UniformUnmanagedMemoryPoolMemoryAllocator allocator = new(options.MaximumPoolSizeMegabytes);
+ if (options.AllocationLimitMegabytes.HasValue)
+ {
+ allocator.MemoryGroupAllocationLimitBytes = options.AllocationLimitMegabytes.Value * 1024 * 1024;
+ allocator.SingleBufferAllocationLimitBytes = (int)Math.Min(allocator.SingleBufferAllocationLimitBytes, allocator.MemoryGroupAllocationLimitBytes);
+ }
+
+ return allocator;
+ }
///
/// Allocates an , holding a of length .
@@ -65,16 +81,35 @@ public virtual void ReleaseRetainedResources()
///
/// Allocates a .
///
+ /// The type of element to allocate.
/// The total length of the buffer.
/// The expected alignment (eg. to make sure image rows fit into single buffers).
/// The .
/// A new .
/// Thrown when 'blockAlignment' converted to bytes is greater than the buffer capacity of the allocator.
- internal virtual MemoryGroup AllocateGroup(
+ internal MemoryGroup AllocateGroup(
long totalLength,
int bufferAlignment,
AllocationOptions options = AllocationOptions.None)
where T : struct
- => MemoryGroup.Allocate(this, totalLength, bufferAlignment, options);
+ {
+ if (totalLength < 0)
+ {
+ throw new InvalidMemoryOperationException($"Attempted to allocate a buffer of negative length={totalLength}.");
+ }
+
+ ulong totalLengthInBytes = (ulong)totalLength * (ulong)Unsafe.SizeOf();
+ if (totalLengthInBytes > (ulong)this.MemoryGroupAllocationLimitBytes)
+ {
+ throw new InvalidMemoryOperationException($"Attempted to allocate a buffer of length={totalLengthInBytes} that exceeded the limit {this.MemoryGroupAllocationLimitBytes}.");
+ }
+
+ // Cast to long is safe because we already checked that the total length is within the limit.
+ return this.AllocateGroupCore(totalLength, (long)totalLengthInBytes, bufferAlignment, options);
+ }
+
+ internal virtual MemoryGroup AllocateGroupCore(long totalLengthInElements, long totalLengthInBytes, int bufferAlignment, AllocationOptions options)
+ where T : struct
+ => MemoryGroup.Allocate(this, totalLengthInElements, bufferAlignment, options);
}
}
diff --git a/src/ImageSharp/Memory/Allocators/MemoryAllocatorOptions.cs b/src/ImageSharp/Memory/Allocators/MemoryAllocatorOptions.cs
index 22a0410755..70dc515fe4 100644
--- a/src/ImageSharp/Memory/Allocators/MemoryAllocatorOptions.cs
+++ b/src/ImageSharp/Memory/Allocators/MemoryAllocatorOptions.cs
@@ -1,4 +1,4 @@
-// Copyright (c) Six Labors.
+// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
namespace SixLabors.ImageSharp.Memory
@@ -9,6 +9,7 @@ namespace SixLabors.ImageSharp.Memory
public struct MemoryAllocatorOptions
{
private int? maximumPoolSizeMegabytes;
+ private int? allocationLimitMegabytes;
///
/// Gets or sets a value defining the maximum size of the 's internal memory pool
@@ -27,5 +28,23 @@ public int? MaximumPoolSizeMegabytes
this.maximumPoolSizeMegabytes = value;
}
}
+
+ ///
+ /// Gets or sets a value defining the maximum (discontiguous) buffer size that can be allocated by the allocator in Megabytes.
+ /// means platform default: 1GB on 32-bit processes, 4GB on 64-bit processes.
+ ///
+ public int? AllocationLimitMegabytes
+ {
+ readonly get => this.allocationLimitMegabytes;
+ set
+ {
+ if (value.HasValue)
+ {
+ Guard.MustBeGreaterThan(value.Value, 0, nameof(this.AllocationLimitMegabytes));
+ }
+
+ this.allocationLimitMegabytes = value;
+ }
+ }
}
}
diff --git a/src/ImageSharp/Memory/Allocators/SimpleGcMemoryAllocator.cs b/src/ImageSharp/Memory/Allocators/SimpleGcMemoryAllocator.cs
index a53ecbc66e..e2b132139b 100644
--- a/src/ImageSharp/Memory/Allocators/SimpleGcMemoryAllocator.cs
+++ b/src/ImageSharp/Memory/Allocators/SimpleGcMemoryAllocator.cs
@@ -1,7 +1,8 @@
-// Copyright (c) Six Labors.
+// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System.Buffers;
+using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Memory.Internals;
namespace SixLabors.ImageSharp.Memory
@@ -17,7 +18,16 @@ public sealed class SimpleGcMemoryAllocator : MemoryAllocator
///
public override IMemoryOwner Allocate(int length, AllocationOptions options = AllocationOptions.None)
{
- Guard.MustBeGreaterThanOrEqualTo(length, 0, nameof(length));
+ if (length < 0)
+ {
+ throw new InvalidMemoryOperationException($"Attempted to allocate a buffer of negative length={length}.");
+ }
+
+ ulong lengthInBytes = (ulong)length * (ulong)Unsafe.SizeOf();
+ if (lengthInBytes > (ulong)this.SingleBufferAllocationLimitBytes)
+ {
+ throw new InvalidMemoryOperationException($"Attempted to allocate a buffer of length={lengthInBytes} that exceeded the limit {this.SingleBufferAllocationLimitBytes}.");
+ }
return new BasicArrayBuffer(new T[length]);
}
diff --git a/src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs b/src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs
index 0da4ff9f8c..a8056db537 100644
--- a/src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs
+++ b/src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs
@@ -87,10 +87,18 @@ public override IMemoryOwner Allocate(
int length,
AllocationOptions options = AllocationOptions.None)
{
- Guard.MustBeGreaterThanOrEqualTo(length, 0, nameof(length));
- int lengthInBytes = length * Unsafe.SizeOf();
+ if (length < 0)
+ {
+ throw new InvalidMemoryOperationException($"Attempted to allocate a buffer of negative length={length}.");
+ }
+
+ ulong lengthInBytes = (ulong)length * (ulong)Unsafe.SizeOf();
+ if (lengthInBytes > (ulong)this.SingleBufferAllocationLimitBytes)
+ {
+ throw new InvalidMemoryOperationException($"Attempted to allocate a buffer of length={lengthInBytes} that exceeded the limit {this.SingleBufferAllocationLimitBytes}.");
+ }
- if (lengthInBytes <= this.sharedArrayPoolThresholdInBytes)
+ if (lengthInBytes <= (ulong)this.sharedArrayPoolThresholdInBytes)
{
var buffer = new SharedArrayPoolBuffer(length);
if (options.Has(AllocationOptions.Clean))
@@ -101,7 +109,7 @@ public override IMemoryOwner Allocate(
return buffer;
}
- if (lengthInBytes <= this.poolBufferSizeInBytes)
+ if (lengthInBytes <= (ulong)this.poolBufferSizeInBytes)
{
UnmanagedMemoryHandle mem = this.pool.Rent();
if (mem.IsValid)
@@ -115,15 +123,15 @@ public override IMemoryOwner Allocate(
}
///
- internal override MemoryGroup AllocateGroup(
- long totalLength,
+ internal override MemoryGroup AllocateGroupCore(
+ long totalLengthInElements,
+ long totalLengthInBytes,
int bufferAlignment,
AllocationOptions options = AllocationOptions.None)
{
- long totalLengthInBytes = totalLength * Unsafe.SizeOf();
if (totalLengthInBytes <= this.sharedArrayPoolThresholdInBytes)
{
- var buffer = new SharedArrayPoolBuffer((int)totalLength);
+ var buffer = new SharedArrayPoolBuffer((int)totalLengthInElements);
return MemoryGroup.CreateContiguous(buffer, options.Has(AllocationOptions.Clean));
}
@@ -133,18 +141,18 @@ internal override MemoryGroup AllocateGroup(
UnmanagedMemoryHandle mem = this.pool.Rent();
if (mem.IsValid)
{
- UnmanagedBuffer buffer = this.pool.CreateGuardedBuffer(mem, (int)totalLength, options.Has(AllocationOptions.Clean));
+ UnmanagedBuffer buffer = this.pool.CreateGuardedBuffer(mem, (int)totalLengthInElements, options.Has(AllocationOptions.Clean));
return MemoryGroup.CreateContiguous(buffer, options.Has(AllocationOptions.Clean));
}
}
// Attempt to rent the whole group from the pool, allocate a group of unmanaged buffers if the attempt fails:
- if (MemoryGroup.TryAllocate(this.pool, totalLength, bufferAlignment, options, out MemoryGroup poolGroup))
+ if (MemoryGroup.TryAllocate(this.pool, totalLengthInElements, bufferAlignment, options, out MemoryGroup? poolGroup))
{
return poolGroup;
}
- return MemoryGroup.Allocate(this.nonPoolAllocator, totalLength, bufferAlignment, options);
+ return MemoryGroup.Allocate(this.nonPoolAllocator, totalLengthInElements, bufferAlignment, options);
}
public override void ReleaseRetainedResources() => this.pool.Release();
diff --git a/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs b/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs
index 9844f4a3bc..d77c2c4dc0 100644
--- a/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs
+++ b/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs
@@ -20,7 +20,7 @@ namespace SixLabors.ImageSharp.Memory
/// The element type.
internal abstract partial class MemoryGroup : IMemoryGroup, IDisposable
where T : struct
- {
+ {
private static readonly int ElementSize = Unsafe.SizeOf();
private MemoryGroupSpanCache memoryGroupSpanCache;
@@ -43,7 +43,7 @@ private MemoryGroup(int bufferLength, long totalLength)
///
public bool IsValid { get; private set; } = true;
- public MemoryGroupView View { get; private set; }
+ public MemoryGroupView View { get; private set; } = null!;
///
public abstract Memory this[int index] { get; }
@@ -85,12 +85,14 @@ public static MemoryGroup Allocate(
{
int bufferCapacityInBytes = allocator.GetBufferCapacityInBytes();
Guard.NotNull(allocator, nameof(allocator));
- Guard.MustBeGreaterThanOrEqualTo(totalLengthInElements, 0, nameof(totalLengthInElements));
- Guard.MustBeGreaterThanOrEqualTo(bufferAlignmentInElements, 0, nameof(bufferAlignmentInElements));
- int blockCapacityInElements = bufferCapacityInBytes / ElementSize;
+ if (totalLengthInElements < 0)
+ {
+ throw new InvalidMemoryOperationException($"Attempted to allocate a buffer of negative length={totalLengthInElements}.");
+ }
- if (bufferAlignmentInElements > blockCapacityInElements)
+ int blockCapacityInElements = bufferCapacityInBytes / ElementSize;
+ if (bufferAlignmentInElements < 0 || bufferAlignmentInElements > blockCapacityInElements)
{
throw new InvalidMemoryOperationException(
$"The buffer capacity of the provided MemoryAllocator is insufficient for the requested buffer alignment: {bufferAlignmentInElements}.");
diff --git a/src/ImageSharp/Memory/InvalidMemoryOperationException.cs b/src/ImageSharp/Memory/InvalidMemoryOperationException.cs
index 92b1d8d359..e9730fb964 100644
--- a/src/ImageSharp/Memory/InvalidMemoryOperationException.cs
+++ b/src/ImageSharp/Memory/InvalidMemoryOperationException.cs
@@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0.
using System;
+using System.Diagnostics.CodeAnalysis;
namespace SixLabors.ImageSharp.Memory
{
diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs
index 43ec45a34f..75a9800844 100644
--- a/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs
+++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs
@@ -619,5 +619,18 @@ public void BmpDecoder_CanDecode_Os2BitmapArray(TestImageProvider(TestImageProvider provider)
+ where TPixel : unmanaged, IPixel
+ {
+ // On V2 this is throwing InvalidOperationException,
+ // because of the validation logic in BmpInfoHeader.VerifyDimensions().
+ Assert.Throws(() =>
+ {
+ using Image image = provider.GetImage(BmpDecoder);
+ });
+ }
}
}
diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
index d12c840a1b..708e859eed 100644
--- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
+++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs
@@ -261,5 +261,22 @@ public void ValidateProgressivePdfJsOutput(
this.Output.WriteLine($"Difference for PORT: {portReport.DifferencePercentageString}");
}
}
+
+ [Theory]
+ [WithFile(TestImages.Jpeg.Issues.HangBadScan, PixelTypes.L8)]
+ public void DecodeHang(TestImageProvider provider)
+ where TPixel : unmanaged, IPixel
+ {
+ if (TestEnvironment.IsWindows &&
+ TestEnvironment.RunsOnCI)
+ {
+ // Windows CI runs consistently fail with OOM.
+ return;
+ }
+
+ using Image image = provider.GetImage(JpegDecoder);
+ Assert.Equal(65503, image.Width);
+ Assert.Equal(65503, image.Height);
+ }
}
}
diff --git a/tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs
index eb3bc8c9a5..8708320e09 100644
--- a/tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs
+++ b/tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs
@@ -2,8 +2,10 @@
// Licensed under the Apache License, Version 2.0.
using System.IO;
+using System.Text;
using SixLabors.ImageSharp.Formats.Pbm;
using SixLabors.ImageSharp.PixelFormats;
+using SixLabors.ImageSharp.Tests.TestUtilities;
using Xunit;
using static SixLabors.ImageSharp.Tests.TestImages.Pbm;
@@ -97,5 +99,25 @@ public void DecodeReferenceImage(TestImageProvider provider, str
bool isGrayscale = extension is "pgm" or "pbm";
image.CompareToReferenceOutput(provider, grayscale: isGrayscale);
}
+
+
+ [Fact]
+ public void PlainText_PrematureEof()
+ {
+ byte[] bytes = Encoding.ASCII.GetBytes($"P1\n100 100\n1 0 1 0 1 0");
+ using EofHitCounter eofHitCounter = EofHitCounter.RunDecoder(bytes);
+
+ Assert.True(eofHitCounter.EofHitCount <= 2);
+ Assert.Equal(new Size(100, 100), eofHitCounter.Image.Size());
+ }
+
+ [Fact]
+ public void Binary_PrematureEof()
+ {
+ using EofHitCounter eofHitCounter = EofHitCounter.RunDecoder(RgbBinaryPrematureEof);
+
+ Assert.True(eofHitCounter.EofHitCount <= 2);
+ Assert.Equal(new Size(29, 30), eofHitCounter.Image.Size());
+ }
}
}
diff --git a/tests/ImageSharp.Tests/Formats/Pbm/PbmMetadataTests.cs b/tests/ImageSharp.Tests/Formats/Pbm/PbmMetadataTests.cs
index 7915d224a9..8b5381c7ea 100644
--- a/tests/ImageSharp.Tests/Formats/Pbm/PbmMetadataTests.cs
+++ b/tests/ImageSharp.Tests/Formats/Pbm/PbmMetadataTests.cs
@@ -1,7 +1,9 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
+using System;
using System.IO;
+using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Pbm;
using Xunit;
@@ -82,5 +84,12 @@ public void Identify_DetectsCorrectComponentType(string imagePath, PbmComponentT
Assert.NotNull(bitmapMetadata);
Assert.Equal(expectedComponentType, bitmapMetadata.ComponentType);
}
+
+ [Fact]
+ public void Identify_EofInHeader_ThrowsInvalidImageContentException()
+ {
+ byte[] bytes = Convert.FromBase64String("UDEjWAAACQAAAAA=");
+ Assert.Throws(() => Image.Identify(bytes));
+ }
}
}
diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs
index a4fcf63baf..c361b1deb4 100644
--- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs
+++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs
@@ -526,5 +526,13 @@ static void RunTest(string providerDump, string nonContiguousBuffersStr)
"Disco")
.Dispose();
}
+
+ [Theory]
+ [InlineData(TestImages.Png.Bad.Issue2714BadPalette)]
+ public void Decode_BadPalette(string file)
+ {
+ string path = Path.GetFullPath(Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, file));
+ using Image image = Image.Load(path);
+ }
}
}
diff --git a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
index 2a8cbcbf78..a4243c94b6 100644
--- a/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
+++ b/tests/ImageSharp.Tests/Formats/Tiff/TiffDecoderTests.cs
@@ -668,6 +668,18 @@ public void TiffDecoder_ThrowsException_WithTooManyDirectories(TestImage
}
});
+ [Theory]
+ [WithFile(JpegCompressedGray0000539558, PixelTypes.Rgba32)]
+ public void TiffDecoder_ThrowsException_WithCircular_IFD_Offsets(TestImageProvider provider)
+ where TPixel : unmanaged, IPixel
+ => Assert.Throws(
+ () =>
+ {
+ using (provider.GetImage(TiffDecoder))
+ {
+ }
+ });
+
[Theory]
[WithFileCollection(nameof(MultiframeTestImages), PixelTypes.Rgba32)]
public void DecodeMultiframe(TestImageProvider provider)
diff --git a/tests/ImageSharp.Tests/Helpers/ParallelRowIteratorTests.cs b/tests/ImageSharp.Tests/Helpers/ParallelRowIteratorTests.cs
index f46c9519ca..7585998a64 100644
--- a/tests/ImageSharp.Tests/Helpers/ParallelRowIteratorTests.cs
+++ b/tests/ImageSharp.Tests/Helpers/ParallelRowIteratorTests.cs
@@ -4,6 +4,7 @@
using System;
using System.Linq;
using System.Numerics;
+using System.Runtime.CompilerServices;
using System.Threading;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory;
@@ -410,6 +411,41 @@ void RowAction(RowInterval rows, Span memory)
Assert.Contains(width <= 0 ? "Width" : "Height", ex.Message);
}
+ [Fact]
+ public void CanIterateWithoutIntOverflow()
+ {
+ ParallelExecutionSettings parallelSettings = ParallelExecutionSettings.FromConfiguration(Configuration.Default);
+ const int max = 100_000;
+
+ Rectangle rect = new(0, 0, max, max);
+ int intervalMaxY = 0;
+ void RowAction(RowInterval rows, Span memory) => intervalMaxY = Math.Max(rows.Max, intervalMaxY);
+
+ TestRowOperation operation = new(0);
+ TestRowIntervalOperation intervalOperation = new(RowAction);
+
+ ParallelRowIterator.IterateRows(Configuration.Default, rect, in operation);
+ Assert.Equal(max - 1, operation.MaxY.Value);
+
+ ParallelRowIterator.IterateRowIntervals, Rgba32>(rect, in parallelSettings, in intervalOperation);
+ Assert.Equal(max, intervalMaxY);
+ }
+
+ private readonly struct TestRowOperation : IRowOperation
+ {
+ public TestRowOperation(int _) => this.MaxY = new StrongBox();
+
+ public StrongBox MaxY { get; }
+
+ public void Invoke(int y)
+ {
+ lock (this.MaxY)
+ {
+ this.MaxY.Value = Math.Max(y, this.MaxY.Value);
+ }
+ }
+ }
+
private readonly struct TestRowIntervalOperation : IRowIntervalOperation
{
private readonly Action action;
diff --git a/tests/ImageSharp.Tests/Image/ImageTests.cs b/tests/ImageSharp.Tests/Image/ImageTests.cs
index 0a9e2817a5..1bfd307cbd 100644
--- a/tests/ImageSharp.Tests/Image/ImageTests.cs
+++ b/tests/ImageSharp.Tests/Image/ImageTests.cs
@@ -40,6 +40,10 @@ public void Width_Height()
}
}
+ [Fact]
+ public void Width_Height_SizeNotRepresentable_ThrowsInvalidImageOperationException()
+ => Assert.Throws(() => new Image(int.MaxValue, int.MaxValue));
+
[Fact]
public void Configuration_Width_Height()
{
diff --git a/tests/ImageSharp.Tests/Memory/Allocators/SimpleGcMemoryAllocatorTests.cs b/tests/ImageSharp.Tests/Memory/Allocators/SimpleGcMemoryAllocatorTests.cs
index 3ab45be82d..c4ff74cdd2 100644
--- a/tests/ImageSharp.Tests/Memory/Allocators/SimpleGcMemoryAllocatorTests.cs
+++ b/tests/ImageSharp.Tests/Memory/Allocators/SimpleGcMemoryAllocatorTests.cs
@@ -21,13 +21,17 @@ public BufferTests()
protected SimpleGcMemoryAllocator MemoryAllocator { get; } = new SimpleGcMemoryAllocator();
- [Theory]
- [InlineData(-1)]
- public void Allocate_IncorrectAmount_ThrowsCorrect_ArgumentOutOfRangeException(int length)
+ public static TheoryData InvalidLengths { get; set; } = new()
{
- ArgumentOutOfRangeException ex = Assert.Throws(() => this.MemoryAllocator.Allocate(length));
- Assert.Equal("length", ex.ParamName);
- }
+ { -1 },
+ { (1 << 30) + 1 }
+ };
+
+ [Theory]
+ [MemberData(nameof(InvalidLengths))]
+ public void Allocate_IncorrectAmount_ThrowsCorrect_InvalidMemoryOperationException(int length)
+ => Assert.Throws(
+ () => this.MemoryAllocator.Allocate(length));
[Fact]
public unsafe void Allocate_MemoryIsPinnableMultipleTimes()
diff --git a/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs b/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs
index 0520c3c1fe..c26c7ed888 100644
--- a/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs
+++ b/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs
@@ -111,6 +111,24 @@ public void AllocateGroup_MultipleTimes_ExceedPoolLimit()
}
}
+ [Fact]
+ public void AllocateGroup_SizeInBytesOverLongMaxValue_ThrowsInvalidMemoryOperationException()
+ {
+ var allocator = new UniformUnmanagedMemoryPoolMemoryAllocator(null);
+ Assert.Throws(() => allocator.AllocateGroup(int.MaxValue * (long)int.MaxValue, int.MaxValue));
+ }
+
+ public static TheoryData InvalidLengths { get; set; } = new()
+ {
+ { -1 },
+ { (1 << 30) + 1 }
+ };
+
+ [Theory]
+ [MemberData(nameof(InvalidLengths))]
+ public void Allocate_IncorrectAmount_ThrowsCorrect_InvalidMemoryOperationException(int length)
+ => Assert.Throws(() => new UniformUnmanagedMemoryPoolMemoryAllocator(null).Allocate(length));
+
[Fact]
public unsafe void Allocate_MemoryIsPinnableMultipleTimes()
{
@@ -380,6 +398,30 @@ private static void AllocateSingleAndForget(UniformUnmanagedMemoryPoolMemoryAllo
}
}
+ [Fact]
+ public void Allocate_OverLimit_ThrowsInvalidMemoryOperationException()
+ {
+ MemoryAllocator allocator = MemoryAllocator.Create(new MemoryAllocatorOptions()
+ {
+ AllocationLimitMegabytes = 4
+ });
+ const int oneMb = 1 << 20;
+ allocator.Allocate(4 * oneMb).Dispose(); // Should work
+ Assert.Throws(() => allocator.Allocate(5 * oneMb));
+ }
+
+ [Fact]
+ public void AllocateGroup_OverLimit_ThrowsInvalidMemoryOperationException()
+ {
+ MemoryAllocator allocator = MemoryAllocator.Create(new MemoryAllocatorOptions()
+ {
+ AllocationLimitMegabytes = 4
+ });
+ const int oneMb = 1 << 20;
+ allocator.AllocateGroup(4 * oneMb, 1024).Dispose(); // Should work
+ Assert.Throws(() => allocator.AllocateGroup(5 * oneMb, 1024));
+ }
+
#if NETCOREAPP3_1_OR_GREATER
[Fact]
public void Issue2001_NegativeMemoryReportedByGc()
diff --git a/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs b/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs
index 486fbe4640..751f35890b 100644
--- a/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs
+++ b/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs
@@ -7,6 +7,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Memory;
+using SixLabors.ImageSharp.PixelFormats;
using Xunit;
@@ -342,5 +343,27 @@ public void PublicMemoryGroup_IsMemoryGroupView()
Assert.False(mgBefore.IsValid);
Assert.NotSame(mgBefore, buffer1.MemoryGroup);
}
+
+ public static TheoryData InvalidLengths { get; set; } = new()
+ {
+ { new(-1, -1) },
+ { new(32768, 32769) },
+ { new(32769, 32768) }
+ };
+
+ [Theory]
+ [MemberData(nameof(InvalidLengths))]
+ public void Allocate_IncorrectAmount_ThrowsCorrect_InvalidMemoryOperationException(Size size)
+ => Assert.Throws(() => this.MemoryAllocator.Allocate2D(size.Width, size.Height));
+
+ [Theory]
+ [MemberData(nameof(InvalidLengths))]
+ public void Allocate_IncorrectAmount_ThrowsCorrect_InvalidMemoryOperationException_Size(Size size)
+ => Assert.Throws(() => this.MemoryAllocator.Allocate2D(new Size(size)));
+
+ [Theory]
+ [MemberData(nameof(InvalidLengths))]
+ public void Allocate_IncorrectAmount_ThrowsCorrect_InvalidMemoryOperationException_OverAligned(Size size)
+ => Assert.Throws(() => this.MemoryAllocator.Allocate2DOveraligned(size.Width, size.Height, 1));
}
}
diff --git a/tests/ImageSharp.Tests/Memory/DiscontiguousBuffers/MemoryGroupTests.Allocate.cs b/tests/ImageSharp.Tests/Memory/DiscontiguousBuffers/MemoryGroupTests.Allocate.cs
index adfafcb890..299a6c5134 100644
--- a/tests/ImageSharp.Tests/Memory/DiscontiguousBuffers/MemoryGroupTests.Allocate.cs
+++ b/tests/ImageSharp.Tests/Memory/DiscontiguousBuffers/MemoryGroupTests.Allocate.cs
@@ -1,8 +1,7 @@
-// Copyright (c) Six Labors.
+// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.
using System;
-using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
@@ -10,7 +9,6 @@
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Memory.Internals;
using Xunit;
-using Xunit.Abstractions;
namespace SixLabors.ImageSharp.Tests.Memory.DiscontiguousBuffers
{
@@ -225,12 +223,18 @@ public void MemoryAllocatorIsUtilizedCorrectly(AllocationOptions allocationOptio
[StructLayout(LayoutKind.Sequential, Size = 5)]
internal struct S5
{
- public override string ToString() => "S5";
+ public override readonly string ToString() => nameof(S5);
}
[StructLayout(LayoutKind.Sequential, Size = 4)]
internal struct S4
{
- public override string ToString() => "S4";
+ public override readonly string ToString() => nameof(S4);
+ }
+
+ [StructLayout(LayoutKind.Explicit, Size = 512)]
+ internal struct S512
+ {
+ public override readonly string ToString() => nameof(S512);
}
}
diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs
index 13f965a592..5ff4dddb00 100644
--- a/tests/ImageSharp.Tests/TestImages.cs
+++ b/tests/ImageSharp.Tests/TestImages.cs
@@ -156,6 +156,8 @@ public static class Bad
// Invalid color type.
public const string ColorTypeOne = "Png/xc1n0g08.png";
public const string ColorTypeNine = "Png/xc9n2c08.png";
+
+ public const string Issue2714BadPalette = "Png/issues/Issue_2714.png";
}
}
@@ -266,6 +268,7 @@ public static class Issues
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 const string Issue2133DeduceColorSpace = "Jpg/issues/Issue2133.jpg";
+ public const string HangBadScan = "Jpg/issues/Hang_C438A851.jpg";
public static class Fuzz
{
@@ -386,6 +389,8 @@ public static class Bmp
public const string Rgba321010102 = "Bmp/rgba32-1010102.bmp";
public const string RgbaAlphaBitfields = "Bmp/rgba32abf.bmp";
+ public const string Issue2696 = "Bmp/issue-2696.bmp";
+
public static readonly string[] BitFields =
{
Rgb32bfdef,
@@ -915,6 +920,7 @@ public static class Tiff
public const string Issues1716Rgb161616BitLittleEndian = "Tiff/Issues/Issue1716.tiff";
public const string Issues1891 = "Tiff/Issues/Issue1891.tiff";
public const string Issues2123 = "Tiff/Issues/Issue2123.tiff";
+ public const string JpegCompressedGray0000539558 = "Tiff/Issues/JpegCompressedGray-0000539558.tiff";
public const string SmallRgbDeflate = "Tiff/rgb_small_deflate.tiff";
public const string SmallRgbLzw = "Tiff/rgb_small_lzw.tiff";
@@ -976,6 +982,7 @@ public static class Pbm
public const string GrayscalePlainNormalized = "Pbm/grayscale_plain_normalized.pgm";
public const string GrayscalePlainMagick = "Pbm/grayscale_plain_magick.pgm";
public const string RgbBinary = "Pbm/00000_00000.ppm";
+ public const string RgbBinaryPrematureEof = "Pbm/00000_00000_premature_eof.ppm";
public const string RgbPlain = "Pbm/rgb_plain.ppm";
public const string RgbPlainNormalized = "Pbm/rgb_plain_normalized.ppm";
public const string RgbPlainMagick = "Pbm/rgb_plain_magick.ppm";
diff --git a/tests/ImageSharp.Tests/TestUtilities/EofHitCounter.cs b/tests/ImageSharp.Tests/TestUtilities/EofHitCounter.cs
new file mode 100644
index 0000000000..b627221f5b
--- /dev/null
+++ b/tests/ImageSharp.Tests/TestUtilities/EofHitCounter.cs
@@ -0,0 +1,39 @@
+// Copyright (c) Six Labors.
+// Licensed under the Apache License, Version 2.0.
+
+using System;
+using System.IO;
+using SixLabors.ImageSharp.IO;
+
+namespace SixLabors.ImageSharp.Tests.TestUtilities
+{
+ internal class EofHitCounter : IDisposable
+ {
+ private readonly BufferedReadStream stream;
+
+ public EofHitCounter(BufferedReadStream stream, Image image)
+ {
+ this.stream = stream;
+ this.Image = image;
+ }
+
+ public int EofHitCount => this.stream.EofHitCount;
+
+ public Image Image { get; private set; }
+
+ public static EofHitCounter RunDecoder(string testImage) => RunDecoder(TestFile.Create(testImage).Bytes);
+
+ public static EofHitCounter RunDecoder(byte[] imageData)
+ {
+ BufferedReadStream stream = new(Configuration.Default, new MemoryStream(imageData));
+ Image image = Image.Load(stream);
+ return new EofHitCounter(stream, image);
+ }
+
+ public void Dispose()
+ {
+ this.stream.Dispose();
+ this.Image.Dispose();
+ }
+ }
+}
diff --git a/tests/Images/Input/Bmp/issue-2696.bmp b/tests/Images/Input/Bmp/issue-2696.bmp
new file mode 100644
index 0000000000..6770dd9469
--- /dev/null
+++ b/tests/Images/Input/Bmp/issue-2696.bmp
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:bc42cda9bac8fc73351ad03bf55214069bb8d31ea5bdd806321a8cc8b56c282e
+size 126
diff --git a/tests/Images/Input/Jpg/issues/Hang_C438A851.jpg b/tests/Images/Input/Jpg/issues/Hang_C438A851.jpg
new file mode 100644
index 0000000000..97ab9ad0fb
--- /dev/null
+++ b/tests/Images/Input/Jpg/issues/Hang_C438A851.jpg
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:580760756f2e7e3ed0752a4ec53d6b6786a4f005606f3a50878f732b3b2a1bcb
+size 413
diff --git a/tests/Images/Input/Pbm/00000_00000_premature_eof.ppm b/tests/Images/Input/Pbm/00000_00000_premature_eof.ppm
new file mode 100644
index 0000000000..445cd0059a
--- /dev/null
+++ b/tests/Images/Input/Pbm/00000_00000_premature_eof.ppm
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:39cf6ca5b2f9d428c0c33e0fc7ab5e92c31e0c8a7d9e0276b9285f51a8ff547c
+size 69
diff --git a/tests/Images/Input/Png/issues/Issue_2714.png b/tests/Images/Input/Png/issues/Issue_2714.png
new file mode 100644
index 0000000000..9bb231dd9f
--- /dev/null
+++ b/tests/Images/Input/Png/issues/Issue_2714.png
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:9a4b6efc3090dbd70ae9efe97ea817464845263536beea4e80fd7c884dee6c5a
+size 128
diff --git a/tests/Images/Input/Tiff/Issues/JpegCompressedGray-0000539558.tiff b/tests/Images/Input/Tiff/Issues/JpegCompressedGray-0000539558.tiff
new file mode 100644
index 0000000000..934bf3c9a3
--- /dev/null
+++ b/tests/Images/Input/Tiff/Issues/JpegCompressedGray-0000539558.tiff
@@ -0,0 +1,3 @@
+version https://git-lfs.github.com/spec/v1
+oid sha256:1f1ca630b5e46c7b5f21100fa8c0fbf27b79ca9da8cd95897667b64aedccf6e5
+size 539558