Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0075639

Browse files
authored
Nullable annotations for System.Security.Cryptography.Xml (#67198)
* First pass * More annotations * Rever mistaken change to test * More annotations * More annotations * More annotations * More annotations * More annotations * More annotations * More annotations * More annotations * More annotations * More annotations * More annotations * More annotations * Remove red flag comment * PR feedback * Remove 'red flag' comments * Revert unintended changes to tests * Revert unintended changes * PR feedback: remove redundant !'s from things that can never return null * Added `[MemberNotNullWhen(true, nameof(_cachedXml))]` on implementation. * Updated ref t ypes (with `dotnet msbuild /t:GenerateReferenceAssemblySource`) * Updated ref assembly * PR feedback: add `MemberNotNull` attribute on `Initialize`) * Updated ref again * Put `MemberNotNull` attribute on the getter rather than the property in the reference file. * PR feedback: remove MemberNotNullWhenAttribute from ref * PR feedback: non nullable param to match method in base class * Fix post-merge build errors * PR feedback * PR feedback from @bartonjs - add [DisallowNull] and cascade * PR feedback * PR feedback * PR feedback * Fix build again, by removing the `protected internal` property `CacheValid` * PR feedback * Added back `CacheValid` based on PR comment * Fix issues from recent rebase * Add `DisallowNull` attribute to ref file and remove it from `DefaultGenApiDocIds.txt` * PR feedback * Fix issues from recent merge * Remove some more extraneous damnit operators * Fix NRT analysis errors after prior rebase * PR feedback * PR feedback * PR feedback * PR feedback * PR feedback * CipherReference and CipherValue made to disallow null * Make Uri non-nullable on EnctrypedReference * Add AllowNull attribute on EncryptedType::KeyInfo * AllowNull on EncryptedXml::Recipient * Made baseUri nullable * DisallowNull on PropertlyElement * AllowNull on SignedInfo::CanonicalizationMethod * AllowNull on SignedXml::EncryptedXml * Fix build after recent rebase * Remove redundant damnit operator now that #71860 has been merged * PR feedback * Fix build due to change of the CanonicalXmlElement constructor * PR feedback * Remove redundant damnit operators * Fix broken tests * Fix issues after rebase * PR feedback * PR feedback
1 parent bc27a6a commit 0075639

File tree

63 files changed

+845
-795
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+845
-795
lines changed

src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/ConvertToLibraryImportFixer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
7474
context.Diagnostics);
7575
if (!bool.Parse(diagnostic.Properties[ConvertToLibraryImportAnalyzer.ExactSpelling]))
7676
{
77-
CharSet charSet = (CharSet)Enum.Parse(typeof(CharSet), diagnostic.Properties[ConvertToLibraryImportAnalyzer.CharSet]);
77+
CharSet charSet = (CharSet)Enum.Parse(typeof(CharSet), diagnostic.Properties[ConvertToLibraryImportAnalyzer.CharSet]!);
7878
// CharSet.Auto traditionally maps to either an A or W suffix
7979
// depending on the default CharSet of the platform.
8080
// We will offer both suffix options when CharSet.Auto is provided

src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.cs

Lines changed: 95 additions & 87 deletions
Large diffs are not rendered by default.

src/libraries/System.Security.Cryptography.Xml/ref/System.Security.Cryptography.Xml.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
4-
<Nullable>disable</Nullable>
4+
<Nullable>enable</Nullable>
55
</PropertyGroup>
66

77
<ItemGroup>

src/libraries/System.Security.Cryptography.Xml/src/System.Security.Cryptography.Xml.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
<PropertyGroup>
33
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
44
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
5-
<Nullable>disable</Nullable>
6-
<NoWarn>$(NoWarn);nullable</NoWarn>
75
<NoWarn>$(NoWarn);CA1850</NoWarn> <!-- CA1850 suppressed due to multitargeting -->
86
<IsPackable>true</IsPackable>
97
<PackageDescription>Provides classes to support the creation and validation of XML digital signatures. The classes in this namespace implement the World Wide Web Consortium Recommendation, "XML-Signature Syntax and Processing", described at http://www.w3.org/TR/xmldsig-core/.

src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AncestralNamespaceContextManager.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ internal abstract class AncestralNamespaceContextManager
1212

1313
internal NamespaceFrame GetScopeAt(int i)
1414
{
15-
return (NamespaceFrame)_ancestorStack[i];
15+
return (NamespaceFrame)_ancestorStack[i]!;
1616
}
1717

1818
internal NamespaceFrame GetCurrentScope()
1919
{
2020
return GetScopeAt(_ancestorStack.Count - 1);
2121
}
2222

23-
protected XmlAttribute GetNearestRenderedNamespaceWithMatchingPrefix(string nsPrefix, out int depth)
23+
protected XmlAttribute? GetNearestRenderedNamespaceWithMatchingPrefix(string nsPrefix, out int depth)
2424
{
25-
XmlAttribute attr;
25+
XmlAttribute? attr;
2626
depth = -1;
2727
for (int i = _ancestorStack.Count - 1; i >= 0; i--)
2828
{
@@ -35,9 +35,9 @@ protected XmlAttribute GetNearestRenderedNamespaceWithMatchingPrefix(string nsPr
3535
return null;
3636
}
3737

38-
protected XmlAttribute GetNearestUnrenderedNamespaceWithMatchingPrefix(string nsPrefix, out int depth)
38+
protected XmlAttribute? GetNearestUnrenderedNamespaceWithMatchingPrefix(string nsPrefix, out int depth)
3939
{
40-
XmlAttribute attr;
40+
XmlAttribute? attr;
4141
depth = -1;
4242
for (int i = _ancestorStack.Count - 1; i >= 0; i--)
4343
{

src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/AttributeSortOrder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ internal sealed class AttributeSortOrder : IComparer
1111
{
1212
internal AttributeSortOrder() { }
1313

14-
public int Compare(object a, object b)
14+
public int Compare(object? a, object? b)
1515
{
16-
XmlNode nodeA = a as XmlNode;
17-
XmlNode nodeB = b as XmlNode;
16+
XmlNode? nodeA = a as XmlNode;
17+
XmlNode? nodeB = b as XmlNode;
1818
if ((nodeA == null) || (nodeB == null))
1919
throw new ArgumentException();
2020
int namespaceCompare = string.CompareOrdinal(nodeA.NamespaceURI, nodeB.NamespaceURI);

src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/C14NAncestralNamespaceContextManager.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ private void GetNamespaceToRender(string nsPrefix, SortedList attrListToRender,
2626
}
2727

2828
int rDepth;
29-
XmlAttribute local = (XmlAttribute)nsLocallyDeclared[nsPrefix];
30-
XmlAttribute rAncestral = GetNearestRenderedNamespaceWithMatchingPrefix(nsPrefix, out rDepth);
29+
XmlAttribute? local = (XmlAttribute?)nsLocallyDeclared[nsPrefix];
30+
XmlAttribute? rAncestral = GetNearestRenderedNamespaceWithMatchingPrefix(nsPrefix, out rDepth);
3131
if (local != null)
3232
{
3333
if (Utils.IsNonRedundantNamespaceDecl(local, rAncestral))
@@ -42,7 +42,7 @@ private void GetNamespaceToRender(string nsPrefix, SortedList attrListToRender,
4242
else
4343
{
4444
int uDepth;
45-
XmlAttribute uAncestral = GetNearestUnrenderedNamespaceWithMatchingPrefix(nsPrefix, out uDepth);
45+
XmlAttribute? uAncestral = GetNearestUnrenderedNamespaceWithMatchingPrefix(nsPrefix, out uDepth);
4646
if (uAncestral != null && uDepth > rDepth && Utils.IsNonRedundantNamespaceDecl(uAncestral, rAncestral))
4747
{
4848
if (Utils.IsXmlNamespaceNode(uAncestral))
@@ -55,14 +55,14 @@ private void GetNamespaceToRender(string nsPrefix, SortedList attrListToRender,
5555

5656
internal override void GetNamespacesToRender(XmlElement element, SortedList attrListToRender, SortedList nsListToRender, Hashtable nsLocallyDeclared)
5757
{
58-
XmlAttribute attrib;
58+
XmlAttribute? attrib;
5959
object[] attrs = new object[nsLocallyDeclared.Count];
6060
nsLocallyDeclared.Values.CopyTo(attrs, 0);
6161
foreach (object a in attrs)
6262
{
6363
attrib = (XmlAttribute)a;
6464
int rDepth;
65-
XmlAttribute rAncestral = GetNearestRenderedNamespaceWithMatchingPrefix(Utils.GetNamespacePrefix(attrib), out rDepth);
65+
XmlAttribute? rAncestral = GetNearestRenderedNamespaceWithMatchingPrefix(Utils.GetNamespacePrefix(attrib), out rDepth);
6666
if (Utils.IsNonRedundantNamespaceDecl(attrib, rAncestral))
6767
{
6868
nsLocallyDeclared.Remove(Utils.GetNamespacePrefix(attrib));

src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXml.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ internal sealed class CanonicalXml
1717
// private static string defaultXPathWithComments = "(//. | //@* | //namespace::*)";
1818
// private static string defaultXPathWithComments = "(//. | //@* | //namespace::*)";
1919

20-
internal CanonicalXml(Stream inputStream, bool includeComments, XmlResolver resolver, string strBaseUri)
20+
internal CanonicalXml(Stream inputStream, bool includeComments, XmlResolver? resolver, string strBaseUri)
2121
{
2222
if (inputStream is null)
2323
{
@@ -30,8 +30,8 @@ internal CanonicalXml(Stream inputStream, bool includeComments, XmlResolver reso
3030
_ancMgr = new C14NAncestralNamespaceContextManager();
3131
}
3232

33-
internal CanonicalXml(XmlDocument document, XmlResolver resolver) : this(document, resolver, false) { }
34-
internal CanonicalXml(XmlDocument document, XmlResolver resolver, bool includeComments)
33+
internal CanonicalXml(XmlDocument document, XmlResolver? resolver) : this(document, resolver, false) { }
34+
internal CanonicalXml(XmlDocument document, XmlResolver? resolver, bool includeComments)
3535
{
3636
if (document is null)
3737
{
@@ -44,14 +44,14 @@ internal CanonicalXml(XmlDocument document, XmlResolver resolver, bool includeCo
4444
_ancMgr = new C14NAncestralNamespaceContextManager();
4545
}
4646

47-
internal CanonicalXml(XmlNodeList nodeList, XmlResolver resolver, bool includeComments)
47+
internal CanonicalXml(XmlNodeList nodeList, XmlResolver? resolver, bool includeComments)
4848
{
4949
if (nodeList is null)
5050
{
5151
throw new ArgumentNullException(nameof(nodeList));
5252
}
5353

54-
XmlDocument doc = Utils.GetOwnerDocument(nodeList);
54+
XmlDocument? doc = Utils.GetOwnerDocument(nodeList);
5555
if (doc == null)
5656
throw new ArgumentException(nameof(nodeList));
5757

@@ -79,8 +79,8 @@ private static void MarkInclusionStateForNodes(XmlNodeList nodeList, XmlDocument
7979

8080
do
8181
{
82-
XmlNode currentNode = (XmlNode)elementList[index];
83-
XmlNode currentNodeCanonical = (XmlNode)elementListCanonical[index];
82+
XmlNode currentNode = (XmlNode)elementList[index]!;
83+
XmlNode currentNodeCanonical = (XmlNode)elementListCanonical[index]!;
8484
XmlNodeList childNodes = currentNode.ChildNodes;
8585
XmlNodeList childNodesCanonical = currentNodeCanonical.ChildNodes;
8686
for (int i = 0; i < childNodes.Count; i++)
@@ -90,17 +90,17 @@ private static void MarkInclusionStateForNodes(XmlNodeList nodeList, XmlDocument
9090

9191
if (Utils.NodeInList(childNodes[i], nodeList))
9292
{
93-
MarkNodeAsIncluded(childNodesCanonical[i]);
93+
MarkNodeAsIncluded(childNodesCanonical[i]!);
9494
}
9595

96-
XmlAttributeCollection attribNodes = childNodes[i].Attributes;
96+
XmlAttributeCollection? attribNodes = childNodes[i]!.Attributes;
9797
if (attribNodes != null)
9898
{
9999
for (int j = 0; j < attribNodes.Count; j++)
100100
{
101101
if (Utils.NodeInList(attribNodes[j], nodeList))
102102
{
103-
MarkNodeAsIncluded(childNodesCanonical[i].Attributes.Item(j));
103+
MarkNodeAsIncluded(childNodesCanonical[i]!.Attributes!.Item(j)!);
104104
}
105105
}
106106
}
@@ -121,7 +121,7 @@ internal byte[] GetDigestedBytes(HashAlgorithm hash)
121121
{
122122
_c14nDoc.WriteHash(hash, DocPosition.BeforeRootElement, _ancMgr);
123123
hash.TransformFinalBlock(Array.Empty<byte>(), 0, 0);
124-
byte[] res = (byte[])hash.Hash.Clone();
124+
byte[] res = (byte[])hash.Hash!.Clone();
125125
// reinitialize the hash so it is still usable after the call
126126
hash.Initialize();
127127
return res;

src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal sealed class CanonicalXmlAttribute : XmlAttribute, ICanonicalizableNode
1111
{
1212
private bool _isInNodeSet;
1313

14-
public CanonicalXmlAttribute(string prefix, string localName, string namespaceURI, XmlDocument doc, bool defaultNodeSetInclusionState)
14+
public CanonicalXmlAttribute(string? prefix, string localName, string? namespaceURI, XmlDocument doc, bool defaultNodeSetInclusionState)
1515
: base(prefix, localName, namespaceURI, doc)
1616
{
1717
IsInNodeSet = defaultNodeSetInclusionState;

src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/CanonicalXmlCDataSection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace System.Security.Cryptography.Xml
1010
internal sealed class CanonicalXmlCDataSection : XmlCDataSection, ICanonicalizableNode
1111
{
1212
private bool _isInNodeSet;
13-
public CanonicalXmlCDataSection(string data, XmlDocument doc, bool defaultNodeSetInclusionState) : base(data, doc)
13+
public CanonicalXmlCDataSection(string? data, XmlDocument doc, bool defaultNodeSetInclusionState) : base(data, doc)
1414
{
1515
_isInNodeSet = defaultNodeSetInclusionState;
1616
}

0 commit comments

Comments
 (0)