From afc5f3466042549706cb8423bb70b106a8744b10 Mon Sep 17 00:00:00 2001
From: Tobias Boege <tobias@gambas-buch.de>
Date: Mon, 5 Oct 2015 15:41:37 +0000
Subject: [PATCH] [GB.XML.HTML] * BUG: XmlElement: Fix infinite loop from
 MatchFilter()

git-svn-id: svn://localhost/gambas/trunk@7389 867c0c6c-44f3-4631-809d-bfa615b0a4ec
---
 gb.xml/src/element.cpp | 31 +++++++++++--------------------
 1 file changed, 11 insertions(+), 20 deletions(-)

diff --git a/gb.xml/src/element.cpp b/gb.xml/src/element.cpp
index 3a6a2282d..41057095a 100644
--- a/gb.xml/src/element.cpp
+++ b/gb.xml/src/element.cpp
@@ -210,27 +210,18 @@ bool XMLElement_AttributeContains(const Element *elmt, const char *attrName, siz
 {
         Attribute *attr = XMLElement_GetAttribute(elmt, attrName, lenAttrName);
         if(!attr) return false;
-        char *pos = (char*)memchr(attr->attrValue, ' ' ,attr->lenAttrValue);
-        char *oldPos = attr->attrValue;
-        
-        while(pos)
-        {
-            if((pos + 1) == lenValue + oldPos) //(pos + 1) - oldPos == lenValue
-            {
-                if(!memcmp(value, pos + 1, lenValue)) return true;
-            }
-            oldPos = pos + 1;
-            pos = (char*)memchr(pos, ' ' ,attr->lenAttrValue - (attr->attrValue - pos));
-        }
-        
-        if(((attr->attrValue + attr->lenAttrValue))  == lenValue + oldPos)
-        {
-            if(!memcmp(value, oldPos, lenValue)) return true;
-        }
-        
-        return false;
-        
+        char *pos = attr->attrValue;
+        size_t left = attr->lenAttrValue;
 
+        while (1) {
+            if (!memcmp(value, pos, lenValue)) return true;
+            pos = (char*)memchr(pos, ' ', left);
+            if (!pos) break;
+            pos++;
+            left = attr->lenAttrValue - (pos - attr->attrValue);
+        }
+
+        return false;
 }
 
 void XMLElement_RemoveAttribute(Element *elmt, const char *attrName, size_t lenAttrName)