Fix #11020 FP arrayIndexOutOfBounds if string literals are assigned conditionally (#4428)

This commit is contained in:
chrchr-github 2022-09-01 22:59:46 +02:00 committed by GitHub
parent 4779f0e172
commit 79daad8ff4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -1245,7 +1245,7 @@ static ValueFlow::Value executeImpl(const Token* expr, ProgramMemory& pm, const
auto tokvalue_it = std::find_if(expr->astOperand1()->values().begin(),
expr->astOperand1()->values().end(),
std::mem_fn(&ValueFlow::Value::isTokValue));
if (tokvalue_it == expr->astOperand1()->values().end()) {
if (tokvalue_it == expr->astOperand1()->values().end() || !tokvalue_it->isKnown()) {
return unknown;
}
tokvalue = tokvalue_it->tokvalue;

View File

@ -2524,6 +2524,15 @@ private:
" snprintf(str, sizeof(str), \"%hu\", port);\n"
"}", settings0, "test.c");
ASSERT_EQUALS("", errout.str());
check("int f(int x) {\n" // #11020
" const char* p = (x == 0 ? \"12345\" : \"ABC\");\n"
" int s = 0;\n"
" for (int i = 0; p[i]; i++)\n"
" s += p[i];\n"
" return s;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void array_index_same_struct_and_var_name() {