@@ -40,7 +40,7 @@ public DirectoryReader(Stream stream, MemoryAllocator allocator)
4040 public IList < ExifProfile > Read ( )
4141 {
4242 this . ByteOrder = ReadByteOrder ( this . stream ) ;
43- var headerReader = new HeaderReader ( this . stream , this . ByteOrder ) ;
43+ HeaderReader headerReader = new ( this . stream , this . ByteOrder ) ;
4444 headerReader . ReadFileHeader ( ) ;
4545
4646 this . nextIfdOffset = headerReader . FirstIfdOffset ;
@@ -52,7 +52,12 @@ public IList<ExifProfile> Read()
5252 private static ByteOrder ReadByteOrder ( Stream stream )
5353 {
5454 Span < byte > headerBytes = stackalloc byte [ 2 ] ;
55- stream . Read ( headerBytes ) ;
55+
56+ if ( stream . Read ( headerBytes ) != 2 )
57+ {
58+ throw TiffThrowHelper . ThrowInvalidHeader ( ) ;
59+ }
60+
5661 if ( headerBytes [ 0 ] == TiffConstants . ByteOrderLittleEndian && headerBytes [ 1 ] == TiffConstants . ByteOrderLittleEndian )
5762 {
5863 return ByteOrder . LittleEndian ;
@@ -68,10 +73,10 @@ private static ByteOrder ReadByteOrder(Stream stream)
6873
6974 private IList < ExifProfile > ReadIfds ( bool isBigTiff )
7075 {
71- var readers = new List < EntryReader > ( ) ;
76+ List < EntryReader > readers = new ( ) ;
7277 while ( this . nextIfdOffset != 0 && this . nextIfdOffset < ( ulong ) this . stream . Length )
7378 {
74- var reader = new EntryReader ( this . stream , this . ByteOrder , this . allocator ) ;
79+ EntryReader reader = new ( this . stream , this . ByteOrder , this . allocator ) ;
7580 reader . ReadTags ( isBigTiff , this . nextIfdOffset ) ;
7681
7782 if ( reader . BigValues . Count > 0 )
@@ -85,6 +90,11 @@ private IList<ExifProfile> ReadIfds(bool isBigTiff)
8590 }
8691 }
8792
93+ if ( this . nextIfdOffset >= reader . NextIfdOffset && reader . NextIfdOffset != 0 )
94+ {
95+ TiffThrowHelper . ThrowImageFormatException ( "TIFF image contains circular directory offsets" ) ;
96+ }
97+
8898 this . nextIfdOffset = reader . NextIfdOffset ;
8999 readers . Add ( reader ) ;
90100
@@ -94,11 +104,11 @@ private IList<ExifProfile> ReadIfds(bool isBigTiff)
94104 }
95105 }
96106
97- var list = new List < ExifProfile > ( readers . Count ) ;
107+ List < ExifProfile > list = new ( readers . Count ) ;
98108 foreach ( EntryReader reader in readers )
99109 {
100110 reader . ReadBigValues ( ) ;
101- var profile = new ExifProfile ( reader . Values , reader . InvalidTags ) ;
111+ ExifProfile profile = new ( reader . Values , reader . InvalidTags ) ;
102112 list . Add ( profile ) ;
103113 }
104114
0 commit comments