Skip to content

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

Open
JayToltTech wants to merge 2 commits into
cppcheck-opensource:mainfrom
JayToltTech:fix-15225-array-dim-early
Open

JayToltTech wants to merge 2 commits into
cppcheck-opensource:mainfrom
JayToltTech:fix-15225-array-dim-early

Conversation

@JayToltTech

@JayToltTech JayToltTech commented Nov 16, 2026 •

Copy link
Copy Markdown

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

Replaces #9036, which I've closed. That one patched a single reader of the array dimension, and the review there found it gave a false positive for array parameters. Looking at why turned up the same cause in two more places, so this fixes the cause.

A dimension like 10 + sizeof(struct S), 10 + sizeof(var) or 10 + sizeof(*p) is only marked known by SymbolDatabase::setArrayDimensionsUsingValueFlow(), and that runs after all the ValueFlow passes. So everything inside ValueFlow sees the dimension as unknown, and none of these get a value:

  • sizeof(arr)
  • sizeof(arr) / sizeof(arr[0])
  • sizeof(struct T) where T has an array like that as a member

valueFlowNumber() now sets the dimension when the token walk gets to the end of the array declaration. Constant folding has already given the dimension token a known value by then, and declarations come before uses, so the existing sizeof code works unchanged. Chained ones like char b[sizeof(a)] work too. The pass after ValueFlow stays for dimensions that only become known later.

Array parameters are the other half. sizeof of an array function parameter is the size of a pointer, but it was being evaluated as the size of the array. void f(char p[10]) gave 10 on main already, and knowing more dimensions would have spread that. It gives the pointer size now, and sizeof(p) / sizeof(p[0]) isn't evaluated as the element count. The condition is in Variable::isDecayedArrayArgument(), which CheckSizeof had inline. References to arrays and std::array by value keep their full size.

Tests are in TestValueFlow::valueFlowSizeof, plus a new TestBufferOverrun::array_index_78 for the a[sizeof(a)] from the ticket. Both fail without the lib/ change. testrunner passes on macOS arm64 and runformat makes no changes. I haven't run the selfcheck locally.

The const_cast on Dimension in valueflow.cpp is the same one setArrayDimensionsUsingValueFlow() already does. Change it as you see fit.

…ray 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.
Copilot AI balanced review requested due to automatic review settings November 16, 2026 23:25

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.

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.

2 participants