Skip to content

Commit 46185d6

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

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
@@ -2008,8 +2008,9 @@ void CheckOther::checkConstPointer()
20082008
nonConstPointers.emplace(var);
20092009
}
20102010
for (const Variable *p: pointers) {
2011+
bool inconclusive = false;
20112012
if (p->isArgument()) {
2012-
if (!p->scope() || !p->scope()->function || p->scope()->function->isImplicitlyVirtual(true) || p->scope()->function->hasVirtualSpecifier())
2013+
if (!p->scope() || !p->scope()->function || p->scope()->function->isImplicitlyVirtual(false, (bool*)nullptr, &inconclusive) || p->scope()->function->hasVirtualSpecifier())
20132014
continue;
20142015
if (p->isMaybeUnused())
20152016
continue;
@@ -2026,12 +2027,12 @@ void CheckOther::checkConstPointer()
20262027
continue;
20272028
if (p->typeStartToken() && p->typeStartToken()->isSimplifiedTypedef() && !(Token::simpleMatch(p->typeEndToken(), "*") && !p->typeEndToken()->isSimplifiedTypedef()))
20282029
continue;
2029-
constVariableError(p, p->isArgument() ? p->scope()->function : nullptr);
2030+
constVariableError(p, p->isArgument() ? p->scope()->function : nullptr, &inconclusive);
20302031
}
20312032
}
20322033
}
20332034

2034-
void CheckOther::constVariableError(const Variable *var, const Function *function)
2035+
void CheckOther::constVariableError(const Variable *var, const Function *function, bool* inconclusive)
20352036
{
20362037
if (!var) {
20372038
reportError(nullptr, Severity::style, "constParameter", "Parameter 'x' can be declared with const");
@@ -2062,7 +2063,7 @@ void CheckOther::constVariableError(const Variable *var, const Function *functio
20622063
id += "Pointer";
20632064
}
20642065

2065-
reportError(std::move(errorPath), Severity::style, id.c_str(), message, CWE398, Certainty::normal);
2066+
reportError(std::move(errorPath), Severity::style, id.c_str(), message, CWE398, (inconclusive && *inconclusive) ? Certainty::inconclusive : Certainty::normal);
20662067
}
20672068

20682069
//---------------------------------------------------------------------------

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
@@ -4813,8 +4813,10 @@ void Function::addArguments(const Scope *scope)
48134813
}
48144814
}
48154815

4816-
bool Function::isImplicitlyVirtual(bool defaultVal, bool* pFoundAllBaseClasses) const
4816+
bool Function::isImplicitlyVirtual(bool defaultVal, bool* pFoundAllBaseClasses, bool* inconclusive) const
48174817
{
4818+
if (inconclusive)
4819+
*inconclusive = false; // assume not inconclusive.
48184820
if (hasVirtualSpecifier() || hasOverrideSpecifier() || hasFinalSpecifier())
48194821
return true;
48204822
bool foundAllBaseClasses = true;
@@ -4824,7 +4826,10 @@ bool Function::isImplicitlyVirtual(bool defaultVal, bool* pFoundAllBaseClasses)
48244826
*pFoundAllBaseClasses = foundAllBaseClasses;
48254827
if (foundAllBaseClasses) //If we've seen all the base classes and none of the above were true then it must not be virtual
48264828
return false;
4827-
return defaultVal; //If we can't see all the bases classes then we can't say conclusively
4829+
//If we can't see all the bases classes then we can't say conclusively, set inconclusive (if possible) and return default value
4830+
if (inconclusive)
4831+
*inconclusive = true;
4832+
return defaultVal;
48284833
}
48294834

48304835
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)