From 97b0b37aa1d6875a248751e75ad088961bfe9ff5 Mon Sep 17 00:00:00 2001 From: mayorovp Date: Wed, 26 Feb 2020 07:19:57 +0500 Subject: [PATCH] fix NRE in XNodeNavigator.MoveToNext(XPathNodeType) (#32288) * fix NRE in XNodeNavigator.MoveToNext(XPathNodeType) fixes #31710 * Adding test case Co-authored-by: buyaa-n --- .../System/Xml/XPath/FuncLocation/PathNodeTests.cs | 24 ++++++++++++++++++++++ .../src/System/Xml/XPath/XNodeNavigator.cs | 6 +++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/libraries/Common/tests/System/Xml/XPath/FuncLocation/PathNodeTests.cs b/src/libraries/Common/tests/System/Xml/XPath/FuncLocation/PathNodeTests.cs index 9b3c390..99ace64 100644 --- a/src/libraries/Common/tests/System/Xml/XPath/FuncLocation/PathNodeTests.cs +++ b/src/libraries/Common/tests/System/Xml/XPath/FuncLocation/PathNodeTests.cs @@ -7,6 +7,7 @@ using System; using System.Xml; using System.Xml.XPath; using XPathTests.Common; +using System.Xml.Linq; namespace XPathTests.FunctionalTests.Location.Paths { @@ -15,6 +16,29 @@ namespace XPathTests.FunctionalTests.Location.Paths /// public static partial class NodeTestsTests { + [Fact] + public static void PathNavigatorMoveToNext_HasNotNextNode() + { + var current = new XElement("current"); + new XElement("parent", current); + + XPathNavigator nav = current.CreateNavigator(); + Assert.False(nav.MoveToNext(XPathNodeType.Element)); + Assert.Equal("current", nav.LocalName); + } + + [Fact] + public static void PathNavigatorMoveToNext_HasNextNode() + { + var current = new XElement("current"); + var next = new XElement("next"); + new XElement("parent", current, next); + + XPathNavigator nav = current.CreateNavigator(); + Assert.True(nav.MoveToNext(XPathNodeType.Element)); + Assert.Equal("next", nav.LocalName); + } + /// /// Expected: Selects all text node descendants of the context node. /// descendant::text() diff --git a/src/libraries/System.Private.Xml.Linq/src/System/Xml/XPath/XNodeNavigator.cs b/src/libraries/System.Private.Xml.Linq/src/System/Xml/XPath/XNodeNavigator.cs index e45c917..a8b9e1e 100644 --- a/src/libraries/System.Private.Xml.Linq/src/System/Xml/XPath/XNodeNavigator.cs +++ b/src/libraries/System.Private.Xml.Linq/src/System/Xml/XPath/XNodeNavigator.cs @@ -546,9 +546,13 @@ namespace System.Xml.XPath mask &= ~TextMask; } XNode next = null; - for (XNode node = currentNode; node != null; node = next) + for (XNode node = currentNode; ; node = next) { next = node.NextNode; + if (next == null) + { + break; + } if (((1 << (int)next.NodeType) & mask) != 0 && !(node is XText && next is XText)) { _source = next; -- 2.7.4