fix NRE in XNodeNavigator.MoveToNext(XPathNodeType) (#32288)
authormayorovp <mayorovp@gmail.com>
Wed, 26 Feb 2020 02:19:57 +0000 (07:19 +0500)
committerGitHub <noreply@github.com>
Wed, 26 Feb 2020 02:19:57 +0000 (18:19 -0800)
* fix NRE in XNodeNavigator.MoveToNext(XPathNodeType)

fixes #31710

* Adding test case

Co-authored-by: buyaa-n <buyankhishig.namnan@microsoft.com>
src/libraries/Common/tests/System/Xml/XPath/FuncLocation/PathNodeTests.cs
src/libraries/System.Private.Xml.Linq/src/System/Xml/XPath/XNodeNavigator.cs

index 9b3c390..99ace64 100644 (file)
@@ -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
     /// </summary>
     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);
+        }
+
         /// <summary>
         /// Expected: Selects all text node descendants of the context node.
         /// descendant::text()
index e45c917..a8b9e1e 100644 (file)
@@ -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;