Skip to content

Fix #15225 (FN arrayIndexOutOfBounds: sizeof(arr) unknown when the array dimension contains sizeof of a struct, variable or dereference) - #9036

Closed
JayToltTech wants to merge 1 commit into
cppcheck-opensource:mainfrom
JayToltTech:fix-sizeof-array-dim
Closed

JayToltTech wants to merge 1 commit into
cppcheck-opensource:mainfrom
JayToltTech:fix-sizeof-array-dim

Conversation

@JayToltTech

Copy link
Copy Markdown

https://trac.cppcheck.net/ticket/15225

valueFlowSetConstantValue() evaluated sizeof(arr) from Variable::dimensions(), but a dimension such as 10 + sizeof(struct S), 10 + sizeof(var) or 10 + sizeof(*p) is only marked known by SymbolDatabase::setArrayDimensionsUsingValueFlow(), which runs after all ValueFlow passes. So sizeof(arr) got no value and checks relying on it, e.g. arrayIndexOutOfBounds, missed.

The fix falls back to the dimension token's known value, which constant folding has already set by the time the use is reached.

Tests: three cases added to TestValueFlow::valueFlowSizeof (struct, variable, dereference); they fail without the fix. Full testrunner passes locally; runformat (uncrustify 0.80.1) is clean.

…ray dimension contains sizeof of a struct, variable or dereference)

valueFlowSetConstantValue() evaluated sizeof(arr) from Variable::dimensions(),
but a dimension like 10 + sizeof(struct S) is only marked known by
setArrayDimensionsUsingValueFlow(), which runs after all valueflow passes.
sizeof(arr) therefore got no value, hiding e.g. arrayIndexOutOfBounds.
Fall back to the dimension token's known value, which constant folding has
already set by the time the use is reached.
Copilot AI balanced review requested due to automatic review settings November 16, 2026 17:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@dmcppcheck dmcppcheck left a comment

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 new fallback also applies to array function parameters, so it adds a false positive. In void f(char p[10 + sizeof(struct S)]), sizeof(p) is the size of a pointer (8 on unix64). The PR now gives it the value 14, and p[sizeof(p)] gets a false arrayIndexOutOfBounds that the base version does not report. The same wrong value already exists for parameters whose dimension the tokenizer folds itself (for example char p[10 + sizeof(int)]), but the PR spreads it to the dimensions that this PR makes known. Skipping var->isArgument() (or arrays that decay to pointers) in this branch would fix both. Apart from that, the change works for the cases in the ticket and the existing tests pass.

Testing

I built cppcheck and testrunner in pr/ and cppcheck in base/.

./testrunner TestValueFlow TestBufferOverrun TestSizeof TestSymbolDatabase TestOther: 990 tests, 0 failed.

The ticket's case works. With char a[10 + sizeof(struct S)]; a[sizeof(a)] = 0;, the PR now reports arrayIndexOutOfBounds and base does not.

New false positive (scratch/t2.c):

struct S { char c[4]; };
void f4(char p[10 + sizeof(struct S)]) {
    int x = sizeof(p);
    p[x] = 0;
}

cppcheck -q --enable=warning,style --platform=unix64 t2.c

  • base: only sizeofwithsilentarraypointer
  • PR: also t2.c:4:6: error: Array 'p[14]' accessed at index 14, which is out of bounds. [arrayIndexOutOfBounds] with the note Assignment 'x=sizeof(p)', assigned value is 14. sizeof(p) is the size of a pointer (8), so this is wrong.

Base already gives the same wrong value for void f(char p[10 + sizeof(int)]), where the tokenizer folds the dimension, so the underlying problem is older. The PR spreads it to the new kinds of dimensions.

Other checks:

  • A 2D array char a[10 + sizeof(struct S)][3] gives sizeof(a) = 42, which is correct.
  • A VLA with int n = 5; char a[n]; still gets no value for sizeof(a). The PR does not change this and there is no wrong value.

Automated review of ea816e8 by Claude. The pull request was built and tested locally.

Comment thread lib/vf_common.cpp
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.

@JayToltTech

Copy link
Copy Markdown
Author

Superseded by #9037. The review here was right about array parameters, and the same root cause (the dimension only becoming known after ValueFlow) also left sizeof(arr) / sizeof(arr[0]) and sizeof of a struct containing such an array without a value. #9037 sets the dimension during the constant pass instead of patching this one reader, and gives sizeof of an array parameter the pointer size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants