Skip to content

fix #5591: Add check for meaningless comma operator in if statement, misplaced ')' #7406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4384,6 +4384,41 @@ void CheckOther::overlappingWriteFunction(const Token *tok, const std::string& f
reportError(tok, Severity::error, "overlappingWriteFunction", "Overlapping read/write in " + funcname + "() is undefined behavior");
}

void CheckOther::checkSuspiciousComma()
{
if (!mSettings->severity.isEnabled(Severity::style)) {
return;
}

logChecker("CheckOther::suspiciousComma");

for (const Token* tok = mTokenizer->list.front(); tok; tok = tok->next()) {
if (tok->str() == "," && tok->isBinaryOp()) {
const Token * parent = tok->astParent();
if (parent && Token::Match(parent->previous(), "if|while (")) {
if (tok->strAt(-1) == ")") {
const Function * func = tok->linkAt(-1)->previous()->function();
if (func && func->initArgCount > 0) {
const Token * r_op = tok->astOperand2();
if (r_op && (r_op->isVariable() ||
r_op->tokType() == Token::eNumber ||
r_op->tokType() == Token::eString ||
r_op->tokType() == Token::eChar ||
r_op->tokType() == Token::eBoolean)) {
checkSuspiciousCommaError(tok);
}
}
}
}
}
}
}

void CheckOther::checkSuspiciousCommaError(const Token *tok)
{
reportError(tok, Severity::style, "suspiciousComma", "There is a suspicious comma expression in an if/while condition statement.");
}

void CheckOther::runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger)
{
CheckOther checkOther(&tokenizer, &tokenizer.getSettings(), errorLogger);
Expand Down Expand Up @@ -4431,6 +4466,7 @@ void CheckOther::runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger)
checkOther.checkAccessOfMovedVariable();
checkOther.checkModuloOfOne();
checkOther.checkOverlappingWrite();
checkOther.checkSuspiciousComma();
}

void CheckOther::getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const
Expand Down Expand Up @@ -4503,6 +4539,7 @@ void CheckOther::getErrorMessages(ErrorLogger *errorLogger, const Settings *sett
c.comparePointersError(nullptr, nullptr, nullptr);
c.redundantAssignmentError(nullptr, nullptr, "var", false);
c.redundantInitializationError(nullptr, nullptr, "var", false);
c.checkSuspiciousCommaError(nullptr);

const std::vector<const Token *> nullvec;
c.funcArgOrderDifferent("function", nullptr, nullptr, nullvec, nullvec);
Expand Down
6 changes: 5 additions & 1 deletion lib/checkother.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ class CPPCHECKLIB CheckOther : public Check {
void overlappingWriteUnion(const Token *tok);
void overlappingWriteFunction(const Token *tok, const std::string& funcname);

void checkSuspiciousComma();

// Error messages..
void checkComparisonFunctionIsAlwaysTrueOrFalseError(const Token* tok, const std::string &functionName, const std::string &varName, bool result);
void checkCastIntToCharAndBackError(const Token *tok, const std::string &strFunctionName);
Expand Down Expand Up @@ -249,6 +251,7 @@ class CPPCHECKLIB CheckOther : public Check {
void knownPointerToBoolError(const Token* tok, const ValueFlow::Value* value);
void comparePointersError(const Token *tok, const ValueFlow::Value *v1, const ValueFlow::Value *v2);
void checkModuloOfOneError(const Token *tok);
void checkSuspiciousCommaError(const Token *tok);

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

Expand Down Expand Up @@ -312,7 +315,8 @@ class CPPCHECKLIB CheckOther : public Check {
"- shadow variable.\n"
"- variable can be declared const.\n"
"- calculating modulo of one.\n"
"- known function argument, suspicious calculation.\n";
"- known function argument, suspicious calculation.\n"
"- suspicious comma in if/while condition statement.\n";
}

bool diag(const Token* tok) {
Expand Down
21 changes: 21 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ class TestOther : public TestFixture {

TEST_CASE(knownConditionFloating);
TEST_CASE(knownConditionPrefixed);

TEST_CASE(suspiciousComma);
}

#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
Expand Down Expand Up @@ -13070,6 +13072,25 @@ class TestOther : public TestFixture {
"[test.cpp:2:13] -> [test.cpp:3:11]: (style) The comparison 'i > +1' is always false. [knownConditionTrueFalse]\n",
errout_str());
}

void suspiciousComma()
{
check("void f(int a, int b = 0);\n"
"void foo() {\n"
" if (f(100), 100) {}\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:3:15]: (style) There is a suspicious comma expression in an if/while condition statement. [suspiciousComma]\n",
errout_str());

check("void f(int a, int b = 0);\n"
"void foo() {\n"
" while (f(100), 100) {}\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:3:18]: (style) There is a suspicious comma expression in an if/while condition statement. [suspiciousComma]\n",
errout_str());
}
};

REGISTER_TEST(TestOther)
Loading