Make CXFA_NodeHelper::CountSiblings() return a vector.

Instead of returning the count of the siblings as an int32_t, and saving
the list of siblings in an in-out parameter. Rename it to GetSiblings().
Adjust the internal TraverseSiblings() helper function to stop counting
as well.

As a result, make GetIndex() return size_t since it also uses
TraverseSiblings() internally. Change many GetIndex() callers to use
size_t as well. Also clean up the CFXJSE_ResolveProcessor methods that
call GetIndex().

Change-Id: Icba3648eb091100beed5162e6b9336de3c122a80
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/52182
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/fxjs/xfa/cfxjse_engine.cpp b/fxjs/xfa/cfxjse_engine.cpp
index 26495e9..49ad92d 100644
--- a/fxjs/xfa/cfxjse_engine.cpp
+++ b/fxjs/xfa/cfxjse_engine.cpp
@@ -757,13 +757,13 @@
   return pValue;
 }
 
-int32_t CFXJSE_Engine::GetIndexByName(CXFA_Node* refNode) {
+size_t CFXJSE_Engine::GetIndexByName(CXFA_Node* refNode) {
   return CXFA_NodeHelper::GetIndex(refNode, XFA_LOGIC_Transparent,
                                    CXFA_NodeHelper::NodeIsProperty(refNode),
                                    false);
 }
 
-int32_t CFXJSE_Engine::GetIndexByClassName(CXFA_Node* refNode) {
+size_t CFXJSE_Engine::GetIndexByClassName(CXFA_Node* refNode) {
   return CXFA_NodeHelper::GetIndex(refNode, XFA_LOGIC_Transparent,
                                    CXFA_NodeHelper::NodeIsProperty(refNode),
                                    true);
diff --git a/fxjs/xfa/cfxjse_engine.h b/fxjs/xfa/cfxjse_engine.h
index 66a7944..524e10b 100644
--- a/fxjs/xfa/cfxjse_engine.h
+++ b/fxjs/xfa/cfxjse_engine.h
@@ -84,8 +84,8 @@
   void AddToCacheList(std::unique_ptr<CXFA_List> pList);
   CXFA_Object* GetThisObject() const { return m_pThisObject.Get(); }
 
-  int32_t GetIndexByName(CXFA_Node* refNode);
-  int32_t GetIndexByClassName(CXFA_Node* refNode);
+  size_t GetIndexByName(CXFA_Node* refNode);
+  size_t GetIndexByClassName(CXFA_Node* refNode);
   WideString GetSomExpression(CXFA_Node* refNode);
 
   void SetNodesOfRunScript(std::vector<CXFA_Node*>* pArray);
diff --git a/fxjs/xfa/cfxjse_resolveprocessor.cpp b/fxjs/xfa/cfxjse_resolveprocessor.cpp
index 1605be6..c8eeb77 100644
--- a/fxjs/xfa/cfxjse_resolveprocessor.cpp
+++ b/fxjs/xfa/cfxjse_resolveprocessor.cpp
@@ -25,10 +25,39 @@
 #include "xfa/fxfa/parser/xfa_resolvenode_rs.h"
 #include "xfa/fxfa/parser/xfa_utils.h"
 
+namespace {
+
+void DoPredicateFilter(WideString wsCondition,
+                       size_t iFoundCount,
+                       CFXJSE_ResolveNodeData* pRnd) {
+  ASSERT(iFoundCount == pRnd->m_Objects.size());
+  WideString wsExpression;
+  CXFA_Script::Type eLangType = CXFA_Script::Type::Unknown;
+  if (wsCondition.Left(2).EqualsASCII(".[") && wsCondition.Last() == L']')
+    eLangType = CXFA_Script::Type::Formcalc;
+  else if (wsCondition.Left(2).EqualsASCII(".(") && wsCondition.Last() == L')')
+    eLangType = CXFA_Script::Type::Javascript;
+  else
+    return;
+
+  wsExpression = wsCondition.Mid(2, wsCondition.GetLength() - 3);
+  for (size_t i = iFoundCount; i > 0; --i) {
+    auto pRetValue =
+        pdfium::MakeUnique<CFXJSE_Value>(pRnd->m_pSC->GetIsolate());
+    bool bRet =
+        pRnd->m_pSC->RunScript(eLangType, wsExpression.AsStringView(),
+                               pRetValue.get(), pRnd->m_Objects[i - 1].Get());
+    if (!bRet || !pRetValue->ToBoolean())
+      pRnd->m_Objects.erase(pRnd->m_Objects.begin() + i - 1);
+  }
+}
+
+}  // namespace
+
 CFXJSE_ResolveProcessor::CFXJSE_ResolveProcessor()
     : m_pNodeHelper(pdfium::MakeUnique<CXFA_NodeHelper>()) {}
 
-CFXJSE_ResolveProcessor::~CFXJSE_ResolveProcessor() {}
+CFXJSE_ResolveProcessor::~CFXJSE_ResolveProcessor() = default;
 
 bool CFXJSE_ResolveProcessor::Resolve(CFXJSE_ResolveNodeData& rnd) {
   if (!rnd.m_CurObject)
@@ -79,7 +108,7 @@
       return true;
     }
     if (!rnd.m_Objects.empty())
-      FilterCondition(rnd, rnd.m_wsCondition);
+      FilterCondition(rnd.m_wsCondition, &rnd);
 
     return !rnd.m_Objects.empty();
   }
@@ -110,14 +139,16 @@
     return true;
   }
 
-  std::vector<CXFA_Node*> tempNodes;
+  std::vector<CXFA_Node*> nodes;
   for (const auto& pObject : rnd.m_Objects)
-    tempNodes.push_back(pObject->AsNode());
-  CXFA_NodeHelper::CountSiblings(findNode, XFA_LOGIC_Transparent, &tempNodes,
-                                 bClassName);
+    nodes.push_back(pObject->AsNode());
+
+  std::vector<CXFA_Node*> siblings =
+      CXFA_NodeHelper::GetSiblings(findNode, XFA_LOGIC_Transparent, bClassName);
+  nodes.insert(nodes.end(), siblings.begin(), siblings.end());
   rnd.m_Objects =
-      std::vector<UnownedPtr<CXFA_Object>>(tempNodes.begin(), tempNodes.end());
-  FilterCondition(rnd, wsCondition);
+      std::vector<UnownedPtr<CXFA_Object>>(nodes.begin(), nodes.end());
+  FilterCondition(wsCondition, &rnd);
   return !rnd.m_Objects.empty();
 }
 
@@ -142,8 +173,7 @@
       rnd.m_Objects.emplace_back(pObjNode);
   }
   if (!rnd.m_Objects.empty())
-    FilterCondition(rnd, wsCondition);
-
+    FilterCondition(wsCondition, &rnd);
   return !rnd.m_Objects.empty();
 }
 
@@ -266,7 +296,7 @@
       rndFind.m_Objects.clear();
     }
     if (rnd.m_Objects.size() > nNum) {
-      FilterCondition(rnd, wsCondition);
+      FilterCondition(wsCondition, &rnd);
       return !rnd.m_Objects.empty();
     }
   }
@@ -304,9 +334,9 @@
       if (!(dwStyles & XFA_RESOLVENODE_ALL)) {
         std::vector<CXFA_Node*> upArrayNodes;
         if (CXFA_NodeHelper::NodeIsTransparent(ToNode(curNode))) {
-          CXFA_NodeHelper::CountSiblings(
+          upArrayNodes = CXFA_NodeHelper::GetSiblings(
               ToNode(rnd.m_Objects.front().Get()), XFA_LOGIC_Transparent,
-              &upArrayNodes, !!(dwStyles & XFA_RESOLVENODE_TagName));
+              !!(dwStyles & XFA_RESOLVENODE_TagName));
         }
         if (upArrayNodes.size() > rnd.m_Objects.size()) {
           CXFA_Object* pSaveObject = rnd.m_Objects.front().Get();
@@ -315,7 +345,7 @@
           rnd.m_Objects.front() = pSaveObject;
         }
       }
-      FilterCondition(rnd, wsCondition);
+      FilterCondition(wsCondition, &rnd);
       return !rnd.m_Objects.empty();
     }
   }
@@ -337,7 +367,7 @@
       }
     }
     if (rnd.m_Objects.size() > nNum) {
-      FilterCondition(rnd, wsCondition);
+      FilterCondition(wsCondition, &rnd);
       return !rnd.m_Objects.empty();
     }
 
@@ -371,7 +401,7 @@
   if (!parentNode) {
     if (uCurClassHash == uNameHash) {
       rnd.m_Objects.emplace_back(curNode->AsNode());
-      FilterCondition(rnd, wsCondition);
+      FilterCondition(wsCondition, &rnd);
       if (!rnd.m_Objects.empty())
         return true;
     }
@@ -439,10 +469,9 @@
     }
     if (rnd.m_Objects.size() > nNum) {
       if (CXFA_NodeHelper::NodeIsTransparent(parentNode)) {
-        std::vector<CXFA_Node*> upArrayNodes;
-        CXFA_NodeHelper::CountSiblings(ToNode(rnd.m_Objects.front().Get()),
-                                       XFA_LOGIC_Transparent, &upArrayNodes,
-                                       !!(dwStyles & XFA_RESOLVENODE_TagName));
+        std::vector<CXFA_Node*> upArrayNodes = CXFA_NodeHelper::GetSiblings(
+            ToNode(rnd.m_Objects.front().Get()), XFA_LOGIC_Transparent,
+            !!(dwStyles & XFA_RESOLVENODE_TagName));
         if (upArrayNodes.size() > rnd.m_Objects.size()) {
           CXFA_Object* pSaveObject = rnd.m_Objects.front().Get();
           rnd.m_Objects = std::vector<UnownedPtr<CXFA_Object>>(
@@ -450,7 +479,7 @@
           rnd.m_Objects.front() = pSaveObject;
         }
       }
-      FilterCondition(rnd, wsCondition);
+      FilterCondition(wsCondition, &rnd);
       return !rnd.m_Objects.empty();
     }
   }
@@ -565,14 +594,14 @@
   return nStart;
 }
 
-void CFXJSE_ResolveProcessor::ConditionArray(int32_t iCurIndex,
+void CFXJSE_ResolveProcessor::ConditionArray(size_t iCurIndex,
                                              WideString wsCondition,
-                                             int32_t iFoundCount,
-                                             CFXJSE_ResolveNodeData& rnd) {
-  int32_t iLen = wsCondition.GetLength();
+                                             size_t iFoundCount,
+                                             CFXJSE_ResolveNodeData* pRnd) {
+  size_t iLen = wsCondition.GetLength();
   bool bRelative = false;
   bool bAll = false;
-  int32_t i = 1;
+  size_t i = 1;
   for (; i < iLen; ++i) {
     wchar_t ch = wsCondition[i];
     if (ch == ' ')
@@ -585,18 +614,18 @@
     break;
   }
   if (bAll) {
-    if (rnd.m_dwStyles & XFA_RESOLVENODE_CreateNode) {
-      if (rnd.m_dwStyles & XFA_RESOLVENODE_Bind) {
-        m_pNodeHelper->m_pCreateParent = ToNode(rnd.m_CurObject.Get());
+    if (pRnd->m_dwStyles & XFA_RESOLVENODE_CreateNode) {
+      if (pRnd->m_dwStyles & XFA_RESOLVENODE_Bind) {
+        m_pNodeHelper->m_pCreateParent = ToNode(pRnd->m_CurObject.Get());
         m_pNodeHelper->m_iCreateCount = 1;
-        rnd.m_Objects.clear();
+        pRnd->m_Objects.clear();
         m_pNodeHelper->m_iCurAllStart = -1;
         m_pNodeHelper->m_pAllStartParent = nullptr;
       } else if (m_pNodeHelper->m_iCurAllStart == -1) {
         m_pNodeHelper->m_iCurAllStart = m_iCurStart;
-        m_pNodeHelper->m_pAllStartParent = ToNode(rnd.m_CurObject.Get());
+        m_pNodeHelper->m_pAllStartParent = ToNode(pRnd->m_CurObject.Get());
       }
-    } else if (rnd.m_dwStyles & XFA_RESOLVENODE_BindNew) {
+    } else if (pRnd->m_dwStyles & XFA_RESOLVENODE_BindNew) {
       if (m_pNodeHelper->m_iCurAllStart == -1)
         m_pNodeHelper->m_iCurAllStart = m_iCurStart;
     }
@@ -609,88 +638,64 @@
   if (bRelative)
     iIndex += iCurIndex;
 
-  if (iFoundCount <= iIndex || iIndex < 0) {
-    if (rnd.m_dwStyles & XFA_RESOLVENODE_CreateNode) {
-      m_pNodeHelper->m_pCreateParent = ToNode(rnd.m_CurObject.Get());
+  if (iIndex < 0 || static_cast<size_t>(iIndex) >= iFoundCount) {
+    if (pRnd->m_dwStyles & XFA_RESOLVENODE_CreateNode) {
+      m_pNodeHelper->m_pCreateParent = ToNode(pRnd->m_CurObject.Get());
       m_pNodeHelper->m_iCreateCount = iIndex - iFoundCount + 1;
     }
-    rnd.m_Objects.clear();
+    pRnd->m_Objects.clear();
   } else {
-    rnd.m_Objects =
-        std::vector<UnownedPtr<CXFA_Object>>(1, rnd.m_Objects[iIndex]);
+    pRnd->m_Objects =
+        std::vector<UnownedPtr<CXFA_Object>>(1, pRnd->m_Objects[iIndex]);
   }
 }
 
-void CFXJSE_ResolveProcessor::DoPredicateFilter(int32_t iCurIndex,
-                                                WideString wsCondition,
-                                                int32_t iFoundCount,
-                                                CFXJSE_ResolveNodeData& rnd) {
-  ASSERT(iFoundCount == pdfium::CollectionSize<int32_t>(rnd.m_Objects));
-  WideString wsExpression;
-  CXFA_Script::Type eLangType = CXFA_Script::Type::Unknown;
-  if (wsCondition.Left(2).EqualsASCII(".[") && wsCondition.Last() == L']')
-    eLangType = CXFA_Script::Type::Formcalc;
-  else if (wsCondition.Left(2).EqualsASCII(".(") && wsCondition.Last() == L')')
-    eLangType = CXFA_Script::Type::Javascript;
-  else
-    return;
-
-  wsExpression = wsCondition.Mid(2, wsCondition.GetLength() - 3);
-  for (int32_t i = iFoundCount - 1; i >= 0; i--) {
-    auto pRetValue = pdfium::MakeUnique<CFXJSE_Value>(rnd.m_pSC->GetIsolate());
-    bool bRet = rnd.m_pSC->RunScript(eLangType, wsExpression.AsStringView(),
-                                     pRetValue.get(), rnd.m_Objects[i].Get());
-    if (!bRet || !pRetValue->ToBoolean())
-      rnd.m_Objects.erase(rnd.m_Objects.begin() + i);
-  }
-}
-
-void CFXJSE_ResolveProcessor::FilterCondition(CFXJSE_ResolveNodeData& rnd,
-                                              WideString wsCondition) {
-  int32_t iCurrIndex = 0;
-  const std::vector<CXFA_Node*>* pArray = rnd.m_pSC->GetUpObjectArray();
+void CFXJSE_ResolveProcessor::FilterCondition(WideString wsCondition,
+                                              CFXJSE_ResolveNodeData* pRnd) {
+  size_t iCurIndex = 0;
+  const std::vector<CXFA_Node*>* pArray = pRnd->m_pSC->GetUpObjectArray();
   if (!pArray->empty()) {
     CXFA_Node* curNode = pArray->back();
     bool bIsProperty = CXFA_NodeHelper::NodeIsProperty(curNode);
     bool bIsClassIndex =
         curNode->IsUnnamed() ||
         (bIsProperty && curNode->GetElementType() != XFA_Element::PageSet);
-    iCurrIndex = CXFA_NodeHelper::GetIndex(curNode, XFA_LOGIC_Transparent,
-                                           bIsProperty, bIsClassIndex);
+    iCurIndex = CXFA_NodeHelper::GetIndex(curNode, XFA_LOGIC_Transparent,
+                                          bIsProperty, bIsClassIndex);
   }
 
-  int32_t iFoundCount = pdfium::CollectionSize<int32_t>(rnd.m_Objects);
+  size_t iFoundCount = pRnd->m_Objects.size();
   wsCondition.Trim();
 
   int32_t iLen = wsCondition.GetLength();
   if (!iLen) {
-    if (rnd.m_dwStyles & XFA_RESOLVENODE_ALL)
+    if (pRnd->m_dwStyles & XFA_RESOLVENODE_ALL)
       return;
     if (iFoundCount == 1)
       return;
 
-    if (iFoundCount <= iCurrIndex) {
-      if (rnd.m_dwStyles & XFA_RESOLVENODE_CreateNode) {
-        m_pNodeHelper->m_pCreateParent = ToNode(rnd.m_CurObject.Get());
-        m_pNodeHelper->m_iCreateCount = iCurrIndex - iFoundCount + 1;
+    if (iFoundCount <= iCurIndex) {
+      if (pRnd->m_dwStyles & XFA_RESOLVENODE_CreateNode) {
+        m_pNodeHelper->m_pCreateParent = ToNode(pRnd->m_CurObject.Get());
+        m_pNodeHelper->m_iCreateCount = iCurIndex - iFoundCount + 1;
       }
-      rnd.m_Objects.clear();
+      pRnd->m_Objects.clear();
       return;
     }
 
-    rnd.m_Objects =
-        std::vector<UnownedPtr<CXFA_Object>>(1, rnd.m_Objects[iCurrIndex]);
+    pRnd->m_Objects =
+        std::vector<UnownedPtr<CXFA_Object>>(1, pRnd->m_Objects[iCurIndex]);
     return;
   }
 
   wchar_t wTypeChar = wsCondition[0];
   switch (wTypeChar) {
     case '[':
-      ConditionArray(iCurrIndex, wsCondition, iFoundCount, rnd);
+      ConditionArray(iCurIndex, wsCondition, iFoundCount, pRnd);
       return;
     case '.':
       if (iLen > 1 && (wsCondition[1] == '[' || wsCondition[1] == '('))
-        DoPredicateFilter(iCurrIndex, wsCondition, iFoundCount, rnd);
+        DoPredicateFilter(wsCondition, iFoundCount, pRnd);
       return;
     case '(':
     case '"':
@@ -698,6 +703,7 @@
       return;
   }
 }
+
 void CFXJSE_ResolveProcessor::SetStylesForChild(uint32_t dwParentStyles,
                                                 CFXJSE_ResolveNodeData& rnd) {
   uint32_t dwSubStyles = XFA_RESOLVENODE_Children;
diff --git a/fxjs/xfa/cfxjse_resolveprocessor.h b/fxjs/xfa/cfxjse_resolveprocessor.h
index 3f2a07c..6e4fcba 100644
--- a/fxjs/xfa/cfxjse_resolveprocessor.h
+++ b/fxjs/xfa/cfxjse_resolveprocessor.h
@@ -63,15 +63,11 @@
   bool ResolveNormal(CFXJSE_ResolveNodeData& rnd);
   void SetStylesForChild(uint32_t dwParentStyles, CFXJSE_ResolveNodeData& rnd);
 
-  void ConditionArray(int32_t iCurIndex,
+  void ConditionArray(size_t iCurIndex,
                       WideString wsCondition,
-                      int32_t iFoundCount,
-                      CFXJSE_ResolveNodeData& rnd);
-  void DoPredicateFilter(int32_t iCurIndex,
-                         WideString wsCondition,
-                         int32_t iFoundCount,
-                         CFXJSE_ResolveNodeData& rnd);
-  void FilterCondition(CFXJSE_ResolveNodeData& rnd, WideString wsCondition);
+                      size_t iFoundCount,
+                      CFXJSE_ResolveNodeData* pRnd);
+  void FilterCondition(WideString wsCondition, CFXJSE_ResolveNodeData* pRnd);
 
   int32_t m_iCurStart = 0;
   std::unique_ptr<CXFA_NodeHelper> const m_pNodeHelper;
diff --git a/fxjs/xfa/cjx_tree.cpp b/fxjs/xfa/cjx_tree.cpp
index b01a147..62ce3f4 100644
--- a/fxjs/xfa/cjx_tree.cpp
+++ b/fxjs/xfa/cjx_tree.cpp
@@ -168,7 +168,8 @@
   }
 
   CFXJSE_Engine* pScriptContext = GetDocument()->GetScriptContext();
-  pValue->SetInteger(pScriptContext->GetIndexByName(ToNode(GetXFAObject())));
+  pValue->SetInteger(pdfium::base::checked_cast<int32_t>(
+      pScriptContext->GetIndexByName(ToNode(GetXFAObject()))));
 }
 
 void CJX_Tree::classIndex(CFXJSE_Value* pValue,
@@ -180,8 +181,8 @@
   }
 
   CFXJSE_Engine* pScriptContext = GetDocument()->GetScriptContext();
-  pValue->SetInteger(
-      pScriptContext->GetIndexByClassName(ToNode(GetXFAObject())));
+  pValue->SetInteger(pdfium::base::checked_cast<int32_t>(
+      pScriptContext->GetIndexByClassName(ToNode(GetXFAObject()))));
 }
 
 void CJX_Tree::somExpression(CFXJSE_Value* pValue,
diff --git a/xfa/fxfa/parser/cxfa_nodehelper.cpp b/xfa/fxfa/parser/cxfa_nodehelper.cpp
index cab5625..167aa02 100644
--- a/xfa/fxfa/parser/cxfa_nodehelper.cpp
+++ b/xfa/fxfa/parser/cxfa_nodehelper.cpp
@@ -21,30 +21,30 @@
 namespace {
 
 CXFA_Node* FindFirstSiblingNamedInList(CXFA_Node* parent,
-                                       uint32_t dNameHash,
+                                       uint32_t dwNameHash,
                                        uint32_t dwFilter);
 CXFA_Node* FindFirstSiblingOfClassInList(CXFA_Node* parent,
                                          XFA_Element element,
                                          uint32_t dwFilter);
 
-CXFA_Node* FindFirstSiblingNamed(CXFA_Node* parent, uint32_t dNameHash) {
-  CXFA_Node* result =
-      FindFirstSiblingNamedInList(parent, dNameHash, XFA_NODEFILTER_Properties);
+CXFA_Node* FindFirstSiblingNamed(CXFA_Node* parent, uint32_t dwNameHash) {
+  CXFA_Node* result = FindFirstSiblingNamedInList(parent, dwNameHash,
+                                                  XFA_NODEFILTER_Properties);
   if (result)
     return result;
 
-  return FindFirstSiblingNamedInList(parent, dNameHash,
+  return FindFirstSiblingNamedInList(parent, dwNameHash,
                                      XFA_NODEFILTER_Children);
 }
 
 CXFA_Node* FindFirstSiblingNamedInList(CXFA_Node* parent,
-                                       uint32_t dNameHash,
+                                       uint32_t dwNameHash,
                                        uint32_t dwFilter) {
   for (CXFA_Node* child : parent->GetNodeList(dwFilter, XFA_Element::Unknown)) {
-    if (child->GetNameHash() == dNameHash)
+    if (child->GetNameHash() == dwNameHash)
       return child;
 
-    CXFA_Node* result = FindFirstSiblingNamed(child, dNameHash);
+    CXFA_Node* result = FindFirstSiblingNamed(child, dwNameHash);
     if (result)
       return result;
   }
@@ -75,42 +75,38 @@
   return nullptr;
 }
 
-int32_t TraverseSiblings(CXFA_Node* parent,
-                         uint32_t dNameHash,
-                         std::vector<CXFA_Node*>* pSiblings,
-                         XFA_LOGIC_TYPE eLogicType,
-                         bool bIsClassName,
-                         bool bIsFindProperty) {
-  if (!parent || !pSiblings)
-    return 0;
+void TraverseSiblings(CXFA_Node* parent,
+                      uint32_t dwNameHash,
+                      std::vector<CXFA_Node*>* pSiblings,
+                      XFA_LOGIC_TYPE eLogicType,
+                      bool bIsClassName,
+                      bool bIsFindProperty) {
+  ASSERT(parent);
+  ASSERT(pSiblings);
 
-  int32_t nCount = 0;
   if (bIsFindProperty) {
     for (CXFA_Node* child :
          parent->GetNodeList(XFA_NODEFILTER_Properties, XFA_Element::Unknown)) {
       if (bIsClassName) {
-        if (child->GetClassHashCode() == dNameHash) {
+        if (child->GetClassHashCode() == dwNameHash)
           pSiblings->push_back(child);
-          nCount++;
-        }
       } else {
-        if (child->GetNameHash() == dNameHash) {
+        if (child->GetNameHash() == dwNameHash) {
           if (child->GetElementType() != XFA_Element::PageSet &&
               child->GetElementType() != XFA_Element::Extras &&
               child->GetElementType() != XFA_Element::Items) {
             pSiblings->push_back(child);
-            nCount++;
           }
         }
       }
       if (child->IsUnnamed() &&
           child->GetElementType() == XFA_Element::PageSet) {
-        nCount += TraverseSiblings(child, dNameHash, pSiblings, eLogicType,
-                                   bIsClassName, false);
+        TraverseSiblings(child, dwNameHash, pSiblings, eLogicType, bIsClassName,
+                         false);
       }
     }
-    if (nCount > 0)
-      return nCount;
+    if (!pSiblings->empty())
+      return;
   }
   for (CXFA_Node* child :
        parent->GetNodeList(XFA_NODEFILTER_Children, XFA_Element::Unknown)) {
@@ -118,26 +114,22 @@
       continue;
 
     if (bIsClassName) {
-      if (child->GetClassHashCode() == dNameHash) {
+      if (child->GetClassHashCode() == dwNameHash)
         pSiblings->push_back(child);
-        nCount++;
-      }
     } else {
-      if (child->GetNameHash() == dNameHash) {
+      if (child->GetNameHash() == dwNameHash)
         pSiblings->push_back(child);
-        nCount++;
-      }
     }
     if (eLogicType == XFA_LOGIC_NoTransparent)
       continue;
 
     if (CXFA_NodeHelper::NodeIsTransparent(child) &&
         child->GetElementType() != XFA_Element::PageSet) {
-      nCount += TraverseSiblings(child, dNameHash, pSiblings, eLogicType,
-                                 bIsClassName, false);
+      TraverseSiblings(child, dwNameHash, pSiblings, eLogicType, bIsClassName,
+                       false);
     }
   }
-  return nCount;
+  return;
 }
 
 WideString GetNameExpressionSinglePath(CXFA_Node* refNode) {
@@ -147,14 +139,14 @@
       (bIsProperty && refNode->GetElementType() != XFA_Element::PageSet)) {
     ws = WideString::FromASCII(refNode->GetClassName());
     return WideString::Format(
-        L"#%ls[%d]", ws.c_str(),
+        L"#%ls[%zu]", ws.c_str(),
         CXFA_NodeHelper::GetIndex(refNode, XFA_LOGIC_Transparent, bIsProperty,
                                   true));
   }
   ws = refNode->JSObject()->GetCData(XFA_Attribute::Name);
   ws.Replace(L".", L"\\.");
   return WideString::Format(
-      L"%ls[%d]", ws.c_str(),
+      L"%ls[%zu]", ws.c_str(),
       CXFA_NodeHelper::GetIndex(refNode, XFA_LOGIC_Transparent, bIsProperty,
                                 false));
 }
@@ -187,27 +179,27 @@
 }
 
 // static
-int32_t CXFA_NodeHelper::CountSiblings(CXFA_Node* pNode,
-                                       XFA_LOGIC_TYPE eLogicType,
-                                       std::vector<CXFA_Node*>* pSiblings,
-                                       bool bIsClassName) {
+std::vector<CXFA_Node*> CXFA_NodeHelper::GetSiblings(CXFA_Node* pNode,
+                                                     XFA_LOGIC_TYPE eLogicType,
+                                                     bool bIsClassName) {
+  std::vector<CXFA_Node*> siblings;
   if (!pNode)
-    return 0;
+    return siblings;
   CXFA_Node* parent = GetParent(pNode, XFA_LOGIC_NoTransparent);
   if (!parent)
-    return 0;
+    return siblings;
   if (!parent->HasProperty(pNode->GetElementType()) &&
       eLogicType == XFA_LOGIC_Transparent) {
     parent = GetParent(pNode, XFA_LOGIC_Transparent);
     if (!parent)
-      return 0;
+      return siblings;
   }
-  if (bIsClassName) {
-    return TraverseSiblings(parent, pNode->GetClassHashCode(), pSiblings,
-                            eLogicType, bIsClassName, true);
-  }
-  return TraverseSiblings(parent, pNode->GetNameHash(), pSiblings, eLogicType,
-                          bIsClassName, true);
+
+  uint32_t dwNameHash =
+      bIsClassName ? pNode->GetClassHashCode() : pNode->GetNameHash();
+  TraverseSiblings(parent, dwNameHash, &siblings, eLogicType, bIsClassName,
+                   true);
+  return siblings;
 }
 
 // static
@@ -237,10 +229,10 @@
 }
 
 // static
-int32_t CXFA_NodeHelper::GetIndex(CXFA_Node* pNode,
-                                  XFA_LOGIC_TYPE eLogicType,
-                                  bool bIsProperty,
-                                  bool bIsClassIndex) {
+size_t CXFA_NodeHelper::GetIndex(CXFA_Node* pNode,
+                                 XFA_LOGIC_TYPE eLogicType,
+                                 bool bIsProperty,
+                                 bool bIsClassIndex) {
   CXFA_Node* parent = GetParent(pNode, XFA_LOGIC_NoTransparent);
   if (!parent)
     return 0;
@@ -250,18 +242,14 @@
     if (!parent)
       return 0;
   }
-  uint32_t dwHashName = pNode->GetNameHash();
-  if (bIsClassIndex) {
-    dwHashName = pNode->GetClassHashCode();
-  }
+  uint32_t dwHashName =
+      bIsClassIndex ? pNode->GetClassHashCode() : pNode->GetNameHash();
   std::vector<CXFA_Node*> siblings;
-  int32_t iSize = TraverseSiblings(parent, dwHashName, &siblings, eLogicType,
-                                   bIsClassIndex, true);
-  for (int32_t i = 0; i < iSize; ++i) {
-    CXFA_Node* child = siblings[i];
-    if (child == pNode) {
+  TraverseSiblings(parent, dwHashName, &siblings, eLogicType, bIsClassIndex,
+                   true);
+  for (size_t i = 0; i < siblings.size(); ++i) {
+    if (siblings[i] == pNode)
       return i;
-    }
   }
   return 0;
 }
@@ -319,7 +307,11 @@
     m_iCreateFlag = XFA_ResolveNode_RSType_CreateNodeOne;
     wsIndex = wsCondition.Mid(i, szLen - 1 - i);
   }
-  m_iCreateCount = wsIndex.GetInteger();
+  int32_t iCount = wsIndex.GetInteger();
+  if (iCount < 0)
+    return false;
+
+  m_iCreateCount = iCount;
   return true;
 }
 
@@ -353,7 +345,7 @@
     if (eType == XFA_Element::Unknown)
       return false;
 
-    for (int32_t i = 0; i < m_iCreateCount; ++i) {
+    for (size_t i = 0; i < m_iCreateCount; ++i) {
       CXFA_Node* pNewNode = m_pCreateParent->CreateSamePacketNode(eType);
       if (pNewNode) {
         m_pCreateParent->InsertChild(pNewNode, nullptr);
@@ -368,7 +360,7 @@
     if (bLastNode) {
       eClassType = m_eLastCreateType;
     }
-    for (int32_t i = 0; i < m_iCreateCount; ++i) {
+    for (size_t i = 0; i < m_iCreateCount; ++i) {
       CXFA_Node* pNewNode = m_pCreateParent->CreateSamePacketNode(eClassType);
       if (pNewNode) {
         pNewNode->JSObject()->SetAttribute(XFA_Attribute::Name, wsNameView,
diff --git a/xfa/fxfa/parser/cxfa_nodehelper.h b/xfa/fxfa/parser/cxfa_nodehelper.h
index 0deb0c0..c349d7e 100644
--- a/xfa/fxfa/parser/cxfa_nodehelper.h
+++ b/xfa/fxfa/parser/cxfa_nodehelper.h
@@ -32,14 +32,13 @@
 
   static CXFA_Node* GetParent(CXFA_Node* pNode, XFA_LOGIC_TYPE eLogicType);
 
-  static int32_t CountSiblings(CXFA_Node* pNode,
-                               XFA_LOGIC_TYPE eLogicType,
-                               std::vector<CXFA_Node*>* pSiblings,
-                               bool bIsClassName);
-  static int32_t GetIndex(CXFA_Node* pNode,
-                          XFA_LOGIC_TYPE eLogicType,
-                          bool bIsProperty,
-                          bool bIsClassIndex);
+  static std::vector<CXFA_Node*> GetSiblings(CXFA_Node* pNode,
+                                             XFA_LOGIC_TYPE eLogicType,
+                                             bool bIsClassName);
+  static size_t GetIndex(CXFA_Node* pNode,
+                         XFA_LOGIC_TYPE eLogicType,
+                         bool bIsProperty,
+                         bool bIsClassIndex);
   static WideString GetNameExpression(CXFA_Node* refNode);
   static bool NodeIsTransparent(CXFA_Node* refNode);
   static bool NodeIsProperty(CXFA_Node* refNode);
@@ -53,7 +52,7 @@
 
   XFA_Element m_eLastCreateType = XFA_Element::DataValue;
   XFA_ResolveNode_RSType m_iCreateFlag = XFA_ResolveNode_RSType_CreateNodeOne;
-  int32_t m_iCreateCount = 0;
+  size_t m_iCreateCount = 0;
   int32_t m_iCurAllStart = -1;
   UnownedPtr<CXFA_Node> m_pCreateParent;
   UnownedPtr<CXFA_Node> m_pAllStartParent;