diff --git a/src/ImageSharp/Formats/Exr/Compression/Decompressors/NoneExrCompression.cs b/src/ImageSharp/Formats/Exr/Compression/Decompressors/NoneExrCompression.cs index 26c69db8c6..fb4fa5d832 100644 --- a/src/ImageSharp/Formats/Exr/Compression/Decompressors/NoneExrCompression.cs +++ b/src/ImageSharp/Formats/Exr/Compression/Decompressors/NoneExrCompression.cs @@ -15,7 +15,13 @@ internal class NoneExrCompression : ExrBaseDecompressor /// public override void Decompress(BufferedReadStream stream, uint compressedBytes, Span buffer) - => stream.Read(buffer, 0, Math.Min(buffer.Length, (int)this.BytesPerBlock)); + { + int bytesRead = stream.Read(buffer, 0, Math.Min(buffer.Length, (int)this.BytesPerBlock)); + if (bytesRead != (int)this.BytesPerBlock) + { + ExrThrowHelper.ThrowInvalidImageContentException("Could not read enough pixel data!"); + } + } /// protected override void Dispose(bool disposing) diff --git a/src/ImageSharp/Formats/Exr/ExrDecoderCore.cs b/src/ImageSharp/Formats/Exr/ExrDecoderCore.cs index 1cf0e27304..7598c62f3a 100644 --- a/src/ImageSharp/Formats/Exr/ExrDecoderCore.cs +++ b/src/ImageSharp/Formats/Exr/ExrDecoderCore.cs @@ -454,13 +454,18 @@ internal sealed class ExrDecoderCore : ImageDecoderCore // Next three bytes contain info's about the image. byte flagsByte0 = (byte)stream.ReadByte(); - stream.ReadByte(); - stream.ReadByte(); if ((flagsByte0 & (1 << 1)) != 0) { ExrThrowHelper.ThrowNotSupported("Decoding tiled exr images is not supported yet!"); } + // Discard the next two bytes. + int bytesRead = stream.Read(this.buffer, 0, 2); + if (bytesRead != 2) + { + ExrThrowHelper.ThrowInvalidImageContentException("Could not read enough data for exr file!"); + } + this.HeaderAttributes = this.ParseHeaderAttributes(stream); this.Width = this.HeaderAttributes.DataWindow.XMax - this.HeaderAttributes.DataWindow.XMin + 1; @@ -632,7 +637,10 @@ internal sealed class ExrDecoderCore : ImageDecoderCore } // Last byte should be a null byte. - stream.ReadByte(); + if (stream.ReadByte() == -1) + { + ExrThrowHelper.ThrowInvalidImageContentException("Could not read enough data to read exr channel list!"); + } return channels; } @@ -648,9 +656,11 @@ internal sealed class ExrDecoderCore : ImageDecoderCore byte pLinear = (byte)stream.ReadByte(); // Next 3 bytes are reserved bytes and not use. - stream.ReadByte(); - stream.ReadByte(); - stream.ReadByte(); + if (stream.Read(this.buffer, 0, 3) != 3) + { + ExrThrowHelper.ThrowInvalidImageContentException("Could not read enough data to read exr channel info!"); + } + bytesRead += 4; int xSampling = this.ReadSignedInteger(stream); diff --git a/src/ImageSharp/IO/BufferedReadStream.cs b/src/ImageSharp/IO/BufferedReadStream.cs index 0372f677fc..8080aab87f 100644 --- a/src/ImageSharp/IO/BufferedReadStream.cs +++ b/src/ImageSharp/IO/BufferedReadStream.cs @@ -2,7 +2,6 @@ // Licensed under the Six Labors Split License. using System.Buffers; -using System.Buffers.Binary; using System.Runtime.CompilerServices; namespace SixLabors.ImageSharp.IO;