Skip to content

Commit

Permalink
BUGFIX: Resolve concurrency issue in string heap caches.
Browse files Browse the repository at this point in the history
  • Loading branch information
Washi1337 committed May 17, 2024
1 parent a640a82 commit b1ded1f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Text;
using System.Collections.Concurrent;
using AsmResolver.IO;

namespace AsmResolver.PE.DotNet.Metadata.Strings
Expand All @@ -9,7 +8,7 @@ namespace AsmResolver.PE.DotNet.Metadata.Strings
/// </summary>
public class SerializedStringsStream : StringsStream
{
private readonly Dictionary<uint, Utf8String> _cachedStrings = new();
private readonly ConcurrentDictionary<uint, Utf8String> _cachedStrings = new();
private readonly BinaryStreamReader _reader;

/// <summary>
Expand Down Expand Up @@ -66,7 +65,7 @@ public SerializedStringsStream(string name, in BinaryStreamReader reader)
{
var stringsReader = _reader.ForkRelative(index);
value = stringsReader.ReadUtf8String();
_cachedStrings[index] = value;
_cachedStrings.TryAdd(index, value);
}

return value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Text;
using AsmResolver.IO;

Expand All @@ -9,7 +9,7 @@ namespace AsmResolver.PE.DotNet.Metadata.UserStrings
/// </summary>
public class SerializedUserStringsStream : UserStringsStream
{
private readonly Dictionary<uint, string?> _cachedStrings = new();
private readonly ConcurrentDictionary<uint, string?> _cachedStrings = new();
private readonly BinaryStreamReader _reader;

/// <summary>
Expand Down Expand Up @@ -79,7 +79,7 @@ public SerializedUserStringsStream(string name, BinaryStreamReader reader)
value = Encoding.Unicode.GetString(data, 0, actualLength - 1);
}

_cachedStrings[index] = value;
_cachedStrings.TryAdd(index, value);
}

return value;
Expand Down

0 comments on commit b1ded1f

Please sign in to comment.