Skip to content
New issue

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

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

Already on GitHub? # to your account

fix for possible Null Reference Exception #112412

Merged
merged 3 commits into from
Feb 12, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private void LoadDocSequence(XmlDocument parentDoc)
XmlNode n = LoadNode(true)!;

// Move to the next node
if (n.NodeType != XmlNodeType.Attribute)
if (n != null && n.NodeType != XmlNodeType.Attribute)
Copy link
Member

Choose a reason for hiding this comment

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

The null-forgiving operator in the previous line explicitly expressed that null shouldn't be returned in this case.

Reading through the implementation, null can only be returned in case of malformed xml (end element without starting element). In this case, the behavior should be reconsidered.

Copy link
Member

Choose a reason for hiding this comment

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

It may be more appropriate to fix it like this then?

XmlNode? n = LoadNode(true);
Debug.Assert(n != null);

Copy link
Contributor Author

@dmitr1ibo4kar3v dmitr1ibo4kar3v Feb 11, 2025

Choose a reason for hiding this comment

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

Thanks for the tip. I used option with "Debug.Assert".

reader.Read();

return n;
Expand Down
Loading