From 5789ff1fdbfbcdacb7629249772e24d3b09f16dc Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 1 Sep 2022 19:48:20 +0200 Subject: [PATCH] Fix #11020 FP arrayIndexOutOfBounds if string literals are assigned conditionally --- 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 72d7c00d1e5..fbb00e4961a 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 5d9e8bf3e34..c7b2be83b88 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -2524,6 +2524,15 @@ class TestBufferOverrun : public TestFixture { " 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() {