Fix BufferOverrun TODO (#4300)

* Fix BufferOverrun TODO

* clang-tidy
This commit is contained in:
chrchr-github 2022-07-22 07:24:34 +02:00 committed by GitHub
parent 79f091c59a
commit 0d72216cca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -318,6 +318,21 @@ void CheckBufferOverrun::arrayIndex()
if (!getDimensionsEtc(tok->astOperand1(), mSettings, &dimensions, &errorPath, &mightBeLarger, &path))
continue;
const Variable* const var = array->variable();
if (var && var->isArgument() && var->scope()) {
const Token* changeTok = var->scope()->bodyStart;
bool isChanged = false;
while ((changeTok = findVariableChanged(changeTok->next(), var->scope()->bodyEnd, /*indirect*/ 0, var->declarationId(),
/*globalvar*/ false, mSettings, mTokenizer->isCPP()))) {
if (!Token::simpleMatch(changeTok->astParent(), "[")) {
isChanged = true;
break;
}
}
if (isChanged)
continue;
}
// Positive index
if (!mightBeLarger) { // TODO check arrays with dim 1 also
const std::vector<ValueFlow::Value>& indexValues =

View File

@ -2641,7 +2641,13 @@ private:
" a += 4;\n"
" a[-1] = 0;\n"
"}");
TODO_ASSERT_EQUALS("", "[test.cpp:3]: (error) Array 'a[10]' accessed at index -1, which is out of bounds.\n", errout.str());
ASSERT_EQUALS("", errout.str());
check("void f(char a[10]) {\n"
" a[0] = 0;\n"
" a[-1] = 0;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Array 'a[10]' accessed at index -1, which is out of bounds.\n", errout.str());
}
void array_index_enum_array() { // #8439