From c6d93a4d14661b4090001199456179839f8be273 Mon Sep 17 00:00:00 2001 From: Jay Beavers Date: Mon, 16 Nov 2026 15:24:58 -0800 Subject: [PATCH 1/3] Fix #15225 (FN arrayIndexOutOfBounds: sizeof(arr) unknown when the array dimension contains sizeof of a struct, variable or dereference) A dimension such as 10 + sizeof(struct S) was only marked known by SymbolDatabase::setArrayDimensionsUsingValueFlow(), which runs after all ValueFlow passes. Every reader inside ValueFlow saw it as unknown, so sizeof(arr), sizeof(arr) / sizeof(arr[0]) and sizeof of a struct that contains such an array got no value. valueFlowNumber() now sets the dimension when the token walk reaches the end of the array declaration, where constant folding has already given the dimension token a known value. Declarations precede uses, so the existing sizeof code then works unchanged, including for chained declarations such as char b[sizeof(a)]. The pass after ValueFlow stays for dimensions that only become known later. With more dimensions known, sizeof of an array function parameter would get the size of the array more often. It is a pointer, so sizeof now gives the pointer size and sizeof(p) / sizeof(p[0]) is no longer evaluated as the element count. The condition moves to Variable::isDecayedArrayArgument(), shared with CheckSizeof. --- lib/checksizeof.cpp | 2 +- lib/symboldatabase.cpp | 5 +++ lib/symboldatabase.h | 6 +++ lib/valueflow.cpp | 24 +++++++++++ lib/vf_common.cpp | 8 +++- test/testbufferoverrun.cpp | 18 ++++++++ test/testvalueflow.cpp | 85 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 145 insertions(+), 3 deletions(-) diff --git a/lib/checksizeof.cpp b/lib/checksizeof.cpp index 2d9012078a6..8ac6dcb68fb 100644 --- a/lib/checksizeof.cpp +++ b/lib/checksizeof.cpp @@ -87,7 +87,7 @@ void CheckSizeofImpl::checkSizeofForArrayParameter() } const Variable *var = varTok->variable(); - if (var && var->isArray() && var->isArgument() && !var->isReference() && !(var->isStlType() && var->getTypeName() == "std::array")) + if (var && var->isDecayedArrayArgument()) sizeofForArrayParameterError(tok); } } diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 26801640e99..6fccad1a53d 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -2718,6 +2718,11 @@ bool Variable::isStlStringViewType() const return getFlag(fIsStlType) && valueType() && valueType()->container && valueType()->container->stdStringLike && valueType()->container->view; } +bool Variable::isDecayedArrayArgument() const +{ + return isArray() && isArgument() && !isReference() && !(isStlType() && getTypeName() == "std::array"); +} + std::string Variable::getTypeName() const { std::string ret; diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index 832bcddeab3..485564e8dc4 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -447,6 +447,12 @@ class CPPCHECKLIB Variable { return getFlag(fIsArray) && !getFlag(fIsPointer); } + /** + * Is variable an array function parameter, which is a pointer. + * @return true if array parameter that decays to a pointer, false if not + */ + bool isDecayedArrayArgument() const; + /** * Is pointer variable. * @return true if pointer, false otherwise diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 7f4cd5584e8..66e35d2a778 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -422,9 +422,33 @@ void ValueFlow::combineValueProperties(const ValueFlow::Value &value1, const Val result.path = value1.path; } +// Set the dimensions that are known when the array declaration ends at tok, so sizeof of the array is known +static void valueFlowArrayDimensions(const Token* tok) +{ + const Token* start = tok->link(); + while (Token::simpleMatch(start->previous(), "]")) + start = start->previous()->link(); + const Token* const nameTok = start->previous(); + const Variable* const var = nameTok ? nameTok->variable() : nullptr; + if (!var || var->nameToken() != nameTok || !var->isArray()) + return; + for (const Dimension& constDimension : var->dimensions()) { + auto& dimension = const_cast(constDimension); + if (dimension.known || dimension.num != 0 || !dimension.tok) + continue; + const ValueFlow::Value* const v = dimension.tok->getKnownValue(ValueFlow::Value::ValueType::INT); + if (v && v->intvalue > 0) { + dimension.known = true; + dimension.num = v->intvalue; + } + } +} + static void valueFlowNumber(TokenList &tokenlist, const Settings& settings) { for (Token *tok = tokenlist.front(); tok;) { + if (tok->str() == "]" && !Token::simpleMatch(tok->next(), "[")) + valueFlowArrayDimensions(tok); tok = ValueFlow::valueFlowSetConstantValue(tok, settings); } diff --git a/lib/vf_common.cpp b/lib/vf_common.cpp index 9b1d0387f38..a96c0ea9ad4 100644 --- a/lib/vf_common.cpp +++ b/lib/vf_common.cpp @@ -234,6 +234,7 @@ namespace ValueFlow if (varid1 && sz1->variable() && sz1->variable()->isArray() && + !sz1->variable()->isDecayedArrayArgument() && !sz1->variable()->dimensions().empty() && sz1->variable()->dimensionKnown(0) && Token::Match(sz2->astOperand2(), "*|[") && Token::Match(sz2->astOperand2()->astOperand1(), "%varid%", varid1)) { @@ -245,8 +246,11 @@ namespace ValueFlow } else if (Token::Match(tok2, "%var% )")) { if (const Variable *var = tok2->variable()) { // find the size of the type + const bool isPointer = var->isDecayedArrayArgument(); size_t size = 0; - if (var->isEnumType()) { + if (isPointer) { + size = settings.platform.sizeof_pointer; + } else if (var->isEnumType()) { size = settings.platform.sizeof_int; if (var->type()->classScope && var->type()->classScope->enumType) size = getSizeOfType(var->type()->classScope->enumType, settings); @@ -262,7 +266,7 @@ namespace ValueFlow } // find the number of elements size_t count = 1; - for (size_t i = 0; i < var->dimensions().size(); ++i) { + for (size_t i = 0; !isPointer && i < var->dimensions().size(); ++i) { if (var->dimensionKnown(i)) count *= var->dimension(i); else diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index d639ec142eb..01759d31352 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -166,6 +166,7 @@ class TestBufferOverrun : public TestFixture { TEST_CASE(array_index_76); TEST_CASE(array_index_77); TEST_CASE(array_index_78); + TEST_CASE(array_index_79); // #15225 TEST_CASE(array_index_multidim); TEST_CASE(array_index_switch_in_for); TEST_CASE(array_index_for_in_for); // FP: #2634 @@ -2048,6 +2049,23 @@ class TestBufferOverrun : public TestFixture { ASSERT_EQUALS("", errout_str()); } + void array_index_79() + { + check("struct S { char c[4]; };\n" // #15225 + "void f() {\n" + " char a[10 + sizeof(struct S)];\n" + " a[sizeof(a)] = 0;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4:6]: (error) Array 'a[14]' accessed at index 14, which is out of bounds. [arrayIndexOutOfBounds]\n", + errout_str()); + + check("void f(char a[10]) {\n" + " a[sizeof(a)] = 0;\n" + " a[sizeof(a) / sizeof(a[0])] = 0;\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + } + void array_index_multidim() { check("void f()\n" "{\n" diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index c7f0cd63eb0..6379240bb45 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1547,6 +1547,91 @@ class TestValueFlow : public TestFixture { ASSERT_EQUALS(1U, values.size()); ASSERT_EQUALS(2, values.back().intvalue); + code = "struct S { char c[4]; };\n" + "void f() {\n" + " char a[10 + sizeof(struct S)];\n" + " x = sizeof(a);\n" + "}\n"; + values = tokenValues(code, "( a )"); + ASSERT_EQUALS(1U, values.size()); + ASSERT_EQUALS(14, values.back().intvalue); + + code = "void f() {\n" + " short s;\n" + " char a[10 + sizeof(s)];\n" + " x = sizeof(a);\n" + "}\n"; + values = tokenValues(code, "( a )"); + ASSERT_EQUALS(1U, values.size()); + ASSERT_EQUALS(12, values.back().intvalue); + + code = "struct S { char c[4]; };\n" + "struct S *p;\n" + "void f() {\n" + " char a[10 + sizeof(*p)];\n" + " x = sizeof(a);\n" + "}\n"; + values = tokenValues(code, "( a )"); + ASSERT_EQUALS(1U, values.size()); + ASSERT_EQUALS(14, values.back().intvalue); + + code = "struct S { char c[4]; };\n" + "void f() {\n" + " int a[2 + sizeof(struct S)];\n" + " x = sizeof(a) / sizeof(a[0]);\n" + "}\n"; + values = tokenValues(code, "/"); + ASSERT_EQUALS(1U, values.size()); + ASSERT_EQUALS(6, values.back().intvalue); + + code = "struct S { char c[4]; };\n" + "struct T { char m[10 + sizeof(struct S)]; };\n" + "void f() {\n" + " x = sizeof(struct T);\n" + "}\n"; + values = tokenValues(code, "( struct T )"); + ASSERT_EQUALS(1U, values.size()); + ASSERT_EQUALS(14, values.back().intvalue); + + code = "struct S { char c[4]; };\n" + "void f() {\n" + " char a[10 + sizeof(struct S)];\n" + " char b[sizeof(a)][2];\n" + " x = sizeof(b);\n" + "}\n"; + values = tokenValues(code, "( b )"); + ASSERT_EQUALS(1U, values.size()); + ASSERT_EQUALS(28, values.back().intvalue); + + code = "void f(char a[10]) {\n" // array parameter is a pointer + " x = sizeof(a);\n" + "}\n"; + values = tokenValues(code, "( a )"); + ASSERT_EQUALS(1U, values.size()); + ASSERT_EQUALS(settings.platform.sizeof_pointer, values.back().intvalue); + + code = "struct S { char c[4]; };\n" + "void f(char a[10 + sizeof(struct S)]) {\n" + " x = sizeof(a);\n" + "}\n"; + values = tokenValues(code, "( a )"); + ASSERT_EQUALS(1U, values.size()); + ASSERT_EQUALS(settings.platform.sizeof_pointer, values.back().intvalue); + + code = "void f(char (&a)[10]) {\n" + " x = sizeof(a);\n" + "}\n"; + values = tokenValues(code, "( a )"); + ASSERT_EQUALS(1U, values.size()); + ASSERT_EQUALS(10, values.back().intvalue); + + code = "void f(std::array a) {\n" + " x = sizeof(a);\n" + "}\n"; + values = tokenValues(code, "( a )"); + ASSERT_EQUALS(1U, values.size()); + ASSERT_EQUALS(10, values.back().intvalue); + #define CHECK(A, B, C, D) \ do { \ code = "enum " A " E " B " { E0, E1 };\n" \ From 825d0c85d35aa51ad033a78e434ecaf566291b41 Mon Sep 17 00:00:00 2001 From: Jay Beavers Date: Tue, 17 Nov 2026 09:25:27 -0800 Subject: [PATCH 2/3] Use linkAt(-1) in valueFlowArrayDimensions to fix selfcheck redundantNextPrevious --- lib/valueflow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 66e35d2a778..7e07adbca1a 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -427,7 +427,7 @@ static void valueFlowArrayDimensions(const Token* tok) { const Token* start = tok->link(); while (Token::simpleMatch(start->previous(), "]")) - start = start->previous()->link(); + start = start->linkAt(-1); const Token* const nameTok = start->previous(); const Variable* const var = nameTok ? nameTok->variable() : nullptr; if (!var || var->nameToken() != nameTok || !var->isArray()) From ff5868be0347706eb80b1b81d0d186b8d11497d7 Mon Sep 17 00:00:00 2001 From: Jay Beavers Date: Thu, 19 Nov 2026 16:26:13 -0800 Subject: [PATCH 3/3] Treat an array of std::array parameter as decayed in isDecayedArrayArgument --- lib/symboldatabase.cpp | 4 +++- test/testsizeof.cpp | 6 +++++- test/testvalueflow.cpp | 7 +++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 6fccad1a53d..f1580ee716f 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -2720,7 +2720,9 @@ bool Variable::isStlStringViewType() const bool Variable::isDecayedArrayArgument() const { - return isArray() && isArgument() && !isReference() && !(isStlType() && getTypeName() == "std::array"); + if (!isArray() || !isArgument() || isReference()) + return false; + return !(isStlType() && getTypeName() == "std::array") || Token::simpleMatch(nameToken()->next(), "["); } std::string Variable::getTypeName() const diff --git a/test/testsizeof.cpp b/test/testsizeof.cpp index 0367c993951..fd95506bf17 100644 --- a/test/testsizeof.cpp +++ b/test/testsizeof.cpp @@ -339,8 +339,12 @@ class TestSizeof : public TestFixture { "}\n" "int g(std::string a[2]) {\n" " return sizeof(a);\n" + "}\n" + "int h(std::array a[2]) {\n" + " return sizeof(a);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5:12]: (warning) Using 'sizeof' on array given as function argument returns size of a pointer. [sizeofwithsilentarraypointer]\n", + ASSERT_EQUALS("[test.cpp:5:12]: (warning) Using 'sizeof' on array given as function argument returns size of a pointer. [sizeofwithsilentarraypointer]\n" + "[test.cpp:8:12]: (warning) Using 'sizeof' on array given as function argument returns size of a pointer. [sizeofwithsilentarraypointer]\n", errout_str()); } diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 6379240bb45..41f3791a45a 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1632,6 +1632,13 @@ class TestValueFlow : public TestFixture { ASSERT_EQUALS(1U, values.size()); ASSERT_EQUALS(10, values.back().intvalue); + code = "void f(std::array a[2]) {\n" // array of std::array parameter is a pointer + " x = sizeof(a);\n" + "}\n"; + values = tokenValues(code, "( a )"); + ASSERT_EQUALS(1U, values.size()); + ASSERT_EQUALS(settings.platform.sizeof_pointer, values.back().intvalue); + #define CHECK(A, B, C, D) \ do { \ code = "enum " A " E " B " { E0, E1 };\n" \