STL: Fixed outOfBounds false positive

This commit is contained in:
Daniel Marjamäki 2019-08-24 15:40:29 +02:00
parent 9d26be8380
commit 996daaee4e
2 changed files with 7 additions and 1 deletions

View File

@ -87,7 +87,7 @@ void CheckStl::outOfBounds()
} }
if (!container->arrayLike_indexOp && !container->stdStringLike) if (!container->arrayLike_indexOp && !container->stdStringLike)
continue; continue;
if (value.intvalue == 0 && Token::Match(parent, "[")) { if (value.intvalue == 0 && Token::Match(parent, "[") && tok == parent->astOperand1()) {
outOfBoundsError(parent, tok->expressionString(), &value, "", nullptr); outOfBoundsError(parent, tok->expressionString(), &value, "", nullptr);
continue; continue;
} }

View File

@ -305,6 +305,12 @@ private:
" return (*pv)[42];\n" " return (*pv)[42];\n"
"}\n"); "}\n");
ASSERT_EQUALS("test.cpp:4:error:Out of bounds access in expression '(*pv)[42]' because '*pv' is empty.\n", errout.str()); ASSERT_EQUALS("test.cpp:4:error:Out of bounds access in expression '(*pv)[42]' because '*pv' is empty.\n", errout.str());
checkNormal("void f() {\n"
" std::string s;\n"
" ++abc[s];\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
void outOfBoundsIndexExpression() { void outOfBoundsIndexExpression() {