Skip to content

Commit 0387d0d

Browse files
committed
Fix #13944: FN constParameterPointer in method in derived class
Add inconclusive and default=false (instead of true) in checkConstPointer.
1 parent b021c4f commit 0387d0d

4 files changed

Lines changed: 14 additions & 8 deletions

File tree

lib/checkother.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,8 +1996,9 @@ void CheckOther::checkConstPointer()
19961996
nonConstPointers.emplace(var);
19971997
}
19981998
for (const Variable *p: pointers) {
1999+
bool inconclusive = false;
19992000
if (p->isArgument()) {
2000-
if (!p->scope() || !p->scope()->function || p->scope()->function->isImplicitlyVirtual(true) || p->scope()->function->hasVirtualSpecifier())
2001+
if (!p->scope() || !p->scope()->function || p->scope()->function->isImplicitlyVirtual(false, (bool*)nullptr, &inconclusive) || p->scope()->function->hasVirtualSpecifier())
20012002
continue;
20022003
if (p->isMaybeUnused())
20032004
continue;
@@ -2014,12 +2015,12 @@ void CheckOther::checkConstPointer()
20142015
continue;
20152016
if (p->typeStartToken() && p->typeStartToken()->isSimplifiedTypedef() && !(Token::simpleMatch(p->typeEndToken(), "*") && !p->typeEndToken()->isSimplifiedTypedef()))
20162017
continue;
2017-
constVariableError(p, p->isArgument() ? p->scope()->function : nullptr);
2018+
constVariableError(p, p->isArgument() ? p->scope()->function : nullptr, &inconclusive);
20182019
}
20192020
}
20202021
}
20212022

2022-
void CheckOther::constVariableError(const Variable *var, const Function *function)
2023+
void CheckOther::constVariableError(const Variable *var, const Function *function, bool* inconclusive)
20232024
{
20242025
if (!var) {
20252026
reportError(nullptr, Severity::style, "constParameter", "Parameter 'x' can be declared with const");
@@ -2050,7 +2051,7 @@ void CheckOther::constVariableError(const Variable *var, const Function *functio
20502051
id += "Pointer";
20512052
}
20522053

2053-
reportError(std::move(errorPath), Severity::style, id.c_str(), message, CWE398, Certainty::normal);
2054+
reportError(std::move(errorPath), Severity::style, id.c_str(), message, CWE398, (inconclusive && *inconclusive) ? Certainty::inconclusive : Certainty::normal);
20542055
}
20552056

20562057
//---------------------------------------------------------------------------

lib/checkother.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class CPPCHECKLIB CheckOther : public Check {
215215
void suspiciousFloatingPointCastError(const Token *tok);
216216
void invalidPointerCastError(const Token* tok, const std::string& from, const std::string& to, bool inconclusive, bool toIsInt);
217217
void passedByValueError(const Variable* var, bool inconclusive, bool isRangeBasedFor = false);
218-
void constVariableError(const Variable *var, const Function *function);
218+
void constVariableError(const Variable *var, const Function *function, bool * inconclusive = nullptr);
219219
void constStatementError(const Token *tok, const std::string &type, bool inconclusive);
220220
void signedCharArrayIndexError(const Token *tok);
221221
void unknownSignCharArrayIndexError(const Token *tok);

lib/symboldatabase.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4810,8 +4810,10 @@ void Function::addArguments(const Scope *scope)
48104810
}
48114811
}
48124812

4813-
bool Function::isImplicitlyVirtual(bool defaultVal, bool* pFoundAllBaseClasses) const
4813+
bool Function::isImplicitlyVirtual(bool defaultVal, bool* pFoundAllBaseClasses, bool* inconclusive) const
48144814
{
4815+
if (inconclusive)
4816+
*inconclusive = false; // assume not inconclusive.
48154817
if (hasVirtualSpecifier() || hasOverrideSpecifier() || hasFinalSpecifier())
48164818
return true;
48174819
bool foundAllBaseClasses = true;
@@ -4821,7 +4823,10 @@ bool Function::isImplicitlyVirtual(bool defaultVal, bool* pFoundAllBaseClasses)
48214823
*pFoundAllBaseClasses = foundAllBaseClasses;
48224824
if (foundAllBaseClasses) //If we've seen all the base classes and none of the above were true then it must not be virtual
48234825
return false;
4824-
return defaultVal; //If we can't see all the bases classes then we can't say conclusively
4826+
//If we can't see all the bases classes then we can't say conclusively, set inconclusive (if possible) and return default value
4827+
if (inconclusive)
4828+
*inconclusive = true;
4829+
return defaultVal;
48254830
}
48264831

48274832
std::vector<const Function*> Function::getOverloadedFunctions() const

lib/symboldatabase.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ class CPPCHECKLIB Function {
783783
void addArguments(const Scope *scope);
784784

785785
/** @brief check if this function is virtual in the base classes */
786-
bool isImplicitlyVirtual(bool defaultVal = false, bool* pFoundAllBaseClasses = nullptr) const;
786+
bool isImplicitlyVirtual(bool defaultVal = false, bool* pFoundAllBaseClasses = nullptr, bool* inconclusive = nullptr) const;
787787

788788
std::vector<const Function*> getOverloadedFunctions() const;
789789

0 commit comments

Comments
 (0)