Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Fix race conditions in DatataContractSerialization (#31065)
Browse files Browse the repository at this point in the history
* Fix some race conditions in DatataContractSerialization
  • Loading branch information
jiayi11 authored Jul 16, 2018
1 parent 06b5dab commit 2b9fa07
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/Common/tests/System/Runtime/Serialization/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -35,8 +35,8 @@ internal static void Equal<T>(T[] x, T[] y, Func<T, T, bool> f)
}
}

private static Dictionary<string, string> s_prefixToNamespaceDesk = new Dictionary<string, string>();
private static Dictionary<string, string> s_prefixToNamespaceCoreCLR = new Dictionary<string, string>();
private static ConcurrentDictionary<string, string> s_prefixToNamespaceDesk = new ConcurrentDictionary<string, string>();
private static ConcurrentDictionary<string, string> s_prefixToNamespaceCoreCLR = new ConcurrentDictionary<string, string>();

internal struct CompareResult
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Xml;
using System.Collections.Generic;
using System.Collections;

namespace System.Runtime.Serialization
{
Expand Down Expand Up @@ -37,20 +37,22 @@ private enum ExtensionDataNodeType
private int _attributeCount;
private int _attributeIndex;

private static object s_prefixLock = new object();

#pragma warning disable 0649
private XmlNodeReader _xmlNodeReader;
#pragma warning restore 0649

private XmlObjectSerializerReadContext _context;

private static Dictionary<string, string> s_nsToPrefixTable;
private static Hashtable s_nsToPrefixTable;

private static Dictionary<string, string> s_prefixToNsTable;
private static Hashtable s_prefixToNsTable;

static ExtensionDataReader()
{
s_nsToPrefixTable = new Dictionary<string, string>();
s_prefixToNsTable = new Dictionary<string, string>();
s_nsToPrefixTable = new Hashtable();
s_prefixToNsTable = new Hashtable();
AddPrefix(Globals.XsiPrefix, Globals.SchemaInstanceNamespace);
AddPrefix(Globals.SerPrefix, Globals.SerializationNamespace);
AddPrefix(string.Empty, string.Empty);
Expand Down Expand Up @@ -205,10 +207,7 @@ public override string LookupNamespace(string prefix)
if (IsXmlDataNode)
return _xmlNodeReader.LookupNamespace(prefix);

string ns;
if (!s_prefixToNsTable.TryGetValue(prefix, out ns))
return null;
return ns;
return (string)s_prefixToNsTable[prefix];
}

public override void Skip()
Expand Down Expand Up @@ -482,13 +481,14 @@ private ElementData GetNextElement()

internal static string GetPrefix(string ns)
{
string prefix;
ns = ns ?? string.Empty;
if (!s_nsToPrefixTable.TryGetValue(ns, out prefix))
string prefix = (string)s_nsToPrefixTable[ns];
if (prefix == null)
{
lock (s_nsToPrefixTable)
lock (s_prefixLock)
{
if (!s_nsToPrefixTable.TryGetValue(ns, out prefix))
prefix = (string)s_nsToPrefixTable[ns];
if (prefix == null)
{
prefix = (ns == null || ns.Length == 0) ? string.Empty : "p" + s_nsToPrefixTable.Count;
AddPrefix(prefix, ns);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Runtime.Serialization;
using System.Xml;

Expand All @@ -13,7 +13,7 @@ namespace SerializationTestTypes
public class PrimitiveTypeResolver : DataContractResolver
{
private readonly static string s_defaultNS = "http://www.default.com";
private readonly static Dictionary<string, Type> s_types = new Dictionary<string, Type>();
private readonly static ConcurrentDictionary<string, Type> s_types = new ConcurrentDictionary<string, Type>();

public override bool TryResolveType(Type dcType, Type declaredType, DataContractResolver KTResolver, out XmlDictionaryString typeName, out XmlDictionaryString typeNamespace)
{
Expand Down

0 comments on commit 2b9fa07

Please sign in to comment.