Skip to content

Commit 4d81945

Browse files
committed
Fixed a couple of cppcheck-opensource#6276 integer over/underflow issues
1 parent 493ab54 commit 4d81945

6 files changed

Lines changed: 31 additions & 18 deletions

File tree

cli/cmdlineparser.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,16 @@ bool CmdLineParser::ParseFromArgs(int argc, const char* const argv[])
288288
_settings->_relativePaths = true;
289289
if (argv[i][argv[i][3]=='='?4:17] != 0) {
290290
std::string paths = argv[i]+(argv[i][3]=='='?4:17);
291-
std::string::size_type pos;
292-
do {
293-
pos = paths.find(';');
294-
_settings->_basePaths.push_back(Path::fromNativeSeparators(paths.substr(0, pos)));
295-
paths.erase(0, pos+1);
296-
} while (pos != std::string::npos);
291+
for (;;) {
292+
std::string::size_type pos = paths.find(';');
293+
if (pos == std::string::npos) {
294+
_settings->_basePaths.push_back(Path::fromNativeSeparators(paths));
295+
break;
296+
} else {
297+
_settings->_basePaths.push_back(Path::fromNativeSeparators(paths.substr(0, pos)));
298+
paths.erase(0, pos + 1);
299+
}
300+
}
297301
} else {
298302
PrintMessage("cppcheck: No paths specified for the '" + std::string(argv[i]) + "' option.");
299303
return false;

lib/checkstring.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,9 @@ void CheckString::checkIncorrectStringCompare()
217217
const Scope * scope = symbolDatabase->functionScopes[i];
218218
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
219219
// skip "assert(str && ..)" and "assert(.. && str)"
220-
if (Token::Match(tok, "%var% (") &&
221-
(Token::Match(tok->tokAt(2), "%str% &&") || Token::Match(tok->next()->link()->tokAt(-2), "&& %str% )")) &&
222-
(tok->str().find("assert") + 6U == tok->str().size() || tok->str().find("ASSERT") + 6U == tok->str().size()))
220+
if (tok->str().size() >= 6U && (tok->str().find("assert") == tok->str().size() - 6U || tok->str().find("ASSERT") == tok->str().size() - 6U) &&
221+
Token::Match(tok, "%var% (") &&
222+
(Token::Match(tok->tokAt(2), "%str% &&") || Token::Match(tok->next()->link()->tokAt(-2), "&& %str% )")))
223223
tok = tok->next()->link();
224224

225225
if (Token::simpleMatch(tok, ". substr (") && Token::Match(tok->tokAt(3)->nextArgument(), "%num% )")) {

lib/errorlogger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ void ErrorLogger::ErrorMessage::findAndReplace(std::string &source, const std::s
283283
std::string::size_type index = 0;
284284
while ((index = source.find(searchFor, index)) != std::string::npos) {
285285
source.replace(index, searchFor.length(), replaceWith);
286-
index += replaceWith.length() - searchFor.length() + 1;
286+
index = (std::string::difference_type)index + (std::string::difference_type)replaceWith.length() - (std::string::difference_type)searchFor.length() + 1;
287287
}
288288
}
289289

lib/preprocessor.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,7 +2051,10 @@ static bool openHeader(std::string &filename, const std::list<std::string> &incl
20512051

20522052
std::string Preprocessor::handleIncludes(const std::string &code, const std::string &filePath, const std::list<std::string> &includePaths, std::map<std::string,std::string> &defs, std::set<std::string> &pragmaOnce, std::list<std::string> includes)
20532053
{
2054-
const std::string path(filePath.substr(0, 1 + filePath.find_last_of("\\/")));
2054+
std::string path;
2055+
std::string::size_type sep_pos = filePath.find_last_of("\\/");
2056+
if (sep_pos != std::string::npos)
2057+
path = filePath.substr(0, 1 + sep_pos);
20552058

20562059
// current #if indent level.
20572060
std::stack<bool>::size_type indent = 0;
@@ -2264,7 +2267,9 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filePath
22642267
std::list<std::string> paths;
22652268
std::string path;
22662269
path = filePath;
2267-
path.erase(1 + path.find_last_of("\\/"));
2270+
std::string::size_type sep_pos = path.find_last_of("\\/");
2271+
if (sep_pos != std::string::npos)
2272+
path.erase(1 + sep_pos);
22682273
paths.push_back(path);
22692274
std::string::size_type pos = 0;
22702275
std::string::size_type endfilePos = 0;

lib/token.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,11 @@ std::string Token::strValue() const
153153

154154
void Token::deleteNext(unsigned long index)
155155
{
156-
while (_next && index--) {
156+
while (_next && index) {
157157
Token *n = _next;
158158
_next = n->next();
159159
delete n;
160+
--index;
160161
}
161162

162163
if (_next)

lib/tokenize.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,8 +1097,10 @@ void Tokenizer::simplifyTypedef()
10971097

10981098
if (good) {
10991099
// remove any extra qualification if present
1100-
while (count--)
1100+
while (count) {
11011101
tok2->tokAt(-3)->deleteNext(2);
1102+
--count;
1103+
}
11021104

11031105
// remove global namespace if present
11041106
if (tok2->strAt(-1) == "::") {
@@ -1589,10 +1591,11 @@ void Tokenizer::simplifyMulAndParens()
15891591
Token::Match(tokbegin->tokAt(-2), "[;{}&(] * &") ||
15901592
Token::Match(tokbegin->tokAt(-3), "[;{}&(] * ( &")) {
15911593
//remove the excessive parentheses around the variable
1592-
while (openpars--) {
1594+
while (openpars) {
15931595
tok->deleteNext();
15941596
tokbegin->deleteNext();
15951597
--closedpars;
1598+
--openpars;
15961599
}
15971600
} else
15981601
break;
@@ -4998,7 +5001,7 @@ void Tokenizer::simplifyCasts()
49985001
if (Token::Match(tok->next(), "( %type% ) %num%") && tok->next()->link()->previous()->isStandardType()) {
49995002
const MathLib::bigint value = MathLib::toLongNumber(tok->next()->link()->next()->str());
50005003
unsigned int bits = 8 * _typeSize[tok->next()->link()->previous()->str()];
5001-
if (!tok->tokAt(2)->isUnsigned())
5004+
if (!tok->tokAt(2)->isUnsigned() && bits > 0)
50025005
bits--;
50035006
if (bits < 31 && value >= 0 && value < (1LL << bits)) {
50045007
Token::eraseTokens(tok, tok->next()->link()->next());
@@ -6525,8 +6528,8 @@ bool Tokenizer::simplifyKnownVariables()
65256528
const Token * const valueToken = tok2->tokAt(4);
65266529
std::string value(valueToken->str());
65276530
if (tok2->str() == "sprintf") {
6528-
std::string::size_type n = std::string::npos;
6529-
while ((n = value.find("%%",n+1)) != std::string::npos) {
6531+
std::string::difference_type n = -1;
6532+
while (static_cast<std::string::size_type>(n = value.find("%%",n+1)) != std::string::npos) {
65306533
value.replace(n,2,"%");
65316534
}
65326535
}

0 commit comments

Comments
 (0)