Skip to content

Add null checks in various XML methods - #73243

Merged
krwq merged 2 commits into
dotnet:mainfrom
krwq:nullrefs-in-xml
Aug 8, 2022
Merged

krwq merged 2 commits into
dotnet:mainfrom
krwq:nullrefs-in-xml

Conversation

@krwq

@krwq krwq commented Aug 2, 2022

Copy link
Copy Markdown
Member

Fixes #41061

@krwq
krwq requested a review from eiriktsarpalis August 2, 2022 16:37
@ghost ghost added the area-System.Xml label Aug 2, 2022
@ghost ghost assigned krwq Aug 2, 2022
@ghost

ghost commented Aug 2, 2022

Copy link
Copy Markdown

Tagging subscribers to this area: @dotnet/area-system-xml
See info in area-owners.md if you want to be subscribed.

Issue Details

Fixes #41061

Author: krwq
Assignees: -
Labels:

area-System.Xml

Milestone: -

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the behavior before the check was added? NullReferenceException?

@krwq krwq Aug 2, 2022 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NullRef for almost all, I can't remember which one but one of them also failed in some other unexpected way

@krwq krwq Aug 2, 2022 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Root element is missing." for XPathDocument..ctor(TextReader) when TextReader is null. That's just purely incorrect (we passed it into some other methods without validation until one of them eventually failed with random exception)

@krwq krwq Aug 7, 2022 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems it was also XmlSchema.Write and there is testcase which is testing that which failed (I missed it on the CI because of other unrelated errors). I'll go ahead and change that behavior since this error doesn't seem particularly useful:

Unhandled exception. System.InvalidOperationException: There was an error generating the XML document.
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Xml.Serialization.XmlSerializationWriter.WriteStartDocument()
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterXmlSchema.Write64_schema(Object o)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Schema.XmlSchema.Write(XmlWriter writer, XmlNamespaceManager namespaceManager)
   at XmlSchemaWriteRepro.Program.Main(String[] args) in C:\Users\kwicher\source\repos\XmlSchemaWriteRepro\XmlSchemaWriteRepro\Program.cs:line 13

@eiriktsarpalis eiriktsarpalis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM superficially

@hughbe hughbe Aug 2, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not (Stream)null?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is also TextReader overload and we want to make sure it picks the right one

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apples vs oranges. No strong preference either way. More important to test behavior rather than if we do it one way or another

@krwq
krwq force-pushed the nullrefs-in-xml branch from 0050aad to 85e99f5 Compare August 7, 2022 15:11
@krwq

krwq commented Aug 7, 2022

Copy link
Copy Markdown
Member Author

no changes, just rebase

@krwq

krwq commented Aug 7, 2022 •

Copy link
Copy Markdown
Member Author

Seems there was a test failure where we for some reason expected InvalidOperationException for null argument (with completely useless error message and NullRef on the inner exception), see here for more details: #73243 (comment) - I went ahead and removed this (since I already added test asserting ArgumentNullException) - I don't expect any particular impact from this change since it only happens for null arguments which suggests design error rather than something which app could potentially catch - makes more sense to throw more useful error message. Also now we match the behavior written in docs

@danmoseley

Copy link
Copy Markdown
Contributor

Any of these worth a breaking change note? (I'm guessing not)

@krwq

krwq commented Aug 7, 2022

Copy link
Copy Markdown
Member Author

I don't think so, we're now matching behavior to already documented behavior and this exception seems very unlikely to be found by random input - most likely will be only seen during app-design time by developer.

@krwq
krwq merged commit be33bc7 into dotnet:main Aug 8, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Sep 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NullReferenceException in XML found during nullability annotations

5 participants