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..f1580ee716f 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -2718,6 +2718,13 @@ bool Variable::isStlStringViewType() const return getFlag(fIsStlType) && valueType() && valueType()->container && valueType()->container->stdStringLike && valueType()->container->view; } +bool Variable::isDecayedArrayArgument() const +{ + if (!isArray() || !isArgument() || isReference()) + return false; + return !(isStlType() && getTypeName() == "std::array") || Token::simpleMatch(nameToken()->next(), "["); +} + 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..7e07adbca1a 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->linkAt(-1); + 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/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 c7f0cd63eb0..41f3791a45a 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1547,6 +1547,98 @@ 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); + + 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" \