Skip to content

Commit

Permalink
Fix 11850: false negative: knownConditionTrueFalse with std::string::…
Browse files Browse the repository at this point in the history
…empty() after modification (danmar#5307)
  • Loading branch information
pfultz2 authored Aug 9, 2023
1 parent 4e8c240 commit 24479c6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8806,6 +8806,29 @@ static void valueFlowContainerSize(TokenList& tokenlist,
value.setImpossible();
valueFlowForward(tok->linkAt(2), containerTok, value, tokenlist, settings);
}
} else if (Token::simpleMatch(tok, "+=") && astIsContainer(tok->astOperand1())) {
const Token* containerTok = tok->astOperand1();
const Token* valueTok = tok->astOperand2();
MathLib::bigint size = 0;
if (valueTok->tokType() == Token::eString)
size = Token::getStrLength(valueTok);
else if (astIsGenericChar(tok) || valueTok->tokType() == Token::eChar)
size = 1;
else if (const ValueFlow::Value* v1 = valueTok->getKnownValue(ValueFlow::Value::ValueType::TOK))
size = Token::getStrLength(v1->tokvalue);
else if (const ValueFlow::Value* v2 =
valueTok->getKnownValue(ValueFlow::Value::ValueType::CONTAINER_SIZE))
size = v2->intvalue;
if (size == 0)
continue;
ValueFlow::Value value(size - 1);
value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE;
value.bound = ValueFlow::Value::Bound::Lower;
value.setImpossible();
Token* next = nextAfterAstRightmostLeaf(tok);
if (!next)
next = tok->next();
valueFlowForward(next, containerTok, value, tokenlist, settings);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5013,6 +5013,16 @@ class TestCondition : public TestFixture {
" if(!s.empty()) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());

check("int f(std::string s) {\n"
" if (s.empty())\n"
" return -1;\n"
" s += '\\n';\n"
" if (s.empty())\n"
" return -1;\n"
" return -1;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (style) Condition 's.empty()' is always false\n", errout.str());
}

void alwaysTrueLoop()
Expand Down

0 comments on commit 24479c6

Please sign in to comment.