Skip to content

NullReferenceException in XML found during nullability annotations #41061

Closed

Description

@jozkee

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) and public 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 XmlAttributes and SoapAttributes are 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

Activity

  1. added
    untriaged
    on Aug 19, 2020
  2. krwq commented on Aug 20, 2020

    @krwq
    Member

    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

  3. changed the title [-]NullReferenceException when XPathDocument ctor receives null Stream[/-] [+]NullReferenceException in XML found during nullability annotations[/+] on Aug 26, 2020
  4. krwq commented on Aug 26, 2020

    @krwq
    Member

    I'm making this an uber issue for XML NREs

  5. removed
    untriaged
    on Aug 29, 2020
  6. added this to the 6.0.0 milestone on Aug 29, 2020
  7. modified the milestones: 6.0.0, 7.0.0 on Jul 19, 2021
  8. self-assigned this
    on Aug 2, 2022
  9. ghost added
    in-pr
    on Aug 2, 2022
  10. added a commit that references this issue on Aug 7, 2022
    85e99f5
  11. added a commit that references this issue on Aug 8, 2022
    be33bc7
  12. Repository owner moved this from Future to Done in Triage POD for Reflection, META, etc.on Aug 8, 2022
  13. ghost removed
    in-pr
    on Aug 8, 2022
  14. ghost locked as resolved and limited conversation to collaborators on Sep 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions