@@ -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