Repository navigation
Fix #15225 (FN arrayIndexOutOfBounds: sizeof(arr) unknown when the array dimension contains sizeof of a struct, variable or dereference) - #9036
Conversation
…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.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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 noteAssignment '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]givessizeof(a) = 42, which is correct. - A VLA with
int n = 5; char a[n];still gets no value forsizeof(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.
| 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; |
There was a problem hiding this comment.
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.
|
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 |
https://trac.cppcheck.net/ticket/15225
valueFlowSetConstantValue()evaluatedsizeof(arr)fromVariable::dimensions(), but a dimension such as10 + sizeof(struct S),10 + sizeof(var)or10 + sizeof(*p)is only marked known bySymbolDatabase::setArrayDimensionsUsingValueFlow(), which runs after all ValueFlow passes. Sosizeof(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.