Skip to content

Commit e8be7f2

Browse files
Bugfix: System.Xml.Serialization: Compiler.GetTempAssemblyName is not deterministic under .NET Core (#46499)
* Bugfix: System.Xml.Serialization: Compiler.GetTempAssemblyName is not persistent under .NET Core The implementation of String.GetHashCode() was persistent by default in .NET Framework. allowing to predictably name an XmlSerializers.{HashCode}.dll containing the pre-generated serializers. In .NET Core / .NET 5, String.GetHashCode() is no longer persistent, so the value of ns.GetHashCode() will change during each run, preventing it from picking up the XmlSerializers dll correctly. * Remove trailing whitespace * Use truncated SHA512 * Fixed compilation * Dispose SHA512 instance * Fixed compilation * Update src/libraries/System.Private.Xml/src/System/Xml/Serialization/Compiler.cs Co-authored-by: Stephen Toub <stoub@microsoft.com> * Update src/libraries/System.Private.Xml/src/System/Xml/Serialization/Compiler.cs Co-authored-by: Stephen Toub <stoub@microsoft.com> * Update Compiler.cs Co-authored-by: Stephen Toub <stoub@microsoft.com>
1 parent 7eb749c commit e8be7f2

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

src/libraries/System.Private.Xml/src/System.Private.Xml.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,7 @@
567567
<Reference Include="System.Runtime.Extensions" />
568568
<Reference Include="System.Runtime.InteropServices" />
569569
<Reference Include="System.Security.Cryptography.Algorithms" />
570+
<Reference Include="System.Security.Cryptography.Primitives" />
570571
<Reference Include="System.Text.Encoding.Extensions" />
571572
<Reference Include="System.Text.RegularExpressions" />
572573
<Reference Include="System.Threading" />

src/libraries/System.Private.Xml/src/System/Xml/Serialization/Compiler.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Reflection;
4+
using System.Buffers.Binary;
55
using System.Collections;
6-
using System.IO;
76
using System.Diagnostics;
7+
using System.Diagnostics.CodeAnalysis;
88
using System.Globalization;
9+
using System.IO;
10+
using System.Reflection;
911
using System.Runtime.CompilerServices;
10-
using System.Diagnostics.CodeAnalysis;
12+
using System.Security.Cryptography;
13+
using System.Text;
1114

1215
namespace System.Xml.Serialization
1316
{
@@ -89,7 +92,14 @@ internal TextWriter Source
8992

9093
internal static string GetTempAssemblyName(AssemblyName parent, string? ns)
9194
{
92-
return parent.Name + ".XmlSerializers" + (ns == null || ns.Length == 0 ? "" : "." + ns.GetHashCode());
95+
return parent.Name + ".XmlSerializers" + (string.IsNullOrEmpty(ns) ? "" : $".{GetPersistentHashCode(ns)}");
96+
}
97+
98+
private static uint GetPersistentHashCode(string value)
99+
{
100+
byte[] valueBytes = Encoding.UTF8.GetBytes(value);
101+
byte[] hash = SHA512.HashData(valueBytes);
102+
return BinaryPrimitives.ReadUInt32BigEndian(hash);
93103
}
94104
}
95105
}

0 commit comments

Comments
 (0)