From 79daad8ff4ecbbc5831c2616d9dc586cc5d51526 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 1 Sep 2022 22:59:46 +0200 Subject: [PATCH] Fix #11020 FP arrayIndexOutOfBounds if string literals are assigned conditionally (#4428) --- lib/programmemory.cpp | 2 +- test/testbufferoverrun.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index 72d7c00d1..fbb00e496 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -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; diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 9aa5eea15..7bd86d11e 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -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() {