Skip to content

Commit 7219b70

Browse files
committed
fix #5591
1 parent 5ac33c8 commit 7219b70

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/checkother.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -4367,6 +4367,34 @@ void CheckOther::overlappingWriteFunction(const Token *tok, const std::string& f
43674367
reportError(tok, Severity::error, "overlappingWriteFunction", "Overlapping read/write in " + funcname + "() is undefined behavior");
43684368
}
43694369

4370+
void CheckOther::checkSuspiciousComma()
4371+
{
4372+
if (!mSettings->severity.isEnabled(Severity::style)) {
4373+
return;
4374+
}
4375+
4376+
logChecker("CheckOpComma::warnSuspiciousComma");
4377+
4378+
for (const Token* tok = mTokenizer->list.front(); tok; tok = tok->next()) {
4379+
if (tok->str() == "," && tok->isBinaryOp()) {
4380+
const Token * parent = tok->astParent();
4381+
if (parent && (Token::simpleMatch(parent->previous(), "if (") ||
4382+
Token::simpleMatch(parent->previous(), "while ("))) {
4383+
if (tok->previous() && tok->previous()->str() == ")" && tok->previous()->link() && tok->previous()->link()->str() == "(") {
4384+
const Token * t = tok->previous()->link()->previous();
4385+
const Function * func = t->function();
4386+
if (func && func->initArgCount > 0) {
4387+
const Token * r_op = tok->astOperand2();
4388+
if (r_op && r_op->hasKnownValue()) {
4389+
reportError(tok, Severity::style, "warnSuspiciousComma", "There is an suspicious comma expression used as a condition.");
4390+
}
4391+
}
4392+
}
4393+
}
4394+
}
4395+
}
4396+
}
4397+
43704398
void CheckOther::runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger)
43714399
{
43724400
CheckOther checkOther(&tokenizer, &tokenizer.getSettings(), errorLogger);
@@ -4414,6 +4442,7 @@ void CheckOther::runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger)
44144442
checkOther.checkAccessOfMovedVariable();
44154443
checkOther.checkModuloOfOne();
44164444
checkOther.checkOverlappingWrite();
4445+
checkOther.checkSuspiciousComma();
44174446
}
44184447

44194448
void CheckOther::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const

lib/checkother.h

+2
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ class CPPCHECKLIB CheckOther : public Check {
192192
void overlappingWriteUnion(const Token *tok);
193193
void overlappingWriteFunction(const Token *tok, const std::string& funcname);
194194

195+
void checkSuspiciousComma();
196+
195197
// Error messages..
196198
void checkComparisonFunctionIsAlwaysTrueOrFalseError(const Token* tok, const std::string &functionName, const std::string &varName, bool result);
197199
void checkCastIntToCharAndBackError(const Token *tok, const std::string &strFunctionName);

0 commit comments

Comments
 (0)