From e7db974606b4ac4e907b2015711d4f8155a9269e Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Fri, 17 Dec 2021 14:48:29 -0600 Subject: [PATCH] Fix 10621: FP arrayIndexOutOfBoundsCond with multiple index checks (#3640) --- lib/valueflow.cpp | 15 ++++++++++----- test/testbufferoverrun.cpp | 11 +++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 65fe423d3..8198951e8 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1225,12 +1225,17 @@ static void valueFlowArray(TokenList *tokenlist) 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 - else if (tok->variable() && - tok->variable()->isArray() && - Token::simpleMatch(tok->astParent(), "=") && - tok == tok->astParent()->astOperand2() && - tok->astParent()->astOperand1() && + else if (tok->variable() && tok->variable()->isArray() && Token::simpleMatch(tok->astParent(), "=") && + astIsRHS(tok) && tok->astParent()->astOperand1() && tok->astParent()->astOperand1()->variable() && tok->astParent()->astOperand1()->variable()->isPointer()) { ValueFlow::Value value; diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index f3abe0bee..aed86428d 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -1743,6 +1743,17 @@ private: " return M[i];\n" "}\n"); 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() {