Index: /trunk/src/VBox/VMM/CFGM.cpp
===================================================================
--- /trunk/src/VBox/VMM/CFGM.cpp	(revision 23585)
+++ /trunk/src/VBox/VMM/CFGM.cpp	(revision 23586)
@@ -340,4 +340,32 @@
 
 /**
+ * Compares two names.
+ *
+ * @returns Similar to memcpy.
+ * @param   pszName1            The first name.
+ * @param   cchName1            The length of the first name.
+ * @param   pszName2            The second name.
+ * @param   cchName2            The length of the second name.
+ */
+DECLINLINE(int) cfgmR3CompareNames(const char *pszName1, size_t cchName1, const char *pszName2, size_t cchName2)
+{
+    int iDiff;
+    if (cchName1 <= cchName2)
+    {
+        iDiff = memcmp(pszName1, pszName2, cchName1);
+        if (!iDiff && cchName1 < cchName2)
+            iDiff = -1;
+    }
+    else
+    {
+        iDiff = memcmp(pszName1, pszName2, cchName2);
+        if (!iDiff)
+            iDiff = 1;
+    }
+    return iDiff;
+}
+
+
+/**
  * Validates that the child nodes are within a set of valid names.
  *
@@ -1031,5 +1059,5 @@
             RTUINT cchName = pszNext - pszPath;
 
-            /* search child list. */
+            /* search child list. */ /** @todo the list is ordered now, consider optimizing the search. */
             pChild = pNode->pFirstChild;
             for ( ; pChild; pChild = pChild->pNext)
@@ -1071,4 +1099,5 @@
         while (pLeaf)
         {
+            /** @todo the list is ordered now, consider optimizing the search. */
             if (    cchName == pLeaf->cchName
                 && !memcmp(pszName, pLeaf->szName, cchName) )
@@ -1252,15 +1281,18 @@
              * Check if already exists and find last node in chain.
              */
-            size_t cchName = strlen(pszName);
-            PCFGMNODE pPrev = pNode->pFirstChild;
-            if (pPrev)
+            size_t    cchName = strlen(pszName);
+            PCFGMNODE pPrev   = NULL;
+            PCFGMNODE pNext   = pNode->pFirstChild;
+            if (pNext)
             {
-                for (;; pPrev = pPrev->pNext)
+                for (; pNext; pPrev = pNext, pNext = pNext->pNext)
                 {
-                    if (    cchName == pPrev->cchName
-                        &&  !memcmp(pszName, pPrev->szName, cchName))
-                        return VERR_CFGM_NODE_EXISTS;
-                    if (!pPrev->pNext)
+                    int iDiff = cfgmR3CompareNames(pszName, cchName, pNext->szName, pNext->cchName);
+                    if (iDiff <= 0)
+                    {
+                        if (!iDiff)
+                            return VERR_CFGM_NODE_EXISTS;
                         break;
+                    }
                 }
             }
@@ -1283,5 +1315,4 @@
                  * Insert into child list.
                  */
-                pNew->pNext         = NULL;
                 pNew->pPrev         = pPrev;
                 if (pPrev)
@@ -1289,4 +1320,8 @@
                 else
                     pNode->pFirstChild = pNew;
+                pNew->pNext         = pNext;
+                if (pNext)
+                    pNext->pPrev    = pNew;
+
                 if (ppChild)
                     *ppChild = pNew;
@@ -1388,15 +1423,18 @@
              * Check if already exists and find last node in chain.
              */
-            size_t cchName = strlen(pszName);
-            PCFGMLEAF pPrev = pNode->pFirstLeaf;
-            if (pPrev)
+            size_t    cchName  = strlen(pszName);
+            PCFGMLEAF pPrev    = NULL;
+            PCFGMLEAF pNext    = pNode->pFirstLeaf;
+            if (pNext)
             {
-                for (;; pPrev = pPrev->pNext)
+                for (; pNext; pPrev = pNext, pNext = pNext->pNext)
                 {
-                    if (    cchName == pPrev->cchName
-                        &&  !memcmp(pszName, pPrev->szName, cchName))
-                        return VERR_CFGM_LEAF_EXISTS;
-                    if (!pPrev->pNext)
+                    int iDiff = cfgmR3CompareNames(pszName, cchName, pNext->szName, pNext->cchName);
+                    if (iDiff <= 0)
+                    {
+                        if (!iDiff)
+                            return VERR_CFGM_LEAF_EXISTS;
                         break;
+                    }
                 }
             }
@@ -1414,5 +1452,4 @@
                  * Insert into child list.
                  */
-                pNew->pNext         = NULL;
                 pNew->pPrev         = pPrev;
                 if (pPrev)
@@ -1420,4 +1457,8 @@
                 else
                     pNode->pFirstLeaf = pNew;
+                pNew->pNext         = pNext;
+                if (pNext)
+                    pNext->pPrev    = pNew;
+
                 *ppLeaf = pNew;
                 rc = VINF_SUCCESS;
