Skip to content

Commit ae84ff6

Browse files
committed
Break if there are more then 65534 IFD directories, fixes #1897
1 parent 53456a5 commit ae84ff6

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/ImageSharp/Formats/Tiff/Ifd/DirectoryReader.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ internal class DirectoryReader
1818

1919
private uint nextIfdOffset;
2020

21+
private const int DirectoryMax = 65534;
22+
2123
// used for sequential read big values (actual for multiframe big files)
2224
// todo: different tags can link to the same data (stream offset) - investigate
23-
private readonly SortedList<uint, Action> lazyLoaders = new SortedList<uint, Action>(new DuplicateKeyComparer<uint>());
25+
private readonly SortedList<uint, Action> lazyLoaders = new(new DuplicateKeyComparer<uint>());
2426

2527
public DirectoryReader(Stream stream) => this.stream = stream;
2628

@@ -48,7 +50,8 @@ private static ByteOrder ReadByteOrder(Stream stream)
4850
{
4951
return ByteOrder.LittleEndian;
5052
}
51-
else if (headerBytes[0] == TiffConstants.ByteOrderBigEndian && headerBytes[1] == TiffConstants.ByteOrderBigEndian)
53+
54+
if (headerBytes[0] == TiffConstants.ByteOrderBigEndian && headerBytes[1] == TiffConstants.ByteOrderBigEndian)
5255
{
5356
return ByteOrder.BigEndian;
5457
}
@@ -67,6 +70,11 @@ private IEnumerable<ExifProfile> ReadIfds()
6770
this.nextIfdOffset = reader.NextIfdOffset;
6871

6972
readers.Add(reader);
73+
74+
if (readers.Count >= DirectoryMax)
75+
{
76+
TiffThrowHelper.ThrowImageFormatException("TIFF image contains too many directories");
77+
}
7078
}
7179

7280
// Sequential reading big values.

src/ImageSharp/Formats/Tiff/Ifd/EntryReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public EntryReader(Stream stream, ByteOrder byteOrder, uint ifdOffset, SortedLis
2323
this.lazyLoaders = lazyLoaders;
2424
}
2525

26-
public List<IExifValue> Values { get; } = new List<IExifValue>();
26+
public List<IExifValue> Values { get; } = new();
2727

2828
public uint NextIfdOffset { get; private set; }
2929

0 commit comments

Comments
 (0)