Browse Source

Fix infinate loop when a GIF prematurely terminates in Skip

pull/770/head
Jason Nelson 8 years ago
parent
commit
45fd99983a
  1. 29
      src/ImageSharp/Formats/Gif/GifDecoderCore.cs

29
src/ImageSharp/Formats/Gif/GifDecoderCore.cs

@ -142,8 +142,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
this.ReadApplicationExtension(); this.ReadApplicationExtension();
break; break;
case GifConstants.PlainTextLabel: case GifConstants.PlainTextLabel:
int plainLength = stream.ReadByte(); this.SkipBlock(); // Not supported by any known decoder.
this.Skip(plainLength); // Not supported by any known decoder.
break; break;
} }
} }
@ -190,9 +189,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
switch (stream.ReadByte()) switch (stream.ReadByte())
{ {
case GifConstants.GraphicControlLabel: case GifConstants.GraphicControlLabel:
this.SkipBlock(); // Skip graphic control extension block
// Skip graphic control extension block
this.Skip(0);
break; break;
case GifConstants.CommentLabel: case GifConstants.CommentLabel:
this.ReadComments(); this.ReadComments();
@ -201,8 +198,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
this.ReadApplicationExtension(); this.ReadApplicationExtension();
break; break;
case GifConstants.PlainTextLabel: case GifConstants.PlainTextLabel:
int plainLength = stream.ReadByte(); this.SkipBlock(); // Not supported by any known decoder.
this.Skip(plainLength); // Not supported by any known decoder.
break; break;
} }
} }
@ -288,24 +284,27 @@ namespace SixLabors.ImageSharp.Formats.Gif
// Could be XMP or something else not supported yet. // Could be XMP or something else not supported yet.
// Back up and skip. // Back up and skip.
this.stream.Position -= appLength + 1; this.stream.Position -= appLength + 1;
this.Skip(appLength); this.SkipBlock(appLength);
return; return;
} }
this.Skip(appLength); // Not supported by any known decoder. this.SkipBlock(appLength); // Not supported by any known decoder.
} }
/// <summary> /// <summary>
/// Skips the designated number of bytes in the stream. /// Skips over a block or reads its terminator.
/// <param name="blockSize">The length of the block to skip.</param>
/// </summary> /// </summary>
/// <param name="length">The number of bytes to skip.</param> private void SkipBlock(int blockSize = 0)
private void Skip(int length)
{ {
this.stream.Skip(length); if (blockSize > 0)
{
this.stream.Skip(blockSize);
}
int flag; int flag;
while ((flag = this.stream.ReadByte()) != 0) while ((flag = this.stream.ReadByte()) > 0)
{ {
this.stream.Skip(flag); this.stream.Skip(flag);
} }
@ -370,7 +369,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
this.ReadFrameColors(ref image, ref previousFrame, indices.GetSpan(), colorTable, this.imageDescriptor); this.ReadFrameColors(ref image, ref previousFrame, indices.GetSpan(), colorTable, this.imageDescriptor);
// Skip any remaining blocks // Skip any remaining blocks
this.Skip(0); this.SkipBlock();
} }
finally finally
{ {

Loading…
Cancel
Save