@@ -602,7 +602,7 @@ void CheckCondition::oppositeElseIfConditionError(const Token *ifCond, const Tok
602602
603603// ---------------------------------------------------------------------------
604604// - Opposite inner conditions => always false
605- // - (TODO) Same/Overlapping inner condition => always true
605+ // - Same/Overlapping inner condition => always true
606606// - same condition after early exit => always false
607607// ---------------------------------------------------------------------------
608608
@@ -759,6 +759,8 @@ void CheckCondition::multiCondition2()
759759 oppositeInnerConditionError (firstCondition, cond2, errorPath);
760760 } else if (!isReturnVar && isSameExpression (true , firstCondition, cond2, *mSettings , true , true , &errorPath)) {
761761 identicalInnerConditionError (firstCondition, cond2, errorPath);
762+ } else if (!isReturnVar && isOverlappingCond (cond2, firstCondition, true )) {
763+ overlappingInnerConditionError (firstCondition, cond2, errorPath);
762764 }
763765 }
764766 return ChildrenToVisit::none;
@@ -879,6 +881,21 @@ void CheckCondition::oppositeInnerConditionError(const Token *tok1, const Token*
879881 reportError (std::move (errorPath), Severity::warning, " oppositeInnerCondition" , msg, CWE398, Certainty::normal);
880882}
881883
884+ void CheckCondition::overlappingInnerConditionError (const Token *tok1, const Token* tok2, ErrorPath errorPath)
885+ {
886+ if (diag (tok1, tok2))
887+ return ;
888+ const std::string s1 (tok1 ? tok1->expressionString () : " x" );
889+ const std::string s2 (tok2 ? tok2->expressionString () : " x" );
890+ const std::string innerSmt = innerSmtString (tok2);
891+ errorPath.emplace_back (tok1, " outer condition: " + s1);
892+ errorPath.emplace_back (tok2, " overlapping inner condition: " + s2);
893+
894+ const std::string msg (" Overlapping inner '" + innerSmt + " ' condition is always true.\n "
895+ " Overlapping inner '" + innerSmt + " ' condition is always true (outer condition is '" + s1 + " ' and inner condition is '" + s2 + " ')." );
896+ reportError (std::move (errorPath), Severity::warning, " overlappingInnerCondition" , msg, CWE398, Certainty::normal);
897+ }
898+
882899void CheckCondition::identicalInnerConditionError (const Token *tok1, const Token* tok2, ErrorPath errorPath)
883900{
884901 if (diag (tok1, tok2))
@@ -2106,6 +2123,7 @@ void CheckCondition::getErrorMessages(ErrorLogger *errorLogger, const Settings *
21062123 c.comparisonError (nullptr , " &" , 6 , " ==" , 1 , false );
21072124 c.duplicateConditionError (nullptr , nullptr , ErrorPath{});
21082125 c.overlappingElseIfConditionError (nullptr , 1 );
2126+ c.overlappingInnerConditionError (nullptr , nullptr , ErrorPath ());
21092127 c.mismatchingBitAndError (nullptr , 0xf0 , nullptr , 1 );
21102128 c.oppositeInnerConditionError (nullptr , nullptr , ErrorPath{});
21112129 c.identicalInnerConditionError (nullptr , nullptr , ErrorPath{});
0 commit comments