Skip to content

Commit

Permalink
Fixed KeyNotFoundException in IFDReader: added synchronization locks …
Browse files Browse the repository at this point in the history
…to protect class members ifd_offsets and ifd_loopdetect_refs from being modified from multiple thread simultaneously.
  • Loading branch information
ymalich authored and decriptor committed Aug 31, 2024
1 parent 88343ab commit 74687bf
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/TaglibSharp/IFD/IFDReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,13 @@ public void Read (int count)
/// </summary>
void StartIFDLoopDetect ()
{
if (!ifd_offsets.ContainsKey (file)) {
ifd_offsets[file] = new List<long> ();
ifd_loopdetect_refs[file] = 1;
} else {
ifd_loopdetect_refs[file]++;
lock (ifd_sync_object) {
if (!ifd_offsets.ContainsKey (file)) {
ifd_offsets[file] = new List<long> ();
ifd_loopdetect_refs[file] = 1;
} else {
ifd_loopdetect_refs[file]++;
}
}
}

Expand All @@ -220,9 +222,13 @@ bool DetectIFDLoop (long offset)
{
if (offset == 0)
return false;
if (ifd_offsets[file].Contains (offset))
return true;
ifd_offsets[file].Add (offset);

lock (ifd_sync_object) {
if (ifd_offsets[file].Contains (offset))
return true;
ifd_offsets[file].Add (offset);
}

return false;
}

Expand All @@ -231,13 +237,16 @@ bool DetectIFDLoop (long offset)
/// </summary>
void StopIFDLoopDetect ()
{
ifd_loopdetect_refs[file]--;
if (ifd_loopdetect_refs[file] == 0) {
ifd_offsets.Remove (file);
ifd_loopdetect_refs.Remove (file);
lock (ifd_sync_object) {
ifd_loopdetect_refs[file]--;
if (ifd_loopdetect_refs[file] == 0) {
ifd_offsets.Remove (file);
ifd_loopdetect_refs.Remove (file);
}
}
}

static object ifd_sync_object = new object ();
static readonly Dictionary<File, List<long>> ifd_offsets = new Dictionary<File, List<long>> ();
static readonly Dictionary<File, int> ifd_loopdetect_refs = new Dictionary<File, int> ();

Expand Down

0 comments on commit 74687bf

Please sign in to comment.