Repository navigation
Closed
Description
Repro:
var doc = new XPathDocument(default(Stream));Call stack:
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at System.Xml.XmlReader.CalcBufferSize(Stream input)
at System.Xml.XmlTextReaderImpl.InitStreamInput(Uri baseUri, String baseUriStr, Stream stream, Byte[] bytes, Int32 byteCount, Encoding encoding)
at System.Xml.XmlTextReaderImpl..ctor(String url, Stream input, XmlNameTable nt)
at System.Xml.XPath.XPathDocument..ctor(Stream stream)
at Program.Main(String[] args) in C:\Users\david\source\repos\ConsoleApp57\ConsoleApp57\Program.cs:line 20
As per documentation, this should throw ArgumentNullException instead.
In addition, if the TextReader overload receives null, it throws the below exception, which seems inconsistent.
Unhandled exception. System.Xml.XmlException: Root element is missing.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XPath.XPathDocument.LoadFromReader(XmlReader reader, XmlSpace space)
at System.Xml.XPath.XPathDocument..ctor(TextReader textReader)
at Program.Main(String[] args) in C:\Users\david\source\repos\ConsoleApp57\ConsoleApp57\Program.cs:line 21
Other cases:
- XmlSchema.cs:
public static XmlSchema? Read(XmlReader reader, ValidationEventHandler validationEventHandler)will throw when reader is null - XmlSchema.cs
public void Write(TextWriter writer)andpublic void Write(TextWriter writer, XmlNamespaceManager? namespaceManager)will throw when writer is null - CodeIdentifier.cs
public static string MakeValid(string identifier)will throw on null identifier - SoapAttributes.cs
public SoapAttributes(ICustomAttributeProvider provider)when provider is null - SoapReflectionImporter.cs
public void IncludeTypes(ICustomAttributeProvider provider)will throw on null provder - SoapReflectionImporter.cs
public XmlMembersMapping ImportMembersMapping(string? elementName, string? ns, XmlReflectionMember[] members, bool hasWrapperElement, bool writeAccessors, bool validate, XmlMappingAccess access)and similar overloads will throw NRE when members is null - XmlAttributes.cs
public XmlAttributes(ICustomAttributeProvider provider)will throw on null provider - XmlReflectionMember.cs properties
XmlAttributesandSoapAttributesare not guarded against null but code will throw later if you assign null (either guard or default to something on null)
cc @krwq @buyaa-n
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
krwq commentedon Aug 20, 2020
Thanks for finding. This seems like straight forward fix with relatively low risk. I'm still not sure this meets the bar for 5.0.0 considering we've lived with this bug for several years even before .NET Core
[-]NullReferenceException when XPathDocument ctor receives null Stream[/-][+]NullReferenceException in XML found during nullability annotations[/+]krwq commentedon Aug 26, 2020
I'm making this an uber issue for XML NREs
Fix dotnet#41061: Add null checks in various XML methods
Add null checks in various XML methods (#73243)