Skip to content

Commit 74687bf

Browse files
ymalichdecriptor
authored andcommitted
Fixed KeyNotFoundException in IFDReader: added synchronization locks to protect class members ifd_offsets and ifd_loopdetect_refs from being modified from multiple thread simultaneously.
1 parent 88343ab commit 74687bf

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

Diff for: src/TaglibSharp/IFD/IFDReader.cs

+21-12
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,13 @@ public void Read (int count)
198198
/// </summary>
199199
void StartIFDLoopDetect ()
200200
{
201-
if (!ifd_offsets.ContainsKey (file)) {
202-
ifd_offsets[file] = new List<long> ();
203-
ifd_loopdetect_refs[file] = 1;
204-
} else {
205-
ifd_loopdetect_refs[file]++;
201+
lock (ifd_sync_object) {
202+
if (!ifd_offsets.ContainsKey (file)) {
203+
ifd_offsets[file] = new List<long> ();
204+
ifd_loopdetect_refs[file] = 1;
205+
} else {
206+
ifd_loopdetect_refs[file]++;
207+
}
206208
}
207209
}
208210

@@ -220,9 +222,13 @@ bool DetectIFDLoop (long offset)
220222
{
221223
if (offset == 0)
222224
return false;
223-
if (ifd_offsets[file].Contains (offset))
224-
return true;
225-
ifd_offsets[file].Add (offset);
225+
226+
lock (ifd_sync_object) {
227+
if (ifd_offsets[file].Contains (offset))
228+
return true;
229+
ifd_offsets[file].Add (offset);
230+
}
231+
226232
return false;
227233
}
228234

@@ -231,13 +237,16 @@ bool DetectIFDLoop (long offset)
231237
/// </summary>
232238
void StopIFDLoopDetect ()
233239
{
234-
ifd_loopdetect_refs[file]--;
235-
if (ifd_loopdetect_refs[file] == 0) {
236-
ifd_offsets.Remove (file);
237-
ifd_loopdetect_refs.Remove (file);
240+
lock (ifd_sync_object) {
241+
ifd_loopdetect_refs[file]--;
242+
if (ifd_loopdetect_refs[file] == 0) {
243+
ifd_offsets.Remove (file);
244+
ifd_loopdetect_refs.Remove (file);
245+
}
238246
}
239247
}
240248

249+
static object ifd_sync_object = new object ();
241250
static readonly Dictionary<File, List<long>> ifd_offsets = new Dictionary<File, List<long>> ();
242251
static readonly Dictionary<File, int> ifd_loopdetect_refs = new Dictionary<File, int> ();
243252

0 commit comments

Comments
 (0)