Fixed #8708 (FP: Accessing an item in container that is empty)

This commit is contained in:
Daniel Marjamäki 2018-08-27 13:49:47 +02:00
parent 4378327c43
commit 6223204a06
2 changed files with 12 additions and 0 deletions

View File

@ -3549,6 +3549,12 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold
if (!Token::Match(var->nameToken(), "%name% ;")) if (!Token::Match(var->nameToken(), "%name% ;"))
continue; continue;
ValueFlow::Value value(0); ValueFlow::Value value(0);
if (var->valueType()->container->size_templateArgNo >= 0) {
if (var->dimensions().size() == 1 && var->dimensions().front().known)
value.intvalue = var->dimensions().front().num;
else
continue;
}
value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE; value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE;
value.setKnown(); value.setKnown();
valueFlowContainerForward(var->nameToken()->next(), var->declarationId(), value, settings); valueFlowContainerForward(var->nameToken()->next(), var->declarationId(), value, settings);

View File

@ -3352,6 +3352,12 @@ private:
"}"; "}";
ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "ints . front"), 0)); ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "ints . front"), 0));
code = "void f() {\n"
" std::array<int,10> ints;\n" // Array size is 10
" ints.front();\n"
"}";
ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "ints . front"), 10));
code = "void f() {\n" code = "void f() {\n"
" std::string s=\"abc\";\n" // size of s is 3 " std::string s=\"abc\";\n" // size of s is 3
" s.size();\n" " s.size();\n"