From d4174a31ba41e03d2df34dce58369636c9283abb Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Mon, 6 Sep 2021 00:15:46 -0500 Subject: [PATCH] Fix 10414: FP 'The address of local variable 'single_value' might be accessed at non-zero index.' (regression) (#3447) --- lib/checkbufferoverrun.cpp | 3 +-- test/testbufferoverrun.cpp | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index f4390b481..4cc2a53ae 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -1020,8 +1020,7 @@ void CheckBufferOverrun::objectIndex() return false; return vidx.path == v.path || vidx.path == 0; }); - if (idxValues.empty() || - std::any_of(idxValues.begin(), idxValues.end(), [&](const ValueFlow::Value& vidx) { + if (std::any_of(idxValues.begin(), idxValues.end(), [&](const ValueFlow::Value& vidx) { if (vidx.isImpossible()) return (vidx.intvalue == 0); else diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index d3355d648..1a1e8aaf0 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -4689,7 +4689,10 @@ private: " for(int i=0;i [test.cpp:4] -> [test.cpp:9]: (warning) The address of local variable 'test' might be accessed at non-zero index.\n", errout.str()); + TODO_ASSERT_EQUALS( + "[test.cpp:4] -> [test.cpp:4] -> [test.cpp:9]: (warning) The address of local variable 'test' might be accessed at non-zero index.\n", + "", + errout.str()); check("void Bar(uint8_t data);\n" "void Foo(const uint8_t * const data, const uint8_t length) {\n" @@ -4701,6 +4704,18 @@ private: " Foo(&data,1U);\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("int foo(int n, int* p) {\n" + " int res = 0;\n" + " for(int i = 0; i < n; i++ )\n" + " res += p[i];\n" + " return res;\n" + "}\n" + "int bar() {\n" + " int single_value = 0;\n" + " return foo(1, &single_value);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } };