Repository navigation
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
Conversation
…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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)or10 + sizeof(*p)is only marked known bySymbolDatabase::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)whereThas an array like that as a membervalueFlowNumber()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 existingsizeofcode works unchanged. Chained ones likechar b[sizeof(a)]work too. The pass after ValueFlow stays for dimensions that only become known later.Array parameters are the other half.
sizeofof 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, andsizeof(p) / sizeof(p[0])isn't evaluated as the element count. The condition is inVariable::isDecayedArrayArgument(), whichCheckSizeofhad inline. References to arrays andstd::arrayby value keep their full size.Tests are in
TestValueFlow::valueFlowSizeof, plus a newTestBufferOverrun::array_index_78for thea[sizeof(a)]from the ticket. Both fail without thelib/change. testrunner passes on macOS arm64 andrunformatmakes no changes. I haven't run the selfcheck locally.The
const_castonDimensioninvalueflow.cppis the same onesetArrayDimensionsUsingValueFlow()already does. Change it as you see fit.