|
24 | 24 | // Includes |
25 | 25 | // --------------------------------------------------------------------------- |
26 | 26 | #include <xercesc/util/RuntimeException.hpp> |
| 27 | +#include <xercesc/util/OutOfMemoryException.hpp> |
27 | 28 | #include <xercesc/framework/XMLBuffer.hpp> |
28 | 29 | #include <xercesc/framework/XMLElementDecl.hpp> |
29 | 30 | #include <xercesc/framework/XMLValidator.hpp> |
|
41 | 42 | #include <xercesc/util/RefHashTableOf.hpp> |
42 | 43 | #include <xercesc/util/XMLInteger.hpp> |
43 | 44 | #include <math.h> |
| 45 | +#include <limits> |
44 | 46 |
|
45 | 47 | XERCES_CPP_NAMESPACE_BEGIN |
46 | 48 |
|
@@ -606,8 +608,15 @@ void DFAContentModel::buildDFA(ContentSpecNode* const curNode) |
606 | 608 | // in the fLeafCount member. |
607 | 609 | // |
608 | 610 | fLeafCount=countLeafNodes(curNode); |
| 611 | + // Avoid integer overflow in below fLeafCount++ increment |
| 612 | + if (fLeafCount > (std::numeric_limits<unsigned int>::max() - 1)) |
| 613 | + throw OutOfMemoryException(); |
609 | 614 | fEOCPos = fLeafCount++; |
610 | 615 |
|
| 616 | + // Avoid integer overflow in below memory allocation |
| 617 | + if (fLeafCount > (std::numeric_limits<size_t>::max() / sizeof(CMLeaf*))) |
| 618 | + throw OutOfMemoryException(); |
| 619 | + |
611 | 620 | // We need to build an array of references to the non-epsilon |
612 | 621 | // leaf nodes. We will put them in the array according to their position values |
613 | 622 | // |
@@ -1304,14 +1313,27 @@ unsigned int DFAContentModel::countLeafNodes(ContentSpecNode* const curNode) |
1304 | 1313 | if(nLoopCount!=0) |
1305 | 1314 | { |
1306 | 1315 | 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; |
1309 | 1325 | return count; |
1310 | 1326 | } |
1311 | 1327 | if(leftNode) |
1312 | 1328 | count+=countLeafNodes(leftNode); |
1313 | 1329 | 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 | + } |
1315 | 1337 | } |
1316 | 1338 | return count; |
1317 | 1339 | } |
|
0 commit comments