Fix issue 9932: FP: containerOutOfBounds (#3217)

* Fix issue 9932: FP: containerOutOfBounds
This commit is contained in:
Paul Fultz II 2021-04-18 03:43:38 -05:00 committed by GitHub
parent 9a9043a07e
commit a772d652d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 2 deletions

View File

@ -6295,8 +6295,9 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold
value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE;
value.setKnown();
valueFlowContainerForward(tok->next(), tok->variable(), value, tokenlist);
} else if (action == Library::Container::Action::RESIZE && tok->tokAt(4)->hasKnownIntValue()) {
ValueFlow::Value value(tok->tokAt(4)->values().front());
} else if (action == Library::Container::Action::RESIZE && tok->tokAt(3)->astOperand2() &&
tok->tokAt(3)->astOperand2()->hasKnownIntValue()) {
ValueFlow::Value value(tok->tokAt(3)->astOperand2()->values().front());
value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE;
value.setKnown();
valueFlowContainerForward(tok->next(), tok->variable(), value, tokenlist);

View File

@ -5079,6 +5079,16 @@ private:
ASSERT_EQUALS("",
isPossibleContainerSizeValue(tokenValues(code, "x . front", ValueFlow::Value::ValueType::CONTAINER_SIZE), 0));
code = "int f() {\n"
" const size_t len = 6;\n"
" std::vector<char> v;\n"
" v.resize(1 + len);\n"
" return v.front();\n"
"}\n";
ASSERT_EQUALS(
"",
isKnownContainerSizeValue(tokenValues(code, "v . front", ValueFlow::Value::ValueType::CONTAINER_SIZE), 7));
code = "void f(std::string str) {\n"
" if (str == \"123\") {\n"
" bool x = (str == \"\");\n"