Skip to content

Commit dd0ba8d

Browse files
committed
avoid size_t wrap of the bit count after a bitfield and in sizeof of an array variable
1 parent f84e133 commit dd0ba8d

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
@@ -8819,6 +8819,10 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
88198819
size_t a = getAlignOf(vt2, settings, accuracy, SizeOf::Pointer);
88208820
if (n == 0 || a == 0)
88218821
return accuracy == Accuracy::ExactOrZero ? 0 : total;
8822+
if (nBits >= 0 && n > maxSize / charBit) {
8823+
overflow = true;
8824+
return accuracy == Accuracy::ExactOrZero ? 0 : total;
8825+
}
88228826
if (nBits == 0) {
88238827
if (currentBitfieldAlloc == 0) {
88248828
nBits = n * charBit;
@@ -8854,7 +8858,9 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
88548858
return accuracy == Accuracy::ExactOrZero ? 0 : total;
88558859
}
88568860
if (currentBitCount > 0) {
8857-
bool fitsInBitfield = currentBitCount + (n * charBit) <= currentBitfieldAlloc * charBit;
8861+
// n * charBit can wrap, so compare against the remaining bits divided by charBit
8862+
const size_t allocBits = currentBitfieldAlloc * charBit;
8863+
bool fitsInBitfield = currentBitCount <= allocBits && n <= (allocBits - currentBitCount) / charBit;
88588864
bool isAligned = currentBitCount % (charBit * a) == 0;
88598865
if (vt2.isIntegral() && fitsInBitfield && isAligned) {
88608866
currentBitCount += charBit * n;

‎lib/vf_common.cpp‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,15 @@ namespace ValueFlow
261261
size = getSizeOfType(var->typeStartToken(), settings);
262262
}
263263
// find the number of elements
264-
size_t count = 1;
264+
MathLib::bigint count = 1;
265265
for (size_t i = 0; i < var->dimensions().size(); ++i) {
266-
if (var->dimensionKnown(i))
267-
count *= var->dimension(i);
268-
else
266+
if (!var->dimensionKnown(i) || MathLib::mulOverflows(count, var->dimension(i))) {
269267
count = 0;
268+
break;
269+
}
270+
count *= var->dimension(i);
270271
}
271-
if (size && count > 0) {
272+
if (size && count > 0 && !MathLib::mulOverflows(count, static_cast<MathLib::bigint>(size))) {
272273
Value value(count * size);
273274
if (settings.platform.type != Platform::Type::Unspecified)
274275
value.setKnown();

‎test/testbufferoverrun.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5034,6 +5034,22 @@ class TestBufferOverrun : public TestFixture {
50345034
" return buf[sizeof(D)];\n"
50355035
"}\n", settings0);
50365036
ASSERT_EQUALS("", errout_str());
5037+
5038+
// an array after a bitfield must not be counted as part of the bitfield
5039+
check("struct S { int a : 8; char c[0x2000000000000000]; };\n"
5040+
"void f() {\n"
5041+
" S s;\n"
5042+
" memset(&s, 0, 10);\n"
5043+
"}\n", settings0);
5044+
ASSERT_EQUALS("", errout_str());
5045+
5046+
// sizeof of an array variable must not wrap around
5047+
check("int f() {\n"
5048+
" int a[0x4000000000000001];\n"
5049+
" char buf[2] = {0};\n"
5050+
" return buf[sizeof(a)];\n"
5051+
"}\n", settings0);
5052+
ASSERT_EQUALS("", errout_str());
50375053
}
50385054

50395055
// extracttests.enable

‎test/testvalueflow.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1916,6 +1916,31 @@ class TestValueFlow : public TestFixture {
19161916
values = tokenValues(code, "( D");
19171917
ASSERT_EQUALS(1U, values.size());
19181918
ASSERT(values.back().isImpossible());
1919+
1920+
// the number of bits of an array after a bitfield does not fit in a size_t,
1921+
// the array must not be counted as part of the bitfield
1922+
code = "struct S { int a : 8; char c[0x2000000000000000]; };\n"
1923+
"x = sizeof(S);\n";
1924+
values = tokenValues(code, "( S");
1925+
ASSERT_EQUALS(1U, values.size());
1926+
ASSERT_EQUALS(0x2000000000000004LL, values.back().intvalue);
1927+
1928+
// the size in bytes of an array variable does not fit in a MathLib::bigint
1929+
code = "void f() {\n"
1930+
" int a[0x4000000000000001];\n"
1931+
" x = sizeof(a);\n"
1932+
"}\n";
1933+
values = tokenValues(code, "( a");
1934+
ASSERT_EQUALS(1U, values.size());
1935+
ASSERT(values.back().isImpossible());
1936+
1937+
code = "void f() {\n"
1938+
" char a[0x100000000][0x100000000];\n"
1939+
" x = sizeof(a);\n"
1940+
"}\n";
1941+
values = tokenValues(code, "( a");
1942+
ASSERT_EQUALS(1U, values.size());
1943+
ASSERT(values.back().isImpossible());
19191944
}
19201945

19211946
void valueFlowComma()

0 commit comments

Comments
 (0)