Repository navigation
Conversation
|
There should probably be a trac ticket for this PR. Please create it, or ask us to create it. Then the PR title should be updated. |
|
hey can anyone create trac ticket for this pr, thanks. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The PR removes the signed overflow when the dimensions are multiplied together. It does not fix the wrong results it describes, because the element count is then multiplied by the element size without a check. When the count fits but the byte size does not, it still wraps, and the false positives remain. Example: int a[0x4000000000000001]; memset(a, 0, 10); still reports bufferAccessOutOfBounds. sizeof of struct U { int m[0x4000000000000001]; } is still folded to 4. The PR's own example uses char a[0x4000000000000001][4], which the new check catches only because the element size is 1. Use int a[0x4000000000000001][1] and the bug comes back. The byte-size multiplications are in getBufferSize ((dim - index) * typeSize), n *= dim in the accHelper of getSizeOf, and mul * getSizeOf(...) in clangimport. These are size_t operations, so they are not UB, but the sizes they give are wrong. The new overflow check also duplicates the existing mulOverflows() in checkbufferoverrun.cpp. Apart from this I found no problems; the new overflow check itself looks correct.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/; both built without errors. ./testrunner TestBufferOverrun TestValueFlow TestClangImport TestSymbolDatabase: 825 tests, 0 failed.
Test file a.c:
void f1() { int a[0x4000000000000001]; memset(a, 0, 10); }
void f2() { char a[0x2000000000000001][2]; memset(a, 0, 10); }
void f3() { int a[0x4000000000000001][1]; memset(a, 0, 10); }
struct S { int m[0x4000000000000001]; };
int f4() { return sizeof(struct S); }
struct T { char m[0x4000000000000001][4]; };
int f5() { char b[2]; return b[sizeof(struct T)]; }
struct U { int m[0x4000000000000001]; };
int f6() { char b[2]; return b[sizeof(struct U)]; }Command: cppcheck -q --enable=all a.c
| case | base | PR |
|---|---|---|
| f5 (the case from the PR) | arrayIndexOutOfBounds, index 4 | fixed |
| f1 | a.c:1:47: error: Buffer is accessed out of bounds: a [bufferAccessOutOfBounds] |
still reported |
| f3 | a.c:3:50: error: Buffer is accessed out of bounds: a [bufferAccessOutOfBounds] |
still reported |
| f6 | a.c:9:31: error: Array 'b[2]' accessed at index 4, which is out of bounds. [arrayIndexOutOfBounds] |
still reported |
In f1, f3 and f6 the element count fits in a bigint, but the byte size wraps when it is multiplied by sizeof(int). I did not run a sanitizer build.
Automated review of e208afc by Claude. The pull request was built and tested locally.
| return i1 * dim.num; | ||
| }); | ||
| MathLib::bigint dim = 0; | ||
| if (!var->dimensionsProduct(dim)) |
There was a problem hiding this comment.
AI review
This catches overflow only in the element count. Below, (dim - index) * typeSize can still wrap, because it is size_t arithmetic. void f() { int a[0x4000000000000001]; memset(a, 0, 10); } still gives a false positive bufferAccessOutOfBounds, and so does int a[0x4000000000000001][1]. The byte size needs the same check. The existing mulOverflows() a few lines below in this file could be used for both, instead of adding a second implementation in Variable::dimensionsProduct.
| return i1 * dim.num; | ||
| }); | ||
| MathLib::bigint dim = 0; | ||
| if (!var.dimensionsProduct(dim)) |
There was a problem hiding this comment.
AI review
accHelper later does n *= dim with the element size, in size_t, and that can still wrap. With struct U { int m[0x4000000000000001]; };, sizeof(struct U) is still folded to 4, so char b[2]; b[sizeof(struct U)] reports arrayIndexOutOfBounds at index 4. The test only covers char elements, where the element size is 1.
| typeSize = 0; | ||
| break; | ||
| } | ||
| if (var.valueType()) |
There was a problem hiding this comment.
AI review
The same applies here: mul * getSizeOf(...) and the += into typeSize are not checked, so the struct size can still wrap when the element size is greater than 1.
|
Test results for commit e208afc (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-51-20-36-10.eu-north-1.compute.amazonaws.com/pr-9027/ Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong. |
|
Good point, the element count was only half of it. The byte size is now checked as well: getBufferSize checks Still need a trac ticket for this one so I can fix up the title, could one of you open it? |
|
Test results for commit 1906bfa (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-51-20-36-10.eu-north-1.compute.amazonaws.com/pr-9027/ Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The overflow handling in ValueType::getSizeOf is still wrong for classes with base classes. When a member's size overflows, accHelper now returns 0. accumulateStructMembers then returns {0, false}. But the derivedFrom accumulate still adds the base class sizes to that 0, and the total == 0 && !result.success check no longer fires. So sizeof(Derived) is folded to just the size of the base class, plus padding. Example: struct Base { int i; }; struct Derived : Base { char b[0x6000000000000000]; char c[0x6000000000000000]; };. Here the PR folds sizeof(Derived) to a small constant and reports a new arrayIndexOutOfBounds that base does not report. The cases with one base class that the PR set out to fix (int m[0x4000000000000001], char m[0x100000000][0x100000000]) still give small wrong sizes and false positives, including bufferAccessOutOfBounds in a memset. The simple cases without base classes, the clangimport change and the shared MathLib::mulOverflows look correct to me.
Earlier comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | PR needs a trac ticket and the title should reference it | @dmcppcheck | general | |
| 2 | Byte size (count * element size) can still wrap, so false positives remain for int elements | AI review | general | ✅ Fixed |
| 3 | New overflow check duplicates the existing mulOverflows() | AI review | general | ✅ Fixed |
| 4 | getBufferSize: (dim - index) * typeSize is not checked for overflow | AI review | lib/checkbufferoverrun.cpp:635 |
✅ Fixed |
| 5 | accHelper n *= dim can wrap; sizeof(struct U {int m[...]}) is folded to 4 | AI review | lib/symboldatabase.cpp:8714 |
✅ Fixed |
| 6 | clangimport: mul * getSizeOf and += into typeSize are not checked | AI review | lib/clangimport.cpp:1645 |
✅ Fixed |
@Nussu06, please fix, resolve or reply to the open comments: #1.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/; both builds succeeded. ./testrunner TestBufferOverrun TestValueFlow TestClangImport TestSymbolDatabase TestMathLib: 856 tests, 0 failed. I did not build with a sanitizer.
A new false positive (base has no warning):
// d.cpp
struct Base { int i; };
struct Derived : Base { char b[0x6000000000000000]; char c[0x6000000000000000]; };
int f() { char buf[2] = {0}; return buf[sizeof(Derived)]; }cppcheck -q d.cpp
- PR:
d.cpp:3:40: error: Array 'buf[2]' accessed at index 5, which is out of bounds. [arrayIndexOutOfBounds] - base: no warning
Cases with a base class that the PR does not fix:
// d2.cpp
struct Base { int i; };
struct Derived : Base { int m[0x4000000000000001]; };
int f() { char buf[2] = {0}; return buf[sizeof(Derived)]; }
struct Derived2 : Base { char m[0x100000000][0x100000000]; };
int g() { char buf[2] = {0}; return buf[sizeof(Derived2)]; }PR and base both report arrayIndexOutOfBounds. With the PR the indexes are 8 and 4; base gives 8 and 5. --debug-normal shows that the PR folds sizeof(Derived) to always 8 and sizeof(Derived2) to always 4.
// d3.cpp
struct Base { int i; };
struct Derived : Base { char m[0x100000000][0x100000000]; };
void h() { Derived d; memset(&d, 0, 10); }PR and base: d3.cpp:3:30: error: Buffer is accessed out of bounds: &d [bufferAccessOutOfBounds]
Without base classes: struct A { int x; char c[0x7ffffffffffffffb]; } (the tail padding would overflow) and struct D : B (two bases of 0x7000000000000000 bytes) gave no known sizeof value with the PR, as intended.
Automated review of 1906bfa by Claude. The pull request was built and tested locally.
| return accuracy == Accuracy::ExactOrZero ? 0 : total; | ||
| n *= dim; | ||
| size_t padding = (a - (total % a)) % a; | ||
| if (total + padding > maxSize || n > maxSize - total - padding) |
There was a problem hiding this comment.
AI review
When this check (or the mulOverflows check above) fires, accHelper returns 0. accumulateStructMembers then gives {0, false}, but further down in getSizeOf the derivedFrom accumulate still adds the sizes of the base classes to that 0. After that, total == 0 && !result.success is false, so the overflow is lost. sizeof is folded to the size of the base class.
struct Base { int i; };
struct Derived : Base { char b[0x6000000000000000]; char c[0x6000000000000000]; };
int f() { char buf[2] = {0}; return buf[sizeof(Derived)]; }The PR reports arrayIndexOutOfBounds at index 5; base reports nothing. struct Derived : Base { int m[0x4000000000000001]; } and struct Derived2 : Base { char m[0x100000000][0x100000000]; } still fold sizeof to 8 and 4. A memset(&d, 0, 10) on such an object still reports bufferAccessOutOfBounds. When result.success is false because of an overflow, getSizeOf should return 0 for ExactOrZero before it adds the base classes. The base-class sums (v += ...) should also be checked for overflow.
There was a problem hiding this comment.
Fixed in f84e133. accumulateStructMembers now reports a dimension overflow in its result, and accHelper sets a flag when the byte size overflows, so getSizeOf returns 0 for ExactOrZero after the base classes are added as well. The base class sums are now checked against the bigint max too. d.cpp, d2.cpp and d3.cpp no longer warn, and a struct with bitfields like struct BF { int a : 8; char c; } still folds to 4. I added those cases, plus one where two bases together overflow, to valueFlowSizeof and arraysize_overflow. The full testrunner passes.
|
The base class case is addressed now (see the reply on the inline comment). The trac ticket is the only open item left. I don't have Trac access, so could one of you create one for this? I'll update the title to reference it once it exists. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The fixes for the earlier comments hold up. The derived-class, two-base and int-element cases now give no known sizeof and no false positives.
One overflowing size still gets through ValueType::getSizeOf, the function this PR hardens. When a member array follows a bitfield, fitsInBitfield computes n * charBit in size_t after the new checks have run. That product can wrap. For struct BF { int a : 8; char c[0x2000000000000000]; }; the array is treated as fitting into the bitfield. sizeof(BF) is folded to 4, which gives a false arrayIndexOutOfBounds and a false bufferAccessOutOfBounds on memset(&b, 0, 10). Base reports the same.
Outside the diff, sizeof(a) of an array variable has the same wrap in lib/vf_common.cpp:267-271 (count *= var->dimension(i), count * size). For int a[0x4000000000000001], sizeof(a) is still folded to 4. Changing that site to use dimensionsProduct/mulOverflows would make the fix complete.
The trac ticket (C1) is still not linked.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | PR needs a trac ticket and the title should reference it | @dmcppcheck | general | |
| 2 | Byte size (count * element size) can still wrap, so false positives remain for int elements | AI review | general | ✅ Fixed |
| 3 | New overflow check duplicates the existing mulOverflows() | AI review | general | ✅ Fixed |
| 4 | getBufferSize: (dim - index) * typeSize is not checked for overflow | AI review | lib/checkbufferoverrun.cpp:635 |
✅ Fixed |
| 5 | accHelper n *= dim can wrap; sizeof(struct U {int m[...]}) folded to 4 | AI review | lib/symboldatabase.cpp:8715 |
✅ Fixed |
| 6 | clangimport: mul * getSizeOf and += into typeSize are not checked | AI review | lib/clangimport.cpp:1645 |
✅ Fixed |
| 7 | Overflow lost when base class sizes are added in getSizeOf | AI review | general | ✅ Fixed |
| 8 | n * charBit can still wrap, so sizeof of an array after a bitfield is folded to a small value |
AI review | lib/symboldatabase.cpp:8857 |
🆕 New |
@Nussu06, please fix, resolve or reply to the open and new comments: #1, #8.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/; both built without errors. I did not do a sanitizer build.
./testrunner TestBufferOverrun TestValueFlow TestClangImport TestSymbolDatabase TestMathLib: 856 tests, 0 failed.
Test file scratch/t1.cpp (excerpt):
struct BF { int a : 8; char c[0x2000000000000000]; };
int f1() { char buf[2] = {0}; return buf[sizeof(BF)]; }
void f2() { BF b; memset(&b, 0, 10); }
int f3() { int a[0x4000000000000001]; char buf[2] = {0}; return buf[sizeof(a)]; }
union U { int i; char c[0x100000000][0x100000000]; };
int f6() { char buf[2] = {0}; return buf[sizeof(U)]; }Command: ./cppcheck -q ../scratch/t1.cpp
Output of the PR build:
t1.cpp:2:41: error: Array 'buf[2]' accessed at index 4, which is out of bounds. [arrayIndexOutOfBounds]
t1.cpp:4:68: error: Array 'buf[2]' accessed at index 4, which is out of bounds. [arrayIndexOutOfBounds]
t1.cpp:3:26: error: Buffer is accessed out of bounds: &b [bufferAccessOutOfBounds]
- f1 and f2 (bitfield path in
getSizeOf): false positives in both PR and base.n * charBit= 0x2000000000000000 * 8 wraps to 0, so the array is counted inside the bitfield andsizeof(BF)becomes 4. - f3 (
sizeofof an array variable,vf_common.cpp): false positive in both PR and base. This code is not touched by the PR. - f6 (union): base reports index 4 and the PR reports nothing. The PR fixes this case.
Other cases I tried behaved as expected in both builds:
struct A { int x; char c[0x7ffffffffffffffb]; }(tail padding would overflow): no warning.- A nested struct member with an overflowing array: no warning.
- An array of 0x4000000000000001 empty structs: true positive, index 4611686018427387905.
Automated review of f84e133 by Claude. The pull request was built and tested locally.
| return accuracy == Accuracy::ExactOrZero ? 0 : total; | ||
| } | ||
| if (currentBitCount > 0) { | ||
| bool fitsInBitfield = currentBitCount + (n * charBit) <= currentBitfieldAlloc * charBit; |
There was a problem hiding this comment.
AI review
The new checks above bound n by the bigint max, but n * charBit here can still wrap in size_t. Example: struct BF { int a : 8; char c[0x2000000000000000]; };. Here n = 2^61, and n * 8 wraps to 0. So fitsInBitfield is true, the array is treated as part of the bitfield, and sizeof(BF) is folded to 4.
struct BF { int a : 8; char c[0x2000000000000000]; };
int f1() { char buf[2] = {0}; return buf[sizeof(BF)]; }
void f2() { BF b; memset(&b, 0, 10); }With the PR build, cppcheck -q t.cpp gives:
t.cpp:2:41: error: Array 'buf[2]' accessed at index 4, which is out of bounds. [arrayIndexOutOfBounds]
t.cpp:3:26: error: Buffer is accessed out of bounds: &b [bufferAccessOutOfBounds]
Base gives the same output. A check like MathLib::mulOverflows(n, charBit), or comparing n against (currentBitfieldAlloc * charBit - currentBitCount) / charBit, would avoid the wrap. The sizes of the other members in the bitfield branch (nBits = n * charBit) can wrap in the same way.
There was a problem hiding this comment.
Fixed in dd0ba8d. fitsInBitfield now compares n against (currentBitfieldAlloc * charBit - currentBitCount) / charBit, so the product is never formed. For bitfield members, a member size above maxSize / charBit is treated as an overflow before nBits = n * charBit. With that, sizeof(BF) folds to 0x2000000000000004, the memset no longer warns, and buf[sizeof(BF)] is reported at that index, which is the real size.
|
Test results for commit f84e133 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-51-20-36-10.eu-north-1.compute.amazonaws.com/pr-9027/ Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong. |
|
I also changed the The trac ticket is still the one open item. I can't create one myself, so could one of you open it? I'll put it in the title once it exists. |
dd0ba8d to
b0d514f
Compare
|
Rebased onto main to clear the conflict. The only overlap was the Rechecked the earlier cases on top of current main: f1, f3, f6, the three derived-class cases, the bitfield struct and The red The trac ticket is still the one open item. I don't have Trac access, so could one of you create one? I'll update the title to reference it once it exists. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The overflow handling in ValueType::getSizeOf still misses one case, and it gives false positives. A member can be a struct whose own size overflows. getSizeOf returns 0 for that member, and accHelper takes the n == 0 early return without setting overflow. In a class with a base class, the base sizes are then added to the 0, so sizeof is folded to the size of the base. For example, struct Inner { char m[0x100000000][0x100000000]; }; struct D : B { Inner in; }; still gives arrayIndexOutOfBounds and bufferAccessOutOfBounds. Base reports the same, so this is not a regression, but it is the derived-class problem from C11 again, reached through a nested struct. Apart from that I found no new problems. The trac ticket (C1) is still open.
Comments
10 comments, 1 open, 1 new
@Nussu06, please fix, resolve or reply to the open and new comments: #1, #10.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/ without errors. I did not do a sanitizer build.
./testrunner TestBufferOverrun TestValueFlow TestClangImport TestSymbolDatabase TestMathLib: 859 tests, 0 failed.
Remaining false positive (same in base):
// t1.cpp
struct B { int i; };
struct Inner { char m[0x100000000][0x100000000]; };
struct D : B { Inner in; };
int f1() { char buf[2] = {0}; return buf[sizeof(D)]; }
void f2() { D d; memset(&d, 0, 10); }cppcheck -q t1.cpp, PR:
t1.cpp:4:41: error: Array 'buf[2]' accessed at index 4, which is out of bounds. [arrayIndexOutOfBounds]
t1.cpp:5:25: error: Buffer is accessed out of bounds: &d [bufferAccessOutOfBounds]
Base gives index 5 and the same bufferAccessOutOfBounds. --debug-normal shows that the PR folds sizeof(D) to always 4.
// t2.cpp
struct Inner2 { int m[0x4000000000000001]; };
struct B { int i; };
struct D2 : B { Inner2 in; };
int g2() { char buf[2] = {0}; return buf[sizeof(D2)]; }
struct D3 : B { Inner2 in[2]; };
int g3() { char buf[2] = {0}; return buf[sizeof(D3)]; }The PR reports arrayIndexOutOfBounds at index 8 for both functions. Base reports 8 and 16.
Without a base class (struct E { int i; Inner in; }) there is no warning, as intended.
Other cases gave no known sizeof and no warning, as intended:
- An array of 0x7ffffffffffffffe chars followed by a bitfield.
- A struct whose tail padding would overflow.
Automated review of b0d514f by Claude. The pull request was built and tested locally.
| const size_t charBit = settings.platform.char_bit; | ||
| size_t n = vt2.getSizeOf(settings, accuracy, SizeOf::Pointer, ++maxRecursion); | ||
| size_t a = getAlignOf(vt2, settings, accuracy, SizeOf::Pointer); | ||
| if (n == 0 || a == 0) |
There was a problem hiding this comment.
AI review
If vt2 is a record whose own size overflows, vt2.getSizeOf() returns 0 for ExactOrZero. Then this early return gives back 0 without setting overflow. Without a base class, that still ends as 'unknown'. With a base class, the derivedFrom accumulate adds the base sizes to the 0, so sizeof is folded to the size of the base:
struct B { int i; };
struct Inner { char m[0x100000000][0x100000000]; };
struct D : B { Inner in; };
int f1() { char buf[2] = {0}; return buf[sizeof(D)]; }
void f2() { D d; memset(&d, 0, 10); }PR: arrayIndexOutOfBounds at index 4 (sizeof(D) is always 4) and bufferAccessOutOfBounds on the memset. Base gives the same warnings. struct D2 : B { Inner2 in; } with struct Inner2 { int m[0x4000000000000001]; } behaves the same way and gives index 8.
This is the problem from C11 again, reached through a nested struct. A fix would be to set overflow = true here (or to let getSizeOf report the overflow of the nested type), or to return 0 from the derived-class path whenever !result.success.
There was a problem hiding this comment.
Fixed in ae03b14. I took the third option: the ExactOrZero check at the end of getSizeOf now returns 0 whenever result.success is false, not only when the running total is still 0. Since every unsuccessful return from accumulateStructMembers carries a total of 0, the only case that changes is the one you found, where the derivedFrom accumulate lifts the total off 0 and hides it. Setting overflow in the n == 0 || a == 0 branch would have worked too, but n == 0 there also means "member size simply unknown", so the flag name would have stopped matching what it records.
Your t1.cpp and t2.cpp give no arrayIndexOutOfBounds and no bufferAccessOutOfBounds now, and --debug-normal shows no known value for sizeof(D), sizeof(D2) or sizeof(D3). Determinable derived sizes are unchanged: struct B { int i; }; struct Inner { char m[4][4]; }; struct D : B { Inner in; }; still folds to 20, struct F : E { int x; char c; } to 8 and an empty struct E to 1. Added the nested-struct cases to valueFlowSizeof and arraysize_overflow. Full testrunner: 5434 tests, 0 failed.
|
Test results for commit b0d514f (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-51-20-36-10.eu-north-1.compute.amazonaws.com/pr-9027/ Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong. |
|
The nested-struct case (#10) is fixed in ae03b14, details are in the reply on the inline comment. Short version: the The trac ticket (#1) is the only item left and it is the one thing I cannot do myself, since I have no Trac access. Could one of you create a ticket for this? I will update the title to reference it as soon as it exists. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
One new problem: the overflow check in accHelper also runs for unions, so a union whose members are each large but fit gets no known sizeof. The check n > maxSize - total - padding treats the sum of the member sizes as the size, but a union's size is std::max(total, n). For union U { char a[0x6000000000000000]; char b[0x6000000000000000]; }, base folds sizeof(U) to the correct 0x6000000000000000. The PR gives it no known value, so a true bufferAccessOutOfBounds that base reports is lost. This is a false negative on extreme sizes, so the practical impact is small.
All the earlier review findings are fixed in the current code. I re-ran their examples and none of them warns now. The trac ticket (C1) is still missing.
Comments
14 comments, 1 open, 1 new
@Nussu06, please fix, resolve or reply to the open and new comments: #1, #14.
For #14 (false negative), it is enough to create a Trac ticket and link it here. It does not need to be fixed in this PR.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/; both built without errors. I did not do a sanitizer build.
./testrunner TestBufferOverrun TestValueFlow TestClangImport TestSymbolDatabase TestMathLib: 859 tests, 0 failed.
Re-check of the earlier findings: the nested-struct case with a base class, the derived class with two large members, the array after a bitfield, sizeof(a) of int a[0x4000000000000001] and the memset on int a[0x4000000000000001] all give no warning with the PR.
Union regression (u2.cpp):
union U { char a[0x6000000000000000]; char b[0x6000000000000000]; };
void g() { U u; memset(&u, 0, 0x7000000000000000); }Command: cppcheck -q u2.cpp
- base:
u2.cpp:2:24: error: Buffer is accessed out of bounds: &u [bufferAccessOutOfBounds] - PR: no output
With return buf[sizeof(U)] (char buf[2]), base reports arrayIndexOutOfBounds at index 6917529027641081856, which is the correct size. The PR reports nothing.
Other cases where the PR and base agree, with the correct sizes:
union V { char a[0x7ffffffffffffff0]; int b; }- A struct that holds one large struct.
struct C { char c; char a[0x7ffffffffffffffe]; }, whose size is exactly the bigint max.- A struct with an anonymous struct member.
Automated review of ae03b14 by Claude. The pull request was built and tested locally.
| } | ||
| n *= dim; | ||
| size_t padding = (a - (total % a)) % a; | ||
| if (total + padding > maxSize || n > maxSize - total - padding) { |
There was a problem hiding this comment.
AI review
This check runs for unions too, but the size of a union is std::max(total, n) (line 8890), not total + padding + n. So a union whose members are each large but fit is flagged as an overflow:
union U { char a[0x6000000000000000]; char b[0x6000000000000000]; };
void g() { U u; memset(&u, 0, 0x7000000000000000); }Base folds sizeof(U) to 0x6000000000000000 and reports bufferAccessOutOfBounds. The PR has no known size and reports nothing.
For eUnion, only n needs to fit, since std::max(total, n) <= maxSize holds whenever n <= maxSize. The sum check could be skipped for unions.
There was a problem hiding this comment.
Fixed in c28b4ad. The padding and the sum check are skipped for eUnion now, so only n has to fit, which is what std::max(total, n) needs. Your u2.cpp folds sizeof(U) back to 0x6000000000000000 and reports the memset, union V { char a[0x7ffffffffffffff0]; int b; } gives its real size again, and a union whose own member overflows (char m[0x100000000][0x100000000]) still has no known size. Added the two sizeof cases to valueFlowSizeof and the memset to arraysize_overflow; both fail without the change. Full testrunner: 5434 tests, 0 failed.
|
Test results for commit ae03b14 (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-51-20-36-10.eu-north-1.compute.amazonaws.com/pr-9027/ Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong. |
|
#14 is fixed in c28b4ad rather than deferred, since that false negative came from this PR. The union path now checks only the largest member, details are in the reply on the inline comment. Rechecked the earlier cases on top of it: the derived-class, nested-struct, bitfield and That leaves the trac ticket (#1) as the only open item, and it's the one thing I can't do myself since I have no Trac access. Could one of you create a ticket for this? I'll update the title to reference it as soon as it exists. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
New false negative from the PR. Line 8912 of lib/symboldatabase.cpp changed the ExactOrZero check at the end of getSizeOf to (overflow || !result.success). This makes getSizeOf return 0 for every derived class where accumulateStructMembers gives up. That includes cases with no overflow at all, for example a zero-length array (char d[0]) or a flexible array (char d[]) declared before any member with a nonzero size. Base folded those sizes and reported true bufferAccessOutOfBounds errors; the PR reports nothing. Base's size for these structs was itself a little too small, so this only matters for GNU-extension code.
Apart from this I found no new problems. All earlier review findings are fixed in the current code. The Trac ticket (C1) is still missing.
Comments
16 comments, 1 open, 1 new
@Nussu06, please fix, resolve or reply to the open and new comments: #1, #16.
For #16 (false negative), it is enough to create a Trac ticket and link it here. It does not need to be fixed in this PR.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/; both built without errors. I did not do a sanitizer build, and I did not test the clangimport path.
./testrunner TestBufferOverrun TestValueFlow TestClangImport TestSymbolDatabase TestMathLib: 859 tests, 0 failed.
False negative that the PR introduces (t4.cpp):
struct B { int i; int j; };
struct D : B { char d[]; };
void g1() { D d; memset(&d, 0, 100); }
struct E : B { char d[0]; int k; };
void g2() { E e; memset(&e, 0, 100); }Command: cppcheck -q t4.cpp
- base:
t4.cpp:3:25: error: Buffer is accessed out of bounds: &d [bufferAccessOutOfBounds]
t4.cpp:5:25: error: Buffer is accessed out of bounds: &e [bufferAccessOutOfBounds]
- PR: no output.
The same happens with struct B { int i; }; struct D : B { char d[0]; }; and return buf[sizeof(D)] on char buf[2]: base reports arrayIndexOutOfBounds, the PR reports nothing.
Other edge cases with no false positive in either build:
- The tail padding pushes the size past the bigint maximum:
struct A { int x; char c[0x7ffffffffffffffb]; }. - A pending bitfield is added after the overflow check:
struct S { int a : 8; char c[0x7ffffffffffffffe]; }.
For both, sizeof gets no known value, and memset(&a, 0, 10) and if (sizeof(S) < 10) give no warnings.
Automated review of c28b4ad by Claude. The pull request was built and tested locally.
| } | ||
| if (accuracy == Accuracy::ExactOrZero && total == 0 && !result.success) | ||
| // the member sizes are not known exactly, the base class sizes must not be returned alone | ||
| if (accuracy == Accuracy::ExactOrZero && (overflow || !result.success)) |
There was a problem hiding this comment.
AI review
!result.success does not mean only that the member sizes are unknown or have overflowed. accumulateStructMembers also stops when the running total is still 0 after a member, and a zero-length or flexible array as the first member is enough for that. Before this change, the base class sizes were still used in that case. Now sizeof of such a derived class gets no known value, so true positives that base reports are lost:
struct B { int i; int j; };
struct D : B { char d[]; };
void g1() { D d; memset(&d, 0, 100); }
struct E : B { char d[0]; int k; };
void g2() { E e; memset(&e, 0, 100); }Base reports bufferAccessOutOfBounds for &d and &e; the PR reports nothing.
To keep those cases while still catching overflows, you could check overflow here and keep the old total == 0 && !result.success condition. Then fix the nested-record case from C21 at its source: when vt2 is a record whose own getSizeOf overflows, accHelper should report the overflow.
There was a problem hiding this comment.
Fixed in b7092f7, and I took the route you suggested. The ExactOrZero check at the end of getSizeOf is back to overflow || (total == 0 && !result.success), and the nested-record case from C21 is now reported at its source: getSizeOf takes an optional bool* sizeOverflow out-param that it sets when a record's own size does not fit, and accHelper turns that into overflow in the n == 0 || a == 0 branch. So "member size unknown" and "member size overflowed" are no longer the same thing there, which is what made the old condition too weak in the first place.
Your t4.cpp reports both bufferAccessOutOfBounds again, and struct B { int i; }; struct D : B { char d[0]; }; with buf[sizeof(D)] is reported as before. The C21 cases stay clean: no known sizeof for struct D : B { Inner in; } with struct Inner { char m[0x100000000][0x100000000]; }, nor for Inner2 in or Inner2 in[2]. Determinable derived sizes are unchanged, struct D : B { Inner in; } with char m[4][4] still folds to 20.
Added the zero-length and flexible-array cases to arraysize_overflow and valueFlowSizeof; both fail without the change. Full testrunner: 5434 tests, 0 failed.
…successful member sum
|
#16 is fixed in b7092f7 rather than deferred, since that false negative came out of this PR as well. Details are in the reply on the inline comment. Short version: the Rechecked the earlier cases on top of it: the derived-class, nested-struct, bitfield, union and That leaves the trac ticket (#1) as the only open item, and it's the one thing I can't do myself since I have no Trac access. Could one of you create a ticket for this? I'll update the title to reference it as soon as it exists. |
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
One remaining false positive (base reports it too): the CTU path works out the same buffer size as getBufferSize but does not check for overflow. lib/ctu.cpp:372 computes argtok->variable()->dimension(0) * typeSize in size_t. So int a[0x4000000000000001]; g(a); still gives a buffer size of 4 and a false ctuArrayIndex. That line is outside the diff, so I attached the comment to the check in getBufferSize. Using MathLib::mulOverflows there too would close the gap.
Apart from this I found no new problems:
- All the earlier review findings are fixed in the current code.
- A build with
-fsanitize=signed-integer-overflowof the changed modules showed no runtime errors on any of my inputs.
The Trac ticket that C1 asks for still does not exist.
Comments
18 comments, 1 open, 1 new
@Nussu06, please fix, resolve or reply to the open and new comments: #1, #18.
Testing
Builds
make -j8 cppcheck testrunnerin pr/ andmake cppcheckin base/: both built without errors.- I also built a cppcheck in which
symboldatabase,checkbufferoverrun,vf_common,clangimport,mathlib,valueflowandctuwere compiled with-fsanitize=signed-integer-overflow -fno-sanitize-recover=all.
Unit tests: ./testrunner TestBufferOverrun TestValueFlow TestClangImport TestSymbolDatabase TestMathLib: 859 tests, 0 failed.
Edge cases
- I ran all of these through both builds and the sanitizer build:
- arrays of structs whose own size overflows
- a typedef'd array that overflows
- an anonymous union inside a struct whose member overflows
char a[0x7fffffffffffffff][1]char a[2][0x4000000000000000]- a derived class just under the bigint max
- CTU calls with a huge array argument
- Results:
- The sanitizer reported no runtime errors.
- The PR removes several false positives that base reports, for example
Inner2 a[2]; memset(a,0,10)andsizeof(T6)with the anonymous union. - Sizes that fit are still folded correctly (
sizeof(D)= 9223372036854775800).
- I could not test clangimport with such inputs: clang itself rejects them ("array is too large").
Remaining false positive (same in base), c.cpp:
void g(int *p) { p[10] = 0; }
void f() { int a[0x4000000000000001]; g(a); }Command: cppcheck -q --enable=warning c.cpp
PR and base give the same output:
c.cpp:1:18: error: Array index out of bounds; 'p' buffer size is 4 and it is accessed at offset 40. [ctuArrayIndex]
Automated review of b7092f7 by Claude. The pull request was built and tested locally.
| typeSize = mSettings.platform.sizeof_pointer; | ||
| else | ||
| typeSize = bufTok->valueType()->getSizeOf(mSettings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee); | ||
| if (MathLib::mulOverflows(dim - index, typeSize)) |
There was a problem hiding this comment.
AI review
The CTU check works out the same buffer size as this function, in lib/ctu.cpp:372:
functionCall.callArgValue.value = typeSize > 0 ? argtok->variable()->dimension(0) * typeSize : -1;That line has no overflow check, so the wrap that this PR removes here still happens there:
void g(int *p) { p[10] = 0; }
void f() { int a[0x4000000000000001]; g(a); }cppcheck -q --enable=warning c.cpp (PR and base):
c.cpp:1:18: error: Array index out of bounds; 'p' buffer size is 4 and it is accessed at offset 40. [ctuArrayIndex]
Using MathLib::mulOverflows(dimension(0), typeSize) there, and giving -1 when it overflows, would make the fix cover CTU too.
CheckBufferOverrunImpl::getBufferSize, accumulateStructMembers (used by ValueType::getSizeOf) and setValues in clangimport multiply the array dimensions of a variable in MathLib::bigint without an overflow check, so a declaration like
char a[0x100000000][0x100000000];in the checked code makes cppcheck run into signed integer overflow (UBSan at checkbufferoverrun.cpp:635 and symboldatabase.cpp:8702). When the product wraps to a small value the results are wrong as well: forchar a[0x4000000000000001][4]; memset(a, 0, 10);cppcheck reports bufferAccessOutOfBounds, andsizeofof a struct holding that array is folded to 4. Added Variable::dimensionsProduct, which reports the overflow, and all three sites now treat an overflowing product as an unknown size; sizes that fit are unchanged. TestBufferOverrun::arraysize_overflow and a new case in TestValueFlow::valueFlowSizeof fail without the change, the full testrunner passes, and TestBufferOverrun, TestValueFlow and TestClangImport also pass in a build with -fsanitize=signed-integer-overflow. I don't have Trac access, could one of you create a ticket for this so I can reference it in the title?