Skip to content

fix signed overflow when multiplying array dimensions - #9027

Open
Nussu06 wants to merge 7 commits into
cppcheck-opensource:mainfrom
Nussu06:array-dimensions-overflow
Open

Nussu06 wants to merge 7 commits into
cppcheck-opensource:mainfrom
Nussu06:array-dimensions-overflow

Conversation

@Nussu06

@Nussu06 Nussu06 commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

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: for char a[0x4000000000000001][4]; memset(a, 0, 10); cppcheck reports bufferAccessOutOfBounds, and sizeof of 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?

@dmcppcheck

Copy link
Copy Markdown
Contributor

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.

@Nussu06

Nussu06 commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

hey can anyone create trac ticket for this pr, thanks.

@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 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))

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

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.

Comment thread lib/symboldatabase.cpp
return i1 * dim.num;
});
MathLib::bigint dim = 0;
if (!var.dimensionsProduct(dim))

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

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.

Comment thread lib/clangimport.cpp Outdated
typeSize = 0;
break;
}
if (var.valueType())

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 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.

@dmcppcheck

dmcppcheck commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

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/
packages: 100
Differing warnings: 0 (+0 -0)
Timing: +0.1% (http://ec2-51-20-36-10.eu-north-1.compute.amazonaws.com/pr-9027/2026-11-14_06-15-12-e208afcd4929_timing.html)

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.

@Nussu06

Nussu06 commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

Good point, the element count was only half of it. The byte size is now checked as well: getBufferSize checks (dim - index) * typeSize, the getSizeOf accHelper checks n *= dim and the running total, and clangimport checks mul * size and the +=. Any of those overflowing is treated as an unknown size. I moved mulOverflows() into MathLib so checkbufferoverrun and Variable::dimensionsProduct share the one implementation. Added the int a[0x4000000000000001] and int a[0x4000000000000001][1] memset cases and a sizeof case with int elements; f1, f3 and f6 from your a.c no longer warn, and a real overrun like int a[4]; memset(a, 0, 20); is still reported.

Still need a trac ticket for this one so I can fix up the title, could one of you open it?

@dmcppcheck

dmcppcheck commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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/
packages: 100
Differing warnings: 0 (+0 -0)
Timing: +0.6% (http://ec2-51-20-36-10.eu-north-1.compute.amazonaws.com/pr-9027/2026-11-15_16-41-39-1906bfaf48e6_timing.html)

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 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 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 ⚠️ Open
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.

Comment thread lib/symboldatabase.cpp Outdated
return accuracy == Accuracy::ExactOrZero ? 0 : total;
n *= dim;
size_t padding = (a - (total % a)) % a;
if (total + padding > maxSize || n > maxSize - total - padding)

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@Nussu06

Nussu06 commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

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 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 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 ⚠️ Open
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 and sizeof(BF) becomes 4.
  • f3 (sizeof of 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.

Comment thread lib/symboldatabase.cpp Outdated
return accuracy == Accuracy::ExactOrZero ? 0 : total;
}
if (currentBitCount > 0) {
bool fitsInBitfield = currentBitCount + (n * charBit) <= currentBitfieldAlloc * charBit;

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 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@dmcppcheck

dmcppcheck commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

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/
packages: 100
Differing warnings: 0 (+0 -0)
Timing: -0.0% (http://ec2-51-20-36-10.eu-north-1.compute.amazonaws.com/pr-9027/2026-11-17_19-23-01-f84e1337a106_timing.html)

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.

@Nussu06

Nussu06 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

I also changed the sizeof(a) path in vf_common.cpp to use mulOverflows for the element count and for count * size, so int a[0x4000000000000001] no longer gets a known sizeof. Tests for both cases are in valueFlowSizeof and arraysize_overflow. Ordinary sizes like int a[4] and struct { int a : 8; char c; } still fold the same as before.

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.

@Nussu06
Nussu06 force-pushed the array-dimensions-overflow branch from dd0ba8d to b0d514f Compare November 29, 2026 18:05
@Nussu06

Nussu06 commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main to clear the conflict. The only overlap was the sizeof block in vf_common.cpp, where main added the isDecayedArrayArgument handling. I kept that condition on the dimension loop and left the overflow checks around it, so sizeof(arr) for a decayed int arr[10] still folds to the pointer size, and int a[10] / int a[3][4] still fold to 40 and 48.

Rechecked the earlier cases on top of current main: f1, f3, f6, the three derived-class cases, the bitfield struct and sizeof(a) for int a[0x4000000000000001] all give no warning now, while a real overrun like int a[4]; memset(a, 0, 20); and char b[2]; b[4] are still reported. Full testrunner: 5434 tests, 0 failed.

The red build_qt (windows-2025, 6.10.0) job is a cmake configure error in cmake/compileroptions.cmake on the runner, not something this change touches.

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 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 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
# Comment By Where Status
1 PR needs a trac ticket and the title should reference it @dmcppcheck general ⚠️ Open
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:639 ✅ Fixed
5 accHelper n *= dim can wrap; sizeof(struct U {int m[...]}) folded to 4 AI review lib/symboldatabase.cpp:8732 ✅ 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 lib/symboldatabase.cpp:8848 ✅ Fixed
8 sizeof(a) of an array variable wraps in vf_common.cpp AI review general ✅ Fixed
9 n * charBit can wrap, so an array after a bitfield is counted in the bitfield AI review lib/symboldatabase.cpp:8857 ✅ Fixed
10 Member whose own size overflows (nested struct) returns 0 without setting overflow; base class size is then added, so sizeof is folded wrong AI review lib/symboldatabase.cpp:8837 🆕 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.

Comment thread lib/symboldatabase.cpp Outdated
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)

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@dmcppcheck

dmcppcheck commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

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/
packages: 1000
Differing warnings: 0 (+0 -0)
Timing: +0.2% (http://ec2-51-20-36-10.eu-north-1.compute.amazonaws.com/pr-9027/2026-11-29_18-03-58-b0d514ff992b_timing.html)

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.

@Nussu06

Nussu06 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

The nested-struct case (#10) is fixed in ae03b14, details are in the reply on the inline comment. Short version: the ExactOrZero check at the end of getSizeOf now returns 0 whenever accumulateStructMembers was unsuccessful, so the base class sizes are never handed back on their own. Your t1.cpp and t2.cpp are clean, determinable derived sizes still fold the same (20, 8, 1 for the cases I checked), and the full testrunner is at 5434 tests, 0 failed.

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 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

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
# Comment By Where Status
1 PR needs a trac ticket and the title should reference it @dmcppcheck general ⚠️ Open
2 Byte size (count * element size) can still wrap, false positives remain AI review general ✅ Fixed
3 New overflow check duplicates existing mulOverflows() AI review general ✅ Fixed
4 getBufferSize: (dim - index) * typeSize not checked for overflow AI review lib/checkbufferoverrun.cpp:639 ✅ Fixed
5 accHelper n *= dim can wrap; sizeof folded to 4 AI review lib/symboldatabase.cpp:8732 ✅ Fixed
6 clangimport: mul * getSizeOf and += 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 Overflow lost when base class sizes are added; base sums unchecked AI review lib/symboldatabase.cpp:8848 ✅ Fixed
9 n * charBit wraps for an array after a bitfield AI review general ✅ Fixed
10 sizeof(a) of an array variable wraps in vf_common.cpp AI review general ✅ Fixed
11 n * charBit in fitsInBitfield / nBits can wrap AI review lib/symboldatabase.cpp:8857 ✅ Fixed
12 Nested struct with overflowing size plus base class folds sizeof to base size AI review general ✅ Fixed
13 Nested record overflow returns 0 without overflow flag; base size returned AI review lib/symboldatabase.cpp:8837 ✅ Fixed
14 Union member sizes are summed in the overflow check, so a valid union sizeof becomes unknown AI review lib/symboldatabase.cpp:8873 🆕 New (false negative)

@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.

Comment thread lib/symboldatabase.cpp Outdated
}
n *= dim;
size_t padding = (a - (total % a)) % a;
if (total + padding > maxSize || n > maxSize - total - padding) {

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@dmcppcheck

Copy link
Copy Markdown
Contributor

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/
packages: 1000
Differing warnings: 0 (+0 -0)
Timing: +0.1% (http://ec2-51-20-36-10.eu-north-1.compute.amazonaws.com/pr-9027/2026-12-01_10-09-33-ae03b1496ba4_timing.html)

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.

@Nussu06

Nussu06 commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

#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 int a[0x4000000000000001] cases are still clean, and real overruns like int a[4]; memset(a, 0, 20); and char b[2]; b[4] are still reported. Full testrunner: 5434 tests, 0 failed.

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 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

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
# Comment By Where Status
1 PR needs a trac ticket and the title should reference it @dmcppcheck general ⚠️ Open
2 Byte size (count * element size) can still wrap, false positives remain AI review general ✅ Fixed
3 New overflow check duplicates existing mulOverflows() AI review general ✅ Fixed
4 getBufferSize: (dim - index) * typeSize not checked for overflow AI review lib/checkbufferoverrun.cpp:639 ✅ Fixed
5 accHelper n *= dim can wrap; sizeof folded to 4 AI review lib/symboldatabase.cpp:8732 ✅ Fixed
6 clangimport: mul * getSizeOf and += 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 Overflow lost when base class sizes are added; base sums unchecked AI review lib/symboldatabase.cpp:8848 ✅ Fixed
9 n * charBit wraps for an array after a bitfield AI review general ✅ Fixed
10 sizeof(a) of an array variable wraps in vf_common.cpp AI review general ✅ Fixed
11 n * charBit in fitsInBitfield / nBits can wrap AI review lib/symboldatabase.cpp:8857 ✅ Fixed
12 Nested struct with overflowing size plus base class folds sizeof to base size AI review general ✅ Fixed
13 Nested record overflow returns 0 without overflow flag; base size returned AI review lib/symboldatabase.cpp:8837 ✅ Fixed
14 Union member sizes are summed in the overflow check, so a valid union sizeof becomes unknown AI review general ✅ Fixed
15 Overflow sum check also runs for unions AI review lib/symboldatabase.cpp:8873 ✅ Fixed
16 Derived class with a zero-length or flexible array member gets no sizeof; true positives that base reports are lost AI review lib/symboldatabase.cpp:8912 🆕 New (false negative)

@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.

Comment thread lib/symboldatabase.cpp Outdated
}
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))

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

!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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@Nussu06

Nussu06 commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

#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 ExactOrZero check at the end of getSizeOf is back to the old total == 0 && !result.success condition, and the nested-record overflow from #13 is reported at its source now, through an optional out-param on getSizeOf. A member with no size of its own and a member whose size overflows are no longer treated the same.

Rechecked the earlier cases on top of it: the derived-class, nested-struct, bitfield, union and int a[0x4000000000000001] cases are still clean, your t4.cpp reports both bufferAccessOutOfBounds again, and real overruns like int a[4]; memset(a, 0, 20); and char b[2]; b[4] are still reported. Full testrunner: 5434 tests, 0 failed.

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 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

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-overflow of 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
# Comment By Where Status
1 PR needs a trac ticket and the title should reference it @dmcppcheck general ⚠️ Open
2 Byte size (count * element size) can still wrap, false positives remain AI review general ✅ Fixed
3 New overflow check duplicates existing mulOverflows() AI review general ✅ Fixed
4 getBufferSize: (dim - index) * typeSize not checked for overflow AI review lib/checkbufferoverrun.cpp:639 ✅ Fixed
5 accHelper n *= dim can wrap; sizeof folded to 4 AI review lib/symboldatabase.cpp:8732 ✅ Fixed
6 clangimport: mul * getSizeOf and += 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 Overflow lost when base class sizes are added; base sums unchecked AI review lib/symboldatabase.cpp:8848 ✅ Fixed
9 n * charBit wraps for an array after a bitfield AI review general ✅ Fixed
10 sizeof(a) of an array variable wraps in vf_common.cpp AI review general ✅ Fixed
11 n * charBit in fitsInBitfield / nBits can wrap AI review lib/symboldatabase.cpp:8857 ✅ Fixed
12 Nested struct with overflowing size plus base class folds sizeof to base size AI review general ✅ Fixed
13 Nested record overflow returns 0 without overflow flag AI review lib/symboldatabase.cpp:8837 ✅ Fixed
14 Union member sizes are summed in the overflow check AI review general ✅ Fixed
15 Overflow sum check also runs for unions AI review lib/symboldatabase.cpp:8873 ✅ Fixed
16 Derived class with zero-length/flexible array member gets no sizeof AI review general ✅ Fixed
17 !result.success check loses sizes of derived classes with flexible arrays AI review lib/symboldatabase.cpp:8912 ✅ Fixed
18 CTU computes the same buffer size in ctu.cpp:372 without the overflow check, so ctuArrayIndex still has a false positive AI review lib/checkbufferoverrun.cpp:647 🆕 New

@Nussu06, please fix, resolve or reply to the open and new comments: #1, #18.

Testing

Builds

  • make -j8 cppcheck testrunner in pr/ and make cppcheck in base/: both built without errors.
  • I also built a cppcheck in which symboldatabase, checkbufferoverrun, vf_common, clangimport, mathlib, valueflow and ctu were 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) and sizeof(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))

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 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.

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