Skip to content

Commit b7092f7

Browse files
committed
report the overflow of a nested record instead of discarding every unsuccessful member sum
1 parent c28b4ad commit b7092f7

4 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎lib/symboldatabase.cpp‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8795,7 +8795,7 @@ static size_t getAlignOf(const ValueType& vt, const Settings& settings, ValueTyp
87958795
return 0;
87968796
}
87978797

8798-
size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf sizeOf, int maxRecursion) const
8798+
size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf sizeOf, int maxRecursion, bool* sizeOverflow) const
87998799
{
88008800
if (maxRecursion > settings.vfOptions.maxSizeOfRecursion) {
88018801
// TODO: add bailout message
@@ -8832,10 +8832,15 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
88328832
bool overflow = false;
88338833
auto accHelper = [&](size_t total, const ValueType& vt2, size_t dim, MathLib::bigint nBits) -> size_t {
88348834
const size_t charBit = settings.platform.char_bit;
8835-
size_t n = vt2.getSizeOf(settings, accuracy, SizeOf::Pointer, ++maxRecursion);
8835+
bool memberOverflow = false;
8836+
size_t n = vt2.getSizeOf(settings, accuracy, SizeOf::Pointer, ++maxRecursion, &memberOverflow);
88368837
size_t a = getAlignOf(vt2, settings, accuracy, SizeOf::Pointer);
8837-
if (n == 0 || a == 0)
8838+
if (n == 0 || a == 0) {
8839+
// the member is a record whose own size does not fit, that is an overflow and not an unknown size
8840+
if (memberOverflow)
8841+
overflow = true;
88388842
return accuracy == Accuracy::ExactOrZero ? 0 : total;
8843+
}
88398844
if (nBits >= 0 && n > maxSize / charBit) {
88408845
overflow = true;
88418846
return accuracy == Accuracy::ExactOrZero ? 0 : total;
@@ -8908,8 +8913,9 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
89088913
return v;
89098914
});
89108915
}
8911-
// the member sizes are not known exactly, the base class sizes must not be returned alone
8912-
if (accuracy == Accuracy::ExactOrZero && (overflow || !result.success))
8916+
if (overflow && sizeOverflow)
8917+
*sizeOverflow = true;
8918+
if (accuracy == Accuracy::ExactOrZero && (overflow || (total == 0 && !result.success)))
89138919
return 0;
89148920
total = std::max(size_t{1}, total);
89158921
size_t align = getAlignOf(*this, settings, accuracy, sizeOf);

‎lib/symboldatabase.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,8 @@ class CPPCHECKLIB ValueType {
13461346
Pointer,
13471347
Pointee,
13481348
};
1349-
size_t getSizeOf(const Settings& settings, Accuracy accuracy, SizeOf sizeOf, int maxRecursion = 0) const;
1349+
/** @param sizeOverflow if given, set to true when the size of a record does not fit in a MathLib::bigint */
1350+
size_t getSizeOf(const Settings& settings, Accuracy accuracy, SizeOf sizeOf, int maxRecursion = 0, bool* sizeOverflow = nullptr) const;
13501351

13511352
/// Check if type is the same ignoring const and references
13521353
bool isTypeEqual(const ValueType* that) const;

‎test/testbufferoverrun.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5098,6 +5098,23 @@ class TestBufferOverrun : public TestFixture {
50985098
"}\n", settings0);
50995099
ASSERT_EQUALS("", errout_str());
51005100

5101+
// a zero-length or flexible array member is not an overflow, the derived class size is still used
5102+
check("struct B { int i; int j; };\n"
5103+
"struct D : B { char d[]; };\n"
5104+
"void f() {\n"
5105+
" D d;\n"
5106+
" memset(&d, 0, 100);\n"
5107+
"}\n", settings0);
5108+
ASSERT_EQUALS("[test.cpp:5:12]: (error) Buffer is accessed out of bounds: &d [bufferAccessOutOfBounds]\n", errout_str());
5109+
5110+
check("struct B { int i; int j; };\n"
5111+
"struct D : B { char d[0]; int k; };\n"
5112+
"void f() {\n"
5113+
" D d;\n"
5114+
" memset(&d, 0, 100);\n"
5115+
"}\n", settings0);
5116+
ASSERT_EQUALS("[test.cpp:5:12]: (error) Buffer is accessed out of bounds: &d [bufferAccessOutOfBounds]\n", errout_str());
5117+
51015118
// an array after a bitfield must not be counted as part of the bitfield
51025119
check("struct S { int a : 8; char c[0x2000000000000000]; };\n"
51035120
"void f() {\n"

‎test/testvalueflow.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2049,6 +2049,15 @@ class TestValueFlow : public TestFixture {
20492049
ASSERT_EQUALS(1U, values.size());
20502050
ASSERT(values.back().isImpossible());
20512051

2052+
// a zero-length array member is not an overflow, the derived class size stays known
2053+
code = "struct B { int i; int j; };\n"
2054+
"struct D : B { char d[0]; int k; };\n"
2055+
"x = sizeof(D);\n";
2056+
values = tokenValues(code, "( D");
2057+
ASSERT_EQUALS(1U, values.size());
2058+
ASSERT_EQUALS(2 * settings.platform.sizeof_int, values.back().intvalue);
2059+
ASSERT_EQUALS_ENUM(ValueFlow::Value::ValueKind::Known, values.back().valueKind);
2060+
20522061
// the number of bits of an array after a bitfield does not fit in a size_t,
20532062
// the array must not be counted as part of the bitfield
20542063
code = "struct S { int a : 8; char c[0x2000000000000000]; };\n"

0 commit comments

Comments
 (0)