Browse Source

Evaluate return value of stream.Read() to make sure enough data was read

pull/3096/head
Brian Popow 2 months ago
parent
commit
1853289863
  1. 8
      src/ImageSharp/Formats/Exr/Compression/Decompressors/NoneExrCompression.cs
  2. 22
      src/ImageSharp/Formats/Exr/ExrDecoderCore.cs
  3. 1
      src/ImageSharp/IO/BufferedReadStream.cs

8
src/ImageSharp/Formats/Exr/Compression/Decompressors/NoneExrCompression.cs

@ -15,7 +15,13 @@ internal class NoneExrCompression : ExrBaseDecompressor
/// <inheritdoc/>
public override void Decompress(BufferedReadStream stream, uint compressedBytes, Span<byte> 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!");
}
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)

22
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);

1
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;

Loading…
Cancel
Save