Skip to content

Commit 14aeb3f

Browse files
limit for iteration & bufSz
1 parent 938595b commit 14aeb3f

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Diagnostics;
88
using System.Globalization;
99
using System.Runtime.CompilerServices;
10+
using System.Runtime.InteropServices;
1011
using System.Text;
1112
using SixLabors.ImageSharp.Memory;
1213

@@ -187,18 +188,22 @@ protected void ReadValues(List<IExifValue> values, uint offset)
187188

188189
protected void ReadSubIfd(List<IExifValue> values)
189190
{
190-
if (this.subIfds is not null)
191+
if (this.subIfds != null)
191192
{
192-
do
193+
const int maxSubIfds = 8;
194+
const int maxNestingLevel = 8;
195+
Span<ulong> buf = stackalloc ulong[maxSubIfds];
196+
for (int i = 0; i < maxNestingLevel && this.subIfds.Count > 0; i++)
193197
{
194-
ulong[] buf = [.. this.subIfds];
198+
int sz = Math.Min(this.subIfds.Count, maxSubIfds);
199+
CollectionsMarshal.AsSpan(this.subIfds)[..sz].CopyTo(buf);
200+
195201
this.subIfds.Clear();
196-
foreach (ulong subIfdOffset in buf)
202+
foreach (ulong subIfdOffset in buf[..sz])
197203
{
198204
this.ReadValues(values, (uint)subIfdOffset);
199205
}
200206
}
201-
while (this.subIfds.Count > 0);
202207
}
203208
}
204209

0 commit comments

Comments
 (0)