2013-05-07 21:35:16 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2013-05-07 21:35:16 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// You should not write statements with side effects in assert()
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "checkassert.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "errortypes.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "settings.h"
|
2013-05-07 21:35:16 +02:00
|
|
|
#include "symboldatabase.h"
|
2017-05-27 04:33:47 +02:00
|
|
|
#include "token.h"
|
|
|
|
#include "tokenize.h"
|
|
|
|
#include "tokenlist.h"
|
2013-05-07 21:35:16 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
Mapped toomanyconfigs ,AssignmentAddressToInteger
,AssignmentIntegerToAddress ,CastIntegerToAddressAtReturn
,CastAddressToIntegerAtReturn ,assertWithSideEffect ,assignmentInAssert
,uselessAssignmentArg ,uselessAssignmentPtrArg
,comparisonOfFuncReturningBoolError
,comparisonOfTwoFuncsReturningBoolError ,comparisonOfBoolWithBoolError
,incrementboolean ,comparisonOfBoolWithInt ,compareBoolExpressionWithInt
,negativeIndex ,pointerOutOfBounds ,arrayIndexThenCheck
,possibleBufferAccessOutOfBounds ,argumentSize
,arrayIndexOutOfBoundsCond ,noConstructor ,copyCtorPointerCopying
,noCopyConstructor ,uninitMemberVar ,operatorEqVarError
,unusedPrivateFunction ,memsetClassFloat ,mallocOnClassWarning
,operatorEq ,thisSubtraction ,operatorEqRetRefThis ,operatorEqToSelf
,useInitializationList ,duplInheritedMember ,assignIfError
,comparisonError ,multiCondition ,mismatchingBitAnd
,oppositeInnerCondition ,incorrectLogicOperator ,redundantCondition
,moduloAlwaysTrueFalse to their CWEs ids.
2016-02-20 23:56:36 +01:00
|
|
|
// CWE ids used
|
|
|
|
static const struct CWE CWE398(398U); // Indicator of Poor Code Quality
|
2013-05-07 21:35:16 +02:00
|
|
|
|
|
|
|
// Register this check class (by creating a static instance of it)
|
|
|
|
namespace {
|
|
|
|
CheckAssert instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckAssert::assertWithSideEffects()
|
|
|
|
{
|
2021-02-24 22:00:06 +01:00
|
|
|
if (!mSettings->severity.isEnabled(Severity::warning))
|
2013-05-07 21:35:16 +02:00
|
|
|
return;
|
|
|
|
|
2023-08-29 12:00:52 +02:00
|
|
|
logChecker("CheckAssert::assertWithSideEffects"); // warning
|
|
|
|
|
2018-06-16 16:10:28 +02:00
|
|
|
for (const Token* tok = mTokenizer->list.front(); tok; tok = tok->next()) {
|
2014-08-06 10:15:48 +02:00
|
|
|
if (!Token::simpleMatch(tok, "assert ("))
|
|
|
|
continue;
|
2013-05-07 21:35:16 +02:00
|
|
|
|
2014-08-06 10:15:48 +02:00
|
|
|
const Token *endTok = tok->next()->link();
|
2014-07-14 09:44:58 +02:00
|
|
|
for (const Token* tmp = tok->next(); tmp != endTok; tmp = tmp->next()) {
|
2020-09-24 20:48:26 +02:00
|
|
|
if (Token::simpleMatch(tmp, "sizeof ("))
|
|
|
|
tmp = tmp->linkAt(1);
|
|
|
|
|
2016-05-26 18:29:29 +02:00
|
|
|
checkVariableAssignment(tmp, tok->scope());
|
2013-05-07 21:35:16 +02:00
|
|
|
|
2017-07-26 21:13:49 +02:00
|
|
|
if (tmp->tokType() != Token::eFunction)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const Function* f = tmp->function();
|
2022-10-12 07:51:50 +02:00
|
|
|
const Scope* scope = f->functionScope;
|
|
|
|
if (!scope) {
|
|
|
|
// guess that const method doesn't have side effects
|
|
|
|
if (f->nestedIn->isClassOrStruct() && !f->isConst() && !f->isStatic())
|
|
|
|
sideEffectInAssertError(tmp, f->name()); // Non-const member function called, assume it has side effects
|
2017-07-26 21:13:49 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-27 22:36:30 +02:00
|
|
|
for (const Token *tok2 = scope->bodyStart; tok2 != scope->bodyEnd; tok2 = tok2->next()) {
|
2018-11-09 06:30:41 +01:00
|
|
|
if (!tok2->isAssignmentOp() && tok2->tokType() != Token::eIncDecOp)
|
2017-07-26 21:13:49 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
const Variable* var = tok2->previous()->variable();
|
|
|
|
if (!var || var->isLocal() || (var->isArgument() && !var->isReference() && !var->isPointer()))
|
|
|
|
continue; // See ticket #4937. Assigning function arguments not passed by reference is ok.
|
|
|
|
if (var->isArgument() && var->isPointer() && tok2->strAt(-2) != "*")
|
|
|
|
continue; // Pointers need to be dereferenced, otherwise there is no error
|
|
|
|
|
|
|
|
bool noReturnInScope = true;
|
2018-04-27 22:36:30 +02:00
|
|
|
for (const Token *rt = scope->bodyStart; rt != scope->bodyEnd; rt = rt->next()) {
|
2017-07-26 21:13:49 +02:00
|
|
|
if (rt->str() != "return") continue; // find all return statements
|
|
|
|
if (inSameScope(rt, tok2)) {
|
|
|
|
noReturnInScope = false;
|
2014-08-06 10:15:48 +02:00
|
|
|
break;
|
|
|
|
}
|
2013-05-07 21:35:16 +02:00
|
|
|
}
|
2017-07-26 21:13:49 +02:00
|
|
|
if (noReturnInScope) continue;
|
|
|
|
|
|
|
|
sideEffectInAssertError(tmp, f->name());
|
|
|
|
break;
|
2013-05-07 21:35:16 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-06 10:15:48 +02:00
|
|
|
tok = endTok;
|
2013-05-07 21:35:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
void CheckAssert::sideEffectInAssertError(const Token *tok, const std::string& functionName)
|
|
|
|
{
|
|
|
|
reportError(tok, Severity::warning,
|
2018-04-09 06:43:48 +02:00
|
|
|
"assertWithSideEffect",
|
|
|
|
"$symbol:" + functionName + "\n"
|
|
|
|
"Assert statement calls a function which may have desired side effects: '$symbol'.\n"
|
|
|
|
"Non-pure function: '$symbol' is called inside assert statement. "
|
2013-05-07 21:35:16 +02:00
|
|
|
"Assert statements are removed from release builds so the code inside "
|
|
|
|
"assert statement is not executed. If the code is needed also in release "
|
2021-02-24 22:00:06 +01:00
|
|
|
"builds, this is a bug.", CWE398, Certainty::normal);
|
2013-05-07 21:35:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckAssert::assignmentInAssertError(const Token *tok, const std::string& varname)
|
|
|
|
{
|
|
|
|
reportError(tok, Severity::warning,
|
2018-04-09 06:43:48 +02:00
|
|
|
"assignmentInAssert",
|
|
|
|
"$symbol:" + varname + "\n"
|
|
|
|
"Assert statement modifies '$symbol'.\n"
|
2018-12-28 15:25:22 +01:00
|
|
|
"Variable '$symbol' is modified inside assert statement. "
|
2013-05-07 21:35:16 +02:00
|
|
|
"Assert statements are removed from release builds so the code inside "
|
|
|
|
"assert statement is not executed. If the code is needed also in release "
|
2021-02-24 22:00:06 +01:00
|
|
|
"builds, this is a bug.", CWE398, Certainty::normal);
|
2013-05-07 21:35:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// checks if side effects happen on the variable prior to tmp
|
2016-05-26 18:29:29 +02:00
|
|
|
void CheckAssert::checkVariableAssignment(const Token* assignTok, const Scope *assertionScope)
|
2013-05-07 21:35:16 +02:00
|
|
|
{
|
2020-04-18 09:26:24 +02:00
|
|
|
if (!assignTok->isAssignmentOp() && assignTok->tokType() != Token::eIncDecOp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const Variable* var = assignTok->astOperand1()->variable();
|
|
|
|
if (!var)
|
2015-04-01 17:23:51 +02:00
|
|
|
return;
|
2013-05-07 21:35:16 +02:00
|
|
|
|
2016-11-27 11:40:42 +01:00
|
|
|
// Variable declared in inner scope in assert => don't warn
|
2020-04-18 09:26:24 +02:00
|
|
|
if (assertionScope != var->scope()) {
|
|
|
|
const Scope *s = var->scope();
|
2016-05-26 18:29:29 +02:00
|
|
|
while (s && s != assertionScope)
|
|
|
|
s = s->nestedIn;
|
|
|
|
if (s == assertionScope)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-05-07 21:35:16 +02:00
|
|
|
// assignment
|
2015-08-14 20:46:13 +02:00
|
|
|
if (assignTok->isAssignmentOp() || assignTok->tokType() == Token::eIncDecOp) {
|
2020-04-18 09:26:24 +02:00
|
|
|
if (var->isConst()) {
|
2015-04-01 17:23:51 +02:00
|
|
|
return;
|
2020-04-18 09:26:24 +02:00
|
|
|
}
|
|
|
|
assignmentInAssertError(assignTok, var->name());
|
2013-05-07 21:35:16 +02:00
|
|
|
}
|
2020-04-18 09:26:24 +02:00
|
|
|
// TODO: function calls on var
|
2013-05-07 21:35:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CheckAssert::inSameScope(const Token* returnTok, const Token* assignTok)
|
|
|
|
{
|
|
|
|
// TODO: even if a return is in the same scope, the assignment might not affect it.
|
|
|
|
return returnTok->scope() == assignTok->scope();
|
|
|
|
}
|