Skip to content

Commit ced9887

Browse files
committed
Add checks, if enough data was read
1 parent d22e50c commit ced9887

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/ImageSharp/Formats/Gif/GifDecoderCore.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,11 @@ public IImageInfo Identify(BufferedReadStream stream, CancellationToken cancella
221221
/// </summary>
222222
private void ReadGraphicalControlExtension()
223223
{
224-
this.stream.Read(this.buffer, 0, 6);
224+
int bytesRead = this.stream.Read(this.buffer, 0, 6);
225+
if (bytesRead != 6)
226+
{
227+
GifThrowHelper.ThrowInvalidImageContentException("Not enough data to read the graphic control extension");
228+
}
225229

226230
this.graphicsControlExtension = GifGraphicControlExtension.Parse(this.buffer);
227231
}
@@ -231,7 +235,11 @@ private void ReadGraphicalControlExtension()
231235
/// </summary>
232236
private void ReadImageDescriptor()
233237
{
234-
this.stream.Read(this.buffer, 0, 9);
238+
int bytesRead = this.stream.Read(this.buffer, 0, 9);
239+
if (bytesRead != 9)
240+
{
241+
GifThrowHelper.ThrowInvalidImageContentException("Not enough data to read the image descriptor");
242+
}
235243

236244
this.imageDescriptor = GifImageDescriptor.Parse(this.buffer);
237245
if (this.imageDescriptor.Height == 0 || this.imageDescriptor.Width == 0)
@@ -245,7 +253,11 @@ private void ReadImageDescriptor()
245253
/// </summary>
246254
private void ReadLogicalScreenDescriptor()
247255
{
248-
this.stream.Read(this.buffer, 0, 7);
256+
int bytesRead = this.stream.Read(this.buffer, 0, 7);
257+
if (bytesRead != 7)
258+
{
259+
GifThrowHelper.ThrowInvalidImageContentException("Not enough data to read the logical screen descriptor");
260+
}
249261

250262
this.logicalScreenDescriptor = GifLogicalScreenDescriptor.Parse(this.buffer);
251263
}

0 commit comments

Comments
 (0)