Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/vf_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,12 @@ namespace ValueFlow
}
// find the number of elements
size_t count = 1;
for (size_t i = 0; i < var->dimensions().size(); ++i) {
if (var->dimensionKnown(i))
count *= var->dimension(i);
for (const Dimension& dim : var->dimensions()) {
const Value* v = dim.tok ? dim.tok->getKnownValue(Value::ValueType::INT) : nullptr;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

The fallback also applies to array function parameters, where sizeof(p) is the size of a pointer. For void f(char p[10 + sizeof(struct S)]) { int x = sizeof(p); p[x] = 0; }, sizeof(p) now gets the value 14 instead of 8, and that gives a new false arrayIndexOutOfBounds (base reports nothing). The same wrong value already exists when the dimension is folded to a literal, so skipping var->isArgument() here would fix both.

if (dim.known)
count *= dim.num;
else if (v && v->intvalue > 0)
count *= v->intvalue;
else
count = 0;
}
Expand Down
28 changes: 28 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,34 @@ 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);

#define CHECK(A, B, C, D) \
do { \
code = "enum " A " E " B " { E0, E1 };\n" \
Expand Down
Loading