Skip to content

Commit ce003fc

Browse files
Merge branch 'main' into chr_13678
2 parents a8f7e66 + ed596da commit ce003fc

30 files changed

Lines changed: 201 additions & 53 deletions

.github/workflows/selfcheck.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ jobs:
121121
122122
- name: Self check (unusedFunction / no test / no gui)
123123
run: |
124-
supprs="--suppress=unusedFunction:lib/errorlogger.h:197 --suppress=unusedFunction:lib/importproject.cpp:1666 --suppress=unusedFunction:lib/importproject.cpp:1690"
124+
supprs="--suppress=unusedFunction:lib/errorlogger.h:197 --suppress=unusedFunction:lib/importproject.cpp:1671 --suppress=unusedFunction:lib/importproject.cpp:1695"
125125
./cppcheck -q --template=selfcheck --error-exitcode=1 --library=cppcheck-lib -D__CPPCHECK__ -D__GNUC__ --enable=unusedFunction,information --exception-handling -rp=. --project=cmake.output.notest_nogui/compile_commands.json --suppressions-list=.selfcheck_unused_suppressions --inline-suppr $supprs
126126
env:
127127
DISABLE_VALUEFLOW: 1

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ Jonathan Clohessy
206206
Jonathan Haehne
207207
Jonathan Neuschäfer
208208
Jonathan Thackray
209+
Jonny Paton
209210
José Martins
210211
Jose Roquette
211212
Joshua Beck

gui/checkstatistics.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ unsigned CheckStatistics::getCount(const QString &tool, ShowTypes::ShowType type
109109
QStringList CheckStatistics::getTools() const
110110
{
111111
QSet<QString> ret;
112-
for (const QString& tool: mStyle.keys()) ret.insert(tool);
113-
for (const QString& tool: mWarning.keys()) ret.insert(tool);
114-
for (const QString& tool: mPerformance.keys()) ret.insert(tool);
115-
for (const QString& tool: mPortability.keys()) ret.insert(tool);
116-
for (const QString& tool: mError.keys()) ret.insert(tool);
112+
std::copy(mStyle.keyBegin(), mStyle.keyEnd(), std::inserter(ret, ret.end()));
113+
std::copy(mWarning.keyBegin(), mWarning.keyEnd(), std::inserter(ret, ret.end()));
114+
std::copy(mPerformance.keyBegin(), mPerformance.keyEnd(), std::inserter(ret, ret.end()));
115+
std::copy(mPortability.keyBegin(), mPortability.keyEnd(), std::inserter(ret, ret.end()));
116+
std::copy(mError.keyBegin(), mError.keyEnd(), std::inserter(ret, ret.end()));
117117
return ret.values();
118118
}

lib/astutils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,6 +2592,8 @@ bool isVariableChangedByFunctionCall(const Token *tok, int indirect, const Setti
25922592
if (const Variable* var = tok->variable()) {
25932593
if (tok == var->nameToken() && (!var->isReference() || (var->isConst() && var->type() == tok1->type())) && (!var->isClass() || (var->valueType() && var->valueType()->container))) // const ref or passed to (copy) ctor
25942594
return false;
2595+
if (var->isArray() && var->valueType() && var->valueType()->pointer == 0 && var->valueType()->isPrimitive())
2596+
return false;
25952597
}
25962598

25972599
std::vector<const Variable*> args = getArgumentVars(tok, argnr);

lib/checkbufferoverrun.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,16 @@ ValueFlow::Value CheckBufferOverrunImpl::getBufferSize(const Token *bufTok) cons
556556
{
557557
if (!bufTok->valueType())
558558
return ValueFlow::Value(-1);
559-
if (bufTok->isUnaryOp("&"))
559+
560+
if (bufTok->isUnaryOp("&")) {
560561
bufTok = bufTok->astOperand1();
562+
if (Token::simpleMatch(bufTok, "[")) {
563+
const Token* index = bufTok->astOperand2();
564+
if (!(index && index->hasKnownIntValue() && index->getKnownIntValue() == 0))
565+
return ValueFlow::Value(-1);
566+
bufTok = bufTok->astOperand1();
567+
}
568+
}
561569
const Variable *var = bufTok->variable();
562570

563571
if (!var || var->dimensions().empty()) {

lib/checkother.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ void CheckOtherImpl::checkVariableScope()
12891289
tok = tok->link();
12901290

12911291
// parse else if blocks..
1292-
} else if (Token::simpleMatch(tok, "else { if (") && tok->next()->isSimplifiedScope() && Token::simpleMatch(tok->linkAt(3), ") {")) {
1292+
} else if (Token::simpleMatch(tok, "else { if (") && tok->next()->isInsertedBrace() && Token::simpleMatch(tok->linkAt(3), ") {")) {
12931293
tok = tok->next();
12941294
} else if (tok->varId() == var->declarationId() || tok->str() == "goto") {
12951295
reduce = false;
@@ -1415,7 +1415,7 @@ bool CheckOtherImpl::checkInnerScope(const Token *tok, const Variable* var, bool
14151415
if (scope->type == ScopeType::eSwitch)
14161416
return false; // Used in outer switch scope - unsafe or impossible to reduce scope
14171417

1418-
if (scope->bodyStart && scope->bodyStart->isSimplifiedScope())
1418+
if (scope->bodyStart && scope->bodyStart->isSimplifiedIfInitStmt())
14191419
return false; // simplified if/for/switch init statement
14201420
}
14211421
if (var->isArrayOrPointer()) {
@@ -4055,7 +4055,7 @@ void CheckOtherImpl::checkFuncArgNamesDifferent()
40554055
break;
40564056
}
40574057
// skip over templates and arrays
4058-
if (decl->link() && !Token::Match(decl, "[()]"))
4058+
if (decl->link() && precedes(decl, decl->link()) && !Token::Match(decl, "( [*&]"))
40594059
decl = decl->link();
40604060
else if (decl->varId())
40614061
declarations[j] = decl;
@@ -4589,6 +4589,7 @@ void CheckOtherImpl::checkUnionZeroInit()
45894589
std::unordered_map<const Scope *, Union> unionsByScopeId;
45904590
const std::vector<Union> unions = parseUnions(*symbolDatabase, mSettings);
45914591
for (const Union &u : unions) {
4592+
// cppcheck-suppress useStlAlgorithm - std::transform is cumbersome
45924593
unionsByScopeId.emplace(u.scope, u);
45934594
}
45944595

lib/checks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ namespace CheckInstances
2929
{
3030
/** List of registered check classes. This is used by Cppcheck to run checks and generate documentation */
3131
CPPCHECKLIB const std::list<Check *>& get();
32-
};
32+
}
3333

3434
#endif // checksH

lib/checkstl.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -637,12 +637,14 @@ void CheckStlImpl::iterators()
637637
void CheckStlImpl::mismatchingContainerIteratorError(const Token* containerTok, const Token* iterTok, const Token* containerTok2)
638638
{
639639
const std::string container(containerTok ? containerTok->expressionString() : std::string("v1"));
640+
const std::string containerTemp(isTemporary(containerTok, &mSettings.library) ? " temporary " : " ");
640641
const std::string container2(containerTok2 ? containerTok2->expressionString() : std::string("v2"));
642+
const std::string containerTemp2(isTemporary(containerTok2, &mSettings.library) ? " temporary " : " ");
641643
const std::string iter(iterTok ? iterTok->expressionString() : std::string("it"));
642644
reportError(containerTok,
643645
Severity::error,
644646
"mismatchingContainerIterator",
645-
"Iterator '" + iter + "' referring to container '" + container2 + "' is used with container '" + container + "'.",
647+
"Iterator '" + iter + "' referring to" + containerTemp2 + "container '" + container2 + "' is used with" + containerTemp + "container '" + container + "'.",
646648
CWE664,
647649
Certainty::normal);
648650
}
@@ -884,7 +886,7 @@ void CheckStlImpl::mismatchingContainerIterator()
884886
const std::vector<const Token *> args = getArguments(ftok);
885887

886888
const Library::Container * c = tok->valueType()->container;
887-
const Library::Container::Action action = c->getAction(tok->strAt(2));
889+
const Library::Container::Action action = c->getAction(ftok->str());
888890
const Token* iterTok = nullptr;
889891
if (action == Library::Container::Action::INSERT && args.size() == 2) {
890892
// Skip if iterator pair
@@ -3116,19 +3118,18 @@ void CheckStlImpl::useStlAlgorithm()
31163118
bool useLoopVarInMemCall;
31173119
const Token *memberAccessTok = singleMemberCallInScope(bodyTok, loopVar->varId(), useLoopVarInMemCall, mSettings);
31183120
if (memberAccessTok && loopType == LoopType::RANGE) {
3119-
const Token *memberCallTok = memberAccessTok->astOperand2();
31203121
const int contVarId = memberAccessTok->astOperand1()->varId();
31213122
if (contVarId == loopVar->varId())
31223123
continue;
3123-
if (memberCallTok->str() == "push_back" ||
3124-
memberCallTok->str() == "push_front" ||
3125-
memberCallTok->str() == "emplace_back") {
3124+
using Action = Library::Container::Action;
3125+
const auto action = astContainerAction(memberAccessTok->astOperand1(), mSettings.library);
3126+
if (contains({Action::PUSH, Action::INSERT}, action)) {
31263127
std::string algo;
31273128
if (useLoopVarInMemCall)
31283129
algo = "std::copy";
31293130
else
31303131
algo = "std::transform";
3131-
useStlAlgorithmError(memberCallTok, algo);
3132+
useStlAlgorithmError(memberAccessTok->astOperand2(), algo);
31323133
}
31333134
continue;
31343135
}

lib/cppcheck.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,7 @@ unsigned int CppCheck::check(const FileSettings &fs)
825825
else
826826
tempSettings.userDefines += fs.cppcheckDefines();
827827
tempSettings.includePaths = fs.includePaths;
828+
tempSettings.userIncludes.insert(tempSettings.userIncludes.end(), fs.forcedIncludes.cbegin(), fs.forcedIncludes.cend());
828829
tempSettings.userUndefs.insert(fs.undefs.cbegin(), fs.undefs.cend());
829830
if (fs.standard.find("++") != std::string::npos)
830831
tempSettings.standards.setCPP(fs.standard);

lib/filesettings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ struct CPPCHECKLIB FileSettings {
148148
}
149149
std::set<std::string> undefs;
150150
std::list<std::string> includePaths;
151+
std::list<std::string> forcedIncludes;
151152
// only used by clang mode
152153
std::list<std::string> systemIncludePaths;
153154
std::string standard;

0 commit comments

Comments
 (0)