Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add null checks in various XML methods #73243

Merged
merged 2 commits into from
Aug 8, 2022
Merged

Conversation

krwq
Copy link
Member

@krwq krwq commented Aug 2, 2022

Fixes #41061

@ghost
Copy link

ghost commented Aug 2, 2022

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: -

@@ -58,16 +58,19 @@ public class XmlSchema : XmlSchemaObject

public static XmlSchema? Read(TextReader reader, ValidationEventHandler? validationEventHandler)
{
ArgumentNullException.ThrowIfNull(reader);
Copy link
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?

Copy link
Member Author

@krwq krwq Aug 2, 2022

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

Copy link
Member Author

@krwq krwq Aug 2, 2022

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)

Copy link
Member Author

@krwq krwq Aug 7, 2022

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

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

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

LGTM superficially

[Fact]
public static void XPathDocumentNullStream()
{
Assert.Throws<ArgumentNullException>(() => new XPathDocument(default(Stream)));
Copy link
Contributor

@hughbe hughbe Aug 2, 2022

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
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
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
Copy link
Member Author

krwq commented Aug 7, 2022

no changes, just rebase

@krwq
Copy link
Member Author

krwq commented Aug 7, 2022

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
Member

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

@krwq
Copy link
Member Author

krwq commented Aug 7, 2022

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