Skip to content

Commit 91a9783

Browse files
Add sanitation and make tests parametric.
1 parent 881b79b commit 91a9783

File tree

3 files changed

+153
-72
lines changed

3 files changed

+153
-72
lines changed

src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,11 @@ private void ProcessApplicationHeaderMarker(BufferedReadStream stream, int remai
514514
// TODO: thumbnail
515515
if (remaining > 0)
516516
{
517+
if (stream.Position + remaining >= stream.Length)
518+
{
519+
JpegThrowHelper.ThrowInvalidImageContentException("Bad App0 Marker length.");
520+
}
521+
517522
stream.Skip(remaining);
518523
}
519524
}
@@ -533,6 +538,11 @@ private void ProcessApp1Marker(BufferedReadStream stream, int remaining)
533538
return;
534539
}
535540

541+
if (stream.Position + remaining >= stream.Length)
542+
{
543+
JpegThrowHelper.ThrowInvalidImageContentException("Bad App1 Marker length.");
544+
}
545+
536546
var profile = new byte[remaining];
537547
stream.Read(profile, 0, remaining);
538548

@@ -550,6 +560,7 @@ private void ProcessApp1Marker(BufferedReadStream stream, int remaining)
550560
this.ExtendProfile(ref this.exifData, profile.AsSpan(Exif00).ToArray());
551561
}
552562
}
563+
553564
}
554565

555566
/// <summary>

src/ImageSharp/IO/BufferedReadStream.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public BufferedReadStream(Configuration configuration, Stream stream)
5050
}
5151

5252
this.BaseStream = stream;
53-
this.Position = (int)stream.Position;
5453
this.Length = stream.Length;
54+
this.Position = (int)stream.Position;
5555
this.BufferSize = configuration.StreamProcessingBufferSize;
5656
this.maxBufferIndex = this.BufferSize - 1;
5757
this.readBuffer = ArrayPool<byte>.Shared.Rent(this.BufferSize);
@@ -86,6 +86,9 @@ public override long Position
8686
[MethodImpl(MethodImplOptions.NoInlining)]
8787
set
8888
{
89+
Guard.MustBeGreaterThanOrEqualTo(value, 0, nameof(this.Position));
90+
Guard.MustBeLessThan(value, this.Length, nameof(this.Position));
91+
8992
// Only reset readBufferIndex if we are out of bounds of our working buffer
9093
// otherwise we should simply move the value by the diff.
9194
if (this.IsInReadBuffer(value, out long index))

0 commit comments

Comments
 (0)