Skip to content

Commit 3565303

Browse files
committed
Leave template-argument array dimensions to setArrayDimensionsUsingValueFlow
For std::array<char, 3 + sizeof(S)> a[2], dimension.tok is only the first token of the template argument and may have no AST, so its known value was that of 3 alone. With num set, the later pass skipped the dimension and sizeof(a) became 3, giving a false arrayIndexOutOfBounds.
1 parent 37c4492 commit 3565303

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎lib/valueflow.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ static void valueFlowArrayDimensions(const Token* tok)
436436
auto& dimension = const_cast<Dimension&>(constDimension);
437437
if (dimension.known || dimension.num != 0 || !dimension.tok)
438438
continue;
439+
if (Token::Match(dimension.tok->previous(), "[<,]"))
440+
continue;
439441
const ValueFlow::Value* const v = dimension.tok->getKnownValue(ValueFlow::Value::ValueType::INT);
440442
if (v && v->intvalue > 0) {
441443
dimension.known = true;

‎test/testbufferoverrun.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,6 +2064,14 @@ class TestBufferOverrun : public TestFixture {
20642064
" a[sizeof(a) / sizeof(a[0])] = 0;\n"
20652065
"}\n");
20662066
ASSERT_EQUALS("", errout_str());
2067+
2068+
check("struct S { char c[4]; };\n"
2069+
"void f() {\n"
2070+
" std::array<char, 3 + sizeof(S)> a[2];\n"
2071+
" char buf[sizeof(a)];\n"
2072+
" buf[5] = 0;\n"
2073+
"}\n");
2074+
ASSERT_EQUALS("", errout_str());
20672075
}
20682076

20692077
void array_index_multidim() {

0 commit comments

Comments
 (0)