[GitHub] [xerces-c] rleigh-codelibre commented on a diff i n pull request #51: [XERCESC-2241] Fix integer overflows in DFAContentModel class

GitBox <[email protected]>
Newsgroups gmane.text.xml.xerces-c.devel
Message-ID <PR_kwDODeOUTs5AAzEJ-4ef978b8-ded0-46f1-8ef6-c87a77da7713@gitbox.apache.org>
rleigh-codelibre commented on code in PR #51:
URL: https://github.com/apache/xerces-c/pull/51#discussion_r986030234


##########
src/xercesc/validators/common/DFAContentModel.cpp:
##########
@@ -661,8 +662,15 @@ void DFAContentModel::buildDFA(ContentSpecNode* const curNode)
     //  in the fLeafCount member.
     //
     fLeafCount=countLeafNodes(curNode);
+    // Avoid integer overflow in below fLeafCount++ increment
+    if (fLeafCount > std::numeric_limits<unsigned int>::max() - 1)

Review Comment:
   Should this be `XMLSize_t` (a.k.a. `size_t`) rather than `unsigned int` to match the type of `fLeafCount`?



##########
src/xercesc/validators/common/DFAContentModel.cpp:
##########
@@ -661,8 +662,15 @@ void DFAContentModel::buildDFA(ContentSpecNode* const curNode)
     //  in the fLeafCount member.
     //
     fLeafCount=countLeafNodes(curNode);
+    // Avoid integer overflow in below fLeafCount++ increment
+    if (fLeafCount > std::numeric_limits<unsigned int>::max() - 1)
+        throw OutOfMemoryException();
     fEOCPos = fLeafCount++;
 
+    // Avoid integer overflow in below memory allocatoin

Review Comment:
   typo: allocation
   
   



##########
src/xercesc/validators/common/DFAContentModel.cpp:
##########
@@ -1364,14 +1372,27 @@ unsigned int DFAContentModel::countLeafNodes(ContentSpecNode* const curNode)
         if(nLoopCount!=0)
         {
             count += countLeafNodes(cursor);
-            for(unsigned int i=0;i<nLoopCount;i++)
-                count += countLeafNodes(rightNode);
+            const unsigned int countRight = countLeafNodes(rightNode);
+            // Avoid integer overflow in below multiplication
+            if (countRight > std::numeric_limits<unsigned int>::max() / nLoopCount)

Review Comment:
   In all of the following changes, just want to check as above that the limit check should use `XMLSize_t` rather than `unsigned int`?
   
   And would it also be possible to put brackets around all expressions being compared for clarity?



##########
src/xercesc/validators/common/DFAContentModel.cpp:
##########
@@ -661,8 +662,15 @@ void DFAContentModel::buildDFA(ContentSpecNode* const curNode)
     //  in the fLeafCount member.
     //
     fLeafCount=countLeafNodes(curNode);
+    // Avoid integer overflow in below fLeafCount++ increment
+    if (fLeafCount > std::numeric_limits<unsigned int>::max() - 1)
+        throw OutOfMemoryException();
     fEOCPos = fLeafCount++;
 
+    // Avoid integer overflow in below memory allocatoin
+    if (fLeafCount > std::numeric_limits<size_t>::max() / sizeof(CMLeaf*))

Review Comment:
   Should this also be `XMLSize_t`?
   
   Would it be possible to add brackets around the division for clarity?



##########
src/xercesc/validators/common/DFAContentModel.cpp:
##########
@@ -661,8 +662,15 @@ void DFAContentModel::buildDFA(ContentSpecNode* const curNode)
     //  in the fLeafCount member.
     //
     fLeafCount=countLeafNodes(curNode);
+    // Avoid integer overflow in below fLeafCount++ increment
+    if (fLeafCount > std::numeric_limits<unsigned int>::max() - 1)

Review Comment:
   Oops, I was looking at the wrong definition in src/xercesc/validators/common/ContentLeafNameTypeVector.hpp when I was scanning over the `git grep` output.  In that case, that all looks fine!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.