From 370d71b64b3540787f8d7e107b1ed0d7e4cff7f8 Mon Sep 17 00:00:00 2001 From: Jay Beavers Date: Mon, 16 Nov 2026 15:24:58 -0800 Subject: [PATCH 1/2] 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 62d4012b823..f9ed3a5caec 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 0e0bf5ce385..e098ae01954 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -165,6 +165,7 @@ class TestBufferOverrun : public TestFixture { TEST_CASE(array_index_75); TEST_CASE(array_index_76); TEST_CASE(array_index_77); + TEST_CASE(array_index_78); // #15225 TEST_CASE(array_index_multidim); TEST_CASE(array_index_switch_in_for); TEST_CASE(array_index_for_in_for); // FP: #2634 @@ -2031,6 +2032,23 @@ class TestBufferOverrun : public TestFixture { errout_str()); } + void array_index_78() + { + 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 7e116c7ebd4..5f8ef9aa032 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1539,6 +1539,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 a985a2cd5e5b08ca03528abcd075a478a3e62e7c Mon Sep 17 00:00:00 2001 From: Jay Beavers Date: Tue, 17 Nov 2026 09:25:27 -0800 Subject: [PATCH 2/2] 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 f9ed3a5caec..0e4adcf80a9 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())