Skip to content

Commit b0d514f

Browse files
Nussu06metsw24-max
authored andcommitted
avoid size_t wrap of the bit count after a bitfield and in sizeof of an array variable
1 parent 1621b9e commit b0d514f

4 files changed

Lines changed: 54 additions & 6 deletions

File tree

‎lib/symboldatabase.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8836,6 +8836,10 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
88368836
size_t a = getAlignOf(vt2, settings, accuracy, SizeOf::Pointer);
88378837
if (n == 0 || a == 0)
88388838
return accuracy == Accuracy::ExactOrZero ? 0 : total;
8839+
if (nBits >= 0 && n > maxSize / charBit) {
8840+
overflow = true;
8841+
return accuracy == Accuracy::ExactOrZero ? 0 : total;
8842+
}
88398843
if (nBits == 0) {
88408844
if (currentBitfieldAlloc == 0) {
88418845
nBits = n * charBit;
@@ -8871,7 +8875,9 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
88718875
return accuracy == Accuracy::ExactOrZero ? 0 : total;
88728876
}
88738877
if (currentBitCount > 0) {
8874-
bool fitsInBitfield = currentBitCount + (n * charBit) <= currentBitfieldAlloc * charBit;
8878+
// n * charBit can wrap, so compare against the remaining bits divided by charBit
8879+
const size_t allocBits = currentBitfieldAlloc * charBit;
8880+
bool fitsInBitfield = currentBitCount <= allocBits && n <= (allocBits - currentBitCount) / charBit;
88758881
bool isAligned = currentBitCount % (charBit * a) == 0;
88768882
if (vt2.isIntegral() && fitsInBitfield && isAligned) {
88778883
currentBitCount += charBit * n;

‎lib/vf_common.cpp‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,15 @@ namespace ValueFlow
265265
size = getSizeOfType(var->typeStartToken(), settings);
266266
}
267267
// find the number of elements
268-
size_t count = 1;
268+
MathLib::bigint count = 1;
269269
for (size_t i = 0; !isPointer && i < var->dimensions().size(); ++i) {
270-
if (var->dimensionKnown(i))
271-
count *= var->dimension(i);
272-
else
270+
if (!var->dimensionKnown(i) || MathLib::mulOverflows(count, var->dimension(i))) {
273271
count = 0;
272+
break;
273+
}
274+
count *= var->dimension(i);
274275
}
275-
if (size && count > 0) {
276+
if (size && count > 0 && !MathLib::mulOverflows(count, static_cast<MathLib::bigint>(size))) {
276277
Value value(count * size);
277278
if (settings.platform.type != Platform::Type::Unspecified)
278279
value.setKnown();

‎test/testbufferoverrun.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5088,6 +5088,22 @@ class TestBufferOverrun : public TestFixture {
50885088
" return buf[sizeof(D)];\n"
50895089
"}\n", settings0);
50905090
ASSERT_EQUALS("", errout_str());
5091+
5092+
// an array after a bitfield must not be counted as part of the bitfield
5093+
check("struct S { int a : 8; char c[0x2000000000000000]; };\n"
5094+
"void f() {\n"
5095+
" S s;\n"
5096+
" memset(&s, 0, 10);\n"
5097+
"}\n", settings0);
5098+
ASSERT_EQUALS("", errout_str());
5099+
5100+
// sizeof of an array variable must not wrap around
5101+
check("int f() {\n"
5102+
" int a[0x4000000000000001];\n"
5103+
" char buf[2] = {0};\n"
5104+
" return buf[sizeof(a)];\n"
5105+
"}\n", settings0);
5106+
ASSERT_EQUALS("", errout_str());
50915107
}
50925108

50935109
// extracttests.enable

‎test/testvalueflow.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2031,6 +2031,31 @@ class TestValueFlow : public TestFixture {
20312031
values = tokenValues(code, "( D");
20322032
ASSERT_EQUALS(1U, values.size());
20332033
ASSERT(values.back().isImpossible());
2034+
2035+
// the number of bits of an array after a bitfield does not fit in a size_t,
2036+
// the array must not be counted as part of the bitfield
2037+
code = "struct S { int a : 8; char c[0x2000000000000000]; };\n"
2038+
"x = sizeof(S);\n";
2039+
values = tokenValues(code, "( S");
2040+
ASSERT_EQUALS(1U, values.size());
2041+
ASSERT_EQUALS(0x2000000000000004LL, values.back().intvalue);
2042+
2043+
// the size in bytes of an array variable does not fit in a MathLib::bigint
2044+
code = "void f() {\n"
2045+
" int a[0x4000000000000001];\n"
2046+
" x = sizeof(a);\n"
2047+
"}\n";
2048+
values = tokenValues(code, "( a");
2049+
ASSERT_EQUALS(1U, values.size());
2050+
ASSERT(values.back().isImpossible());
2051+
2052+
code = "void f() {\n"
2053+
" char a[0x100000000][0x100000000];\n"
2054+
" x = sizeof(a);\n"
2055+
"}\n";
2056+
values = tokenValues(code, "( a");
2057+
ASSERT_EQUALS(1U, values.size());
2058+
ASSERT(values.back().isImpossible());
20342059
}
20352060

20362061
void valueFlowComma()

0 commit comments

Comments
 (0)