Просмотр исходного кода

prevent endless recursion in AncestorIterator while collecting results

svn path=/trunk/mcs/; revision=95369
Konstantin Triger 18 лет назад
Родитель
Сommit
1532e9daba

+ 5 - 0
mcs/class/System.XML/System.Xml.XPath/ChangeLog

@@ -1,3 +1,8 @@
+2008-01-10  Konstantin Triger <[email protected]>
+
+	* Iterator.cs: prevent endless recursion in AncestorIterator
+		while collecting results.
+
 2008-01-12  Sebastien Pouliot  <[email protected]>
 
 	* DefaultContext.cs: Fix direct comparison with NaN. Found by 

+ 3 - 14
mcs/class/System.XML/System.Xml.XPath/Iterator.cs

@@ -367,27 +367,16 @@ namespace System.Xml.XPath
 		{
 			navigators = new ArrayList ();
 
-			XPathNavigator ancestors = startPosition.Clone ();
-			if (!ancestors.MoveToParent ())
-				return;
-			while (ancestors.NodeType != XPathNodeType.Root) {
+			XPathNavigator ancestors = startPosition.Clone ();
+			while (ancestors.NodeType != XPathNodeType.Root && ancestors.MoveToParent ())
 				navigators.Add (ancestors.Clone ());
-				ancestors.MoveToParent ();
-			}
 			currentPosition = navigators.Count;
 		}
 
 		public override bool MoveNextCore ()
 		{
-			if (navigators == null) {
+			if (navigators == null)
 				CollectResults ();
-				if (startPosition.NodeType != XPathNodeType.Root) {
-					// First time it returns Root
-					_nav.MoveToRoot ();
-					Current.MoveTo (_nav);
-					return true;
-				}
-			}
 			if (currentPosition == 0)
 				return false;
 			_nav.MoveTo ((XPathNavigator) navigators [--currentPosition]);

+ 16 - 0
mcs/class/System.XML/Test/System.Xml.XPath/SelectNodesTests.cs

@@ -276,6 +276,22 @@ namespace MonoTests.System.Xml.XPath
 			AssertEquals ("hoge", nl [1].LocalName);
 			AssertEquals ("xml", nl [2].LocalName);
 			AssertEquals (3, nl.Count);
+		}
+
+		[Test]
+		public void AncestorAxis () {
+			XmlDocument doc = new XmlDocument ();
+			doc.LoadXml ("<foo><bar><baz><bax /></baz></bar></foo>");
+
+			XmlNode bar = doc.GetElementsByTagName ("bar") [0];
+			XmlElement barClone = (XmlElement) bar.CloneNode (true);
+			XmlNodeList baxs = barClone.GetElementsByTagName ("bax");
+
+			XmlNode bax = baxs [0];
+			XmlNodeList ans = bax.SelectNodes ("ancestor::*");
+			AssertEquals (2, ans.Count);
+			AssertEquals ("bar", ans [0].Name);
+			AssertEquals ("baz", ans [1].Name);
 		}
 	}
 }