Skip to content

Commit d6db810

Browse files
scantorGuilhem Moulin
authored andcommitted
XERCESC-2241 - Integer overflows in DFAContentModel class
Origin: apache/xerces-c@1296a40 Bug: apache/xerces-c#51 Bug: https://issues.apache.org/jira/browse/XERCESC-2241 Bug-Debian: https://security-tracker.debian.org/tracker/CVE-2023-37536 Gbp-Pq: Name CVE-2023-37536.patch
1 parent 8c0657d commit d6db810

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/xercesc/validators/common/DFAContentModel.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
// Includes
2525
// ---------------------------------------------------------------------------
2626
#include <xercesc/util/RuntimeException.hpp>
27+
#include <xercesc/util/OutOfMemoryException.hpp>
2728
#include <xercesc/framework/XMLBuffer.hpp>
2829
#include <xercesc/framework/XMLElementDecl.hpp>
2930
#include <xercesc/framework/XMLValidator.hpp>
@@ -41,6 +42,7 @@
4142
#include <xercesc/util/RefHashTableOf.hpp>
4243
#include <xercesc/util/XMLInteger.hpp>
4344
#include <math.h>
45+
#include <limits>
4446

4547
XERCES_CPP_NAMESPACE_BEGIN
4648

@@ -606,8 +608,15 @@ void DFAContentModel::buildDFA(ContentSpecNode* const curNode)
606608
// in the fLeafCount member.
607609
//
608610
fLeafCount=countLeafNodes(curNode);
611+
// Avoid integer overflow in below fLeafCount++ increment
612+
if (fLeafCount > (std::numeric_limits<unsigned int>::max() - 1))
613+
throw OutOfMemoryException();
609614
fEOCPos = fLeafCount++;
610615

616+
// Avoid integer overflow in below memory allocation
617+
if (fLeafCount > (std::numeric_limits<size_t>::max() / sizeof(CMLeaf*)))
618+
throw OutOfMemoryException();
619+
611620
// We need to build an array of references to the non-epsilon
612621
// leaf nodes. We will put them in the array according to their position values
613622
//
@@ -1304,14 +1313,27 @@ unsigned int DFAContentModel::countLeafNodes(ContentSpecNode* const curNode)
13041313
if(nLoopCount!=0)
13051314
{
13061315
count += countLeafNodes(cursor);
1307-
for(unsigned int i=0;i<nLoopCount;i++)
1308-
count += countLeafNodes(rightNode);
1316+
const unsigned int countRight = countLeafNodes(rightNode);
1317+
// Avoid integer overflow in below multiplication
1318+
if (countRight > (std::numeric_limits<unsigned int>::max() / nLoopCount))
1319+
throw OutOfMemoryException();
1320+
const unsigned int countRightMulLoopCount = nLoopCount * countRight;
1321+
// Avoid integer overflow in below addition
1322+
if (count > (std::numeric_limits<unsigned int>::max() - countRightMulLoopCount))
1323+
throw OutOfMemoryException();
1324+
count += countRightMulLoopCount;
13091325
return count;
13101326
}
13111327
if(leftNode)
13121328
count+=countLeafNodes(leftNode);
13131329
if(rightNode)
1314-
count+=countLeafNodes(rightNode);
1330+
{
1331+
const unsigned int countRight = countLeafNodes(rightNode);
1332+
// Avoid integer overflow in below addition
1333+
if (count > (std::numeric_limits<unsigned int>::max() - countRight))
1334+
throw OutOfMemoryException();
1335+
count+=countRight;
1336+
}
13151337
}
13161338
return count;
13171339
}

0 commit comments

Comments
 (0)