Rule Definition
The narrow type in comparison may overflow and never reach the max limit and the loop may run infinitely
Remediation
Use types of same width for comparison
Violation Code Sample
short int bytesRec = 0;
char buf[SOMEBIGNUM];
while(bytesRec < MAXGET) {
bytesRec += getFromInput(buf+bytesRec);
}
Fixed Code Sample
//This example represents only one of the ways to remediate the violation
void testIntegerOverflow() {
int bytesRec = 0;
auto getFromInput = [](char*) {
return 10000;
};
#define MAXGET INT_MAX
char buf[100000];
while (bytesRec < MAXGET) {
int bytes = getFromInput(buf + bytesRec);
if (static_cast<int>(bytes + bytesRec) < bytesRec) {
break;
}
bytesRec += bytes;
}
}
Reference
https://cwe.mitre.org/data/definitions/190.html -Example 3
Related Technologies
Technical Criterion
CWE-190 - Integer Overflow or Wraparound
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.