Fix 10621: FP arrayIndexOutOfBoundsCond with multiple index checks (#3640)

This commit is contained in:
Paul Fultz II 2021-12-17 14:48:29 -06:00 committed by GitHub
parent ffae40d2f4
commit e7db974606
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 5 deletions

View File

@ -1225,12 +1225,17 @@ static void valueFlowArray(TokenList *tokenlist)
setTokenValue(tok, value, tokenlist->getSettings()); setTokenValue(tok, value, tokenlist->getSettings());
} }
// const array decl
else if (tok->variable() && tok->variable()->isArray() && tok->variable()->isConst() &&
tok->variable()->nameToken() == tok && Token::Match(tok, "%var% [ %num%| ] = {")) {
const Token* rhstok = tok->next()->link()->tokAt(2);
constantArrays[tok->varId()] = rhstok;
tok = rhstok->link();
}
// pointer = array // pointer = array
else if (tok->variable() && else if (tok->variable() && tok->variable()->isArray() && Token::simpleMatch(tok->astParent(), "=") &&
tok->variable()->isArray() && astIsRHS(tok) && tok->astParent()->astOperand1() &&
Token::simpleMatch(tok->astParent(), "=") &&
tok == tok->astParent()->astOperand2() &&
tok->astParent()->astOperand1() &&
tok->astParent()->astOperand1()->variable() && tok->astParent()->astOperand1()->variable() &&
tok->astParent()->astOperand1()->variable()->isPointer()) { tok->astParent()->astOperand1()->variable()->isPointer()) {
ValueFlow::Value value; ValueFlow::Value value;

View File

@ -1743,6 +1743,17 @@ private:
" return M[i];\n" " return M[i];\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("struct S { enum E { e0 }; };\n"
"const S::E M[4] = { S::E:e0, S::E:e0, S::E:e0, S::E:e0 };\n"
"int f(int i) {\n"
" if (i > std::size(M) + 1)\n"
" return -1;\n"
" if (i < 0 || i >= std::size(M))\n"
" return 0;\n"
" return M[i]; \n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void array_index_multidim() { void array_index_multidim() {