MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / overlappingStrcmp

Method overlappingStrcmp

lib/checkstring.cpp:344–404  ·  view source on GitHub ↗

--------------------------------------------------------------------------- always true: strcmp(str,"a")==0 || strcmp(str,"b") TODO: Library configuration for string comparison functions ---------------------------------------------------------------------------

Source from the content-addressed store, hash-verified

342// TODO: Library configuration for string comparison functions
343//---------------------------------------------------------------------------
344void CheckStringImpl::overlappingStrcmp()
345{
346 if (!mSettings.severity.isEnabled(Severity::warning))
347 return;
348
349 logChecker("CheckString::overlappingStrcmp"); // warning
350
351 const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
352 for (const Scope * scope : symbolDatabase->functionScopes) {
353 for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
354 if (tok->str() != "||")
355 continue;
356 std::list<const Token *> equals0;
357 std::list<const Token *> notEquals0;
358 visitAstNodes(tok, [&](const Token * t) {
359 if (!t)
360 return ChildrenToVisit::none;
361 if (t->str() == "||") {
362 return ChildrenToVisit::op1_and_op2;
363 }
364 if (t->str() == "==") {
365 if (Token::simpleMatch(t->astOperand1(), "(") && Token::simpleMatch(t->astOperand2(), "0"))
366 equals0.push_back(t->astOperand1());
367 else if (Token::simpleMatch(t->astOperand2(), "(") && Token::simpleMatch(t->astOperand1(), "0"))
368 equals0.push_back(t->astOperand2());
369 return ChildrenToVisit::none;
370 }
371 if (t->str() == "!=") {
372 if (Token::simpleMatch(t->astOperand1(), "(") && Token::simpleMatch(t->astOperand2(), "0"))
373 notEquals0.push_back(t->astOperand1());
374 else if (Token::simpleMatch(t->astOperand2(), "(") && Token::simpleMatch(t->astOperand1(), "0"))
375 notEquals0.push_back(t->astOperand2());
376 return ChildrenToVisit::none;
377 }
378 if (t->str() == "!" && Token::simpleMatch(t->astOperand1(), "("))
379 equals0.push_back(t->astOperand1());
380 else if (t->str() == "(")
381 notEquals0.push_back(t);
382 return ChildrenToVisit::none;
383 });
384
385 for (const Token *eq0 : equals0) {
386 for (const Token * ne0 : notEquals0) {
387 if (!Token::Match(eq0->previous(), "strcmp|wcscmp ("))
388 continue;
389 if (!Token::Match(ne0->previous(), "strcmp|wcscmp ("))
390 continue;
391 const std::vector<const Token *> args1 = getArguments(eq0->previous());
392 const std::vector<const Token *> args2 = getArguments(ne0->previous());
393 if (args1.size() != 2 || args2.size() != 2)
394 continue;
395 if (args1[1]->isLiteral() &&
396 args2[1]->isLiteral() &&
397 args1[1]->str() != args2[1]->str() &&
398 isSameExpression(true, args1[0], args2[0], mSettings, true, false))
399 overlappingStrcmpError(eq0, ne0);
400 }
401 }

Callers 1

runChecksMethod · 0.80

Calls 11

visitAstNodesFunction · 0.85
isSameExpressionFunction · 0.85
nextMethod · 0.80
astOperand1Method · 0.80
astOperand2Method · 0.80
simpleMatchFunction · 0.70
getArgumentsFunction · 0.70
isEnabledMethod · 0.45
strMethod · 0.45
push_backMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected