Skip to content

Commit c43201b

Browse files
committed
fix #5591
1 parent 5ac33c8 commit c43201b

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

lib/checkother.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4367,6 +4367,41 @@ 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("CheckOther::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::Match(parent->previous(), "if|while (")) {
4382+
if (tok->previous()->str() == ")") {
4383+
const Function * func = tok->linkAt(-1)->previous()->function();
4384+
if (func && func->initArgCount > 0) {
4385+
const Token * r_op = tok->astOperand2();
4386+
if (r_op && (r_op->isVariable() ||
4387+
r_op->tokType() == Token::eNumber ||
4388+
r_op->tokType() == Token::eString ||
4389+
r_op->tokType() == Token::eChar ||
4390+
r_op->tokType() == Token::eBoolean)) {
4391+
checkSuspiciousCommaError(tok);
4392+
}
4393+
}
4394+
}
4395+
}
4396+
}
4397+
}
4398+
}
4399+
4400+
void CheckOther::checkSuspiciousCommaError(const Token *tok)
4401+
{
4402+
reportError(tok, Severity::style, "warnSuspiciousComma", "There is an suspicious comma expression used as a condition in an if/while condition statement.");
4403+
}
4404+
43704405
void CheckOther::runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger)
43714406
{
43724407
CheckOther checkOther(&tokenizer, &tokenizer.getSettings(), errorLogger);
@@ -4414,6 +4449,7 @@ void CheckOther::runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger)
44144449
checkOther.checkAccessOfMovedVariable();
44154450
checkOther.checkModuloOfOne();
44164451
checkOther.checkOverlappingWrite();
4452+
checkOther.checkSuspiciousComma();
44174453
}
44184454

44194455
void CheckOther::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const
@@ -4486,6 +4522,7 @@ void CheckOther::getErrorMessages(ErrorLogger *errorLogger, const Settings *sett
44864522
c.comparePointersError(nullptr, nullptr, nullptr);
44874523
c.redundantAssignmentError(nullptr, nullptr, "var", false);
44884524
c.redundantInitializationError(nullptr, nullptr, "var", false);
4525+
c.checkSuspiciousCommaError(nullptr);
44894526

44904527
const std::vector<const Token *> nullvec;
44914528
c.funcArgOrderDifferent("function", nullptr, nullptr, nullvec, nullvec);

lib/checkother.h

Lines changed: 4 additions & 0 deletions
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);
@@ -249,6 +251,7 @@ class CPPCHECKLIB CheckOther : public Check {
249251
void knownPointerToBoolError(const Token* tok, const ValueFlow::Value* value);
250252
void comparePointersError(const Token *tok, const ValueFlow::Value *v1, const ValueFlow::Value *v2);
251253
void checkModuloOfOneError(const Token *tok);
254+
void checkSuspiciousCommaError(const Token *tok);
252255

253256
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override;
254257

@@ -313,6 +316,7 @@ class CPPCHECKLIB CheckOther : public Check {
313316
"- variable can be declared const.\n"
314317
"- calculating modulo of one.\n"
315318
"- known function argument, suspicious calculation.\n";
319+
"- suspicious comma in if/while condition statement.\n";
316320
}
317321

318322
bool diag(const Token* tok) {

test/testother.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,8 @@ class TestOther : public TestFixture {
304304

305305
TEST_CASE(knownConditionFloating);
306306
TEST_CASE(knownConditionPrefixed);
307+
308+
TEST_CASE(suspiciousComma);
307309
}
308310

309311
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
@@ -13047,6 +13049,25 @@ class TestOther : public TestFixture {
1304713049
"[test.cpp:2] -> [test.cpp:3]: (style) The comparison 'i > +1' is always false.\n",
1304813050
errout_str());
1304913051
}
13052+
13053+
void suspiciousComma()
13054+
{
13055+
check("void f(int a, int b = 0);\n"
13056+
"void foo() {\n"
13057+
" if (f(100), 100) {}\n"
13058+
"}\n");
13059+
ASSERT_EQUALS(
13060+
"[test.cpp:3]: (style) There is an suspicious comma expression used as a condition in an if/while condition statement.\n",
13061+
errout_str());
13062+
13063+
check("void f(int a, int b = 0);\n"
13064+
"void foo() {\n"
13065+
" while (f(100), 100) {}\n"
13066+
"}\n");
13067+
ASSERT_EQUALS(
13068+
"[test.cpp:3]: (style) There is an suspicious comma expression used as a condition in an if/while condition statement.\n",
13069+
errout_str());
13070+
}
1305013071
};
1305113072

1305213073
REGISTER_TEST(TestOther)

0 commit comments

Comments
 (0)