Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 15 additions & 25 deletions lib/checkbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,37 +631,27 @@ ValueFlow::Value CheckBufferOverrunImpl::getBufferSize(const Token *bufTok, cons
if (!var || var->isPointer() || (astIsContainer(bufTok) && var->getTypeName() != "std::array"))
return ValueFlow::Value(-1);

const MathLib::bigint dim = std::accumulate(var->dimensions().cbegin(), var->dimensions().cend(), MathLib::bigint(1), [](MathLib::bigint i1, const Dimension &dim) {
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.

return ValueFlow::Value(-1);

MathLib::bigint typeSize;
if (var->isPointerArray())
typeSize = mSettings.platform.sizeof_pointer;
else
typeSize = bufTok->valueType()->getSizeOf(mSettings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
if (MathLib::mulOverflows(dim - index, typeSize))
return ValueFlow::Value(-1);

ValueFlow::Value v;
v.setKnown();
v.valueType = ValueFlow::Value::ValueType::BUFFER_SIZE;

if (var->isPointerArray())
v.intvalue = (dim - index) * mSettings.platform.sizeof_pointer;
else {
const size_t typeSize = bufTok->valueType()->getSizeOf(mSettings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
v.intvalue = (dim - index) * typeSize;
}
v.intvalue = (dim - index) * typeSize;

return v;
}
//---------------------------------------------------------------------------

// Return true if a * b does not fit in a MathLib::bigint
static bool mulOverflows(MathLib::bigint a, MathLib::bigint b)
{
const MathLib::bigint maxValue = std::numeric_limits<MathLib::bigint>::max();
const MathLib::bigint minValue = std::numeric_limits<MathLib::bigint>::min();
if (a > 0)
return b > 0 ? a > maxValue / b : b < minValue / a;
if (a < 0)
return b > 0 ? a < minValue / b : (b < 0 && a < maxValue / b);
return false;
}

static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::MinSize &minsize, const std::vector<const Token *> &args, ValueFlow::Value& bufferSize, const Settings &settings, const Tokenizer* tokenizer)
{
const Token * const arg = (minsize.arg > 0 && minsize.arg - 1 < args.size()) ? args[minsize.arg - 1] : nullptr;
Expand All @@ -686,7 +676,7 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
break;
MathLib::bigint myMinsize = argVal->intvalue;
const int baseSize = tokenizer->sizeOfType(minsize.baseType);
const bool overflow = baseSize != 0 && mulOverflows(myMinsize, baseSize);
const bool overflow = baseSize != 0 && MathLib::mulOverflows(myMinsize, baseSize);
if (baseSize != 0 && !overflow)
myMinsize *= baseSize;
const bool ok = !overflow && myMinsize <= bufferSize.intvalue;
Expand All @@ -704,7 +694,7 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
break;
case Library::ArgumentChecks::MinSize::Type::MUL:
if (arg && arg2 && arg->hasKnownIntValue() && arg2->hasKnownIntValue()) {
if (mulOverflows(arg->getKnownIntValue(), arg2->getKnownIntValue()))
if (MathLib::mulOverflows(arg->getKnownIntValue(), arg2->getKnownIntValue()))
return false;
return (arg->getKnownIntValue() * arg2->getKnownIntValue()) <= bufferSize.intvalue;
}
Expand All @@ -713,7 +703,7 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
MathLib::bigint myMinsize = minsize.value;
const int baseSize = tokenizer->sizeOfType(minsize.baseType);
if (baseSize != 0) {
if (mulOverflows(myMinsize, baseSize))
if (MathLib::mulOverflows(myMinsize, baseSize))
return false;
myMinsize *= baseSize;
}
Expand Down
20 changes: 14 additions & 6 deletions lib/clangimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <cstring>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
Expand All @@ -43,7 +44,6 @@
#include <string>
#include <utility>
#include <vector>
#include <numeric>

class Settings;

Expand Down Expand Up @@ -1638,11 +1638,19 @@ static void setValues(const Tokenizer &tokenizer, const SymbolDatabase *symbolDa

MathLib::bigint typeSize = 0;
for (const Variable &var: scope.varlist) {
const MathLib::bigint mul = std::accumulate(var.dimensions().cbegin(), var.dimensions().cend(), MathLib::bigint(1), [](MathLib::bigint v, const Dimension& dim) {
return v * dim.num;
});
if (var.valueType())
typeSize += mul * var.valueType()->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer);
MathLib::bigint mul = 0;
if (!var.dimensionsProduct(mul)) {
typeSize = 0;
break;
}
if (var.valueType()) {
const MathLib::bigint size = var.valueType()->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer);
if (MathLib::mulOverflows(mul, size) || mul * size > std::numeric_limits<MathLib::bigint>::max() - typeSize) {
typeSize = 0;
break;
}
typeSize += mul * size;
}
}
scope.definedType->sizeOf = typeSize;
}
Expand Down
11 changes: 11 additions & 0 deletions lib/mathlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,17 @@ bool MathLib::isOctalDigit(char c)
return (c >= '0' && c <= '7');
}

bool MathLib::mulOverflows(bigint a, bigint b)
{
const bigint maxValue = std::numeric_limits<bigint>::max();
const bigint minValue = std::numeric_limits<bigint>::min();
if (a > 0)
return b > 0 ? a > maxValue / b : b < minValue / a;
if (a < 0)
return b > 0 ? a < minValue / b : (b < 0 && a < maxValue / b);
return false;
}

MathLib::value operator+(const MathLib::value &v1, const MathLib::value &v2)
{
return MathLib::value::calc('+',v1,v2);
Expand Down
3 changes: 3 additions & 0 deletions lib/mathlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ class CPPCHECKLIB MathLib {
*/
static bool isOctalDigit(char c);

/** @brief Return true if a * b does not fit in a bigint */
static bool mulOverflows(bigint a, bigint b);

static unsigned int encodeMultiChar(const std::string& str);
};

Expand Down
48 changes: 39 additions & 9 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <numeric>
#include <sstream>
#include <stack>
Expand Down Expand Up @@ -2515,6 +2516,17 @@ bool Variable::isMember() const {
return mScope && mScope->isClassOrStructOrUnion();
}

bool Variable::dimensionsProduct(MathLib::bigint &product) const
{
product = 1;
for (const Dimension &dim : mDimensions) {
if (MathLib::mulOverflows(product, dim.num))
return false;
product *= dim.num;
}
return true;
}

bool Variable::isPointerArray() const
{
if (!isArray())
Expand Down Expand Up @@ -8683,6 +8695,7 @@ namespace {
{
size_t total;
bool success;
bool overflow;
};
}

Expand All @@ -8697,10 +8710,10 @@ static Result accumulateStructMembers(const Scope* scope, F f, ValueType::Accura
const MathLib::bigint bits = var.nameToken() ? var.nameToken()->bits() : -1;
if (const ValueType* vt = var.valueType()) {
if (vt->type == ValueType::Type::RECORD && vt->typeScope == scope)
return {0, false};
const MathLib::bigint dim = std::accumulate(var.dimensions().cbegin(), var.dimensions().cend(), MathLib::bigint(1), [](MathLib::bigint i1, const Dimension& dim) {
return i1 * dim.num;
});
return {0, false, false};
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.

return {0, false, true};
if (var.nameToken()->scope() != scope && var.nameToken()->scope()->definedType) { // anonymous union
const auto ret = anonScopes.insert(var.nameToken()->scope());
if (ret.second)
Expand All @@ -8714,9 +8727,9 @@ static Result accumulateStructMembers(const Scope* scope, F f, ValueType::Accura
total = f(total, *vt, dim, bits);
}
if (accuracy == ValueType::Accuracy::ExactOrZero && total == 0 && bits == -1)
return {0, false};
return {0, false, false};
}
return {total, true};
return {total, true, false};
}


Expand Down Expand Up @@ -8797,6 +8810,9 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
if (type == ValueType::Type::RECORD && typeScope) {
size_t currentBitCount = 0;
size_t currentBitfieldAlloc = 0;
// the size must fit in a MathLib::bigint
const size_t maxSize = std::numeric_limits<MathLib::bigint>::max();
bool overflow = false;
auto accHelper = [&](size_t total, const ValueType& vt2, size_t dim, MathLib::bigint nBits) -> size_t {
const size_t charBit = settings.platform.char_bit;
size_t n = vt2.getSizeOf(settings, accuracy, SizeOf::Pointer, ++maxRecursion);
Expand Down Expand Up @@ -8827,8 +8843,16 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
currentBitCount += nBits;
return ret;
}
if (MathLib::mulOverflows(static_cast<MathLib::bigint>(n), static_cast<MathLib::bigint>(dim))) {
overflow = true;
return accuracy == Accuracy::ExactOrZero ? 0 : total;
}
n *= dim;
size_t padding = (a - (total % a)) % a;
if (total + padding > maxSize || n > maxSize - total - padding) {
overflow = true;
return accuracy == Accuracy::ExactOrZero ? 0 : total;
}
if (currentBitCount > 0) {
bool fitsInBitfield = currentBitCount + (n * charBit) <= currentBitfieldAlloc * charBit;
bool isAligned = currentBitCount % (charBit * a) == 0;
Expand All @@ -8843,17 +8867,23 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
return typeScope->type == ScopeType::eUnion ? std::max(total, n) : total + padding + n;
};
Result result = accumulateStructMembers(typeScope, accHelper, accuracy, settings);
overflow = overflow || result.overflow;
size_t total = result.total;
if (currentBitCount > 0)
total += currentBitfieldAlloc;
if (const ::Type* dt = typeScope->definedType) {
total = std::accumulate(dt->derivedFrom.begin(), dt->derivedFrom.end(), total, [&](size_t v, const ::Type::BaseInfo& bi) {
if (bi.type && bi.type->classScope)
v += accumulateStructMembers(bi.type->classScope, accHelper, accuracy, settings).total;
if (bi.type && bi.type->classScope) {
const Result baseResult = accumulateStructMembers(bi.type->classScope, accHelper, accuracy, settings);
if (baseResult.overflow || v > maxSize || baseResult.total > maxSize - v)
overflow = true;
else
v += baseResult.total;
}
return v;
});
}
if (accuracy == Accuracy::ExactOrZero && total == 0 && !result.success)
if (accuracy == Accuracy::ExactOrZero && (overflow || (total == 0 && !result.success)))
return 0;
total = std::max(size_t{1}, total);
size_t align = getAlignOf(*this, settings, accuracy, sizeOf);
Expand Down
7 changes: 7 additions & 0 deletions lib/symboldatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,13 @@ class CPPCHECKLIB Variable {
return mDimensions;
}

/**
* Get the number of array elements, the product of all dimension lengths.
* @param[out] product the number of elements
* @return false if the product does not fit in a MathLib::bigint
*/
bool dimensionsProduct(MathLib::bigint &product) const;

/**
* Get array dimension length.
* @return length of dimension
Expand Down
46 changes: 46 additions & 0 deletions test/testbufferoverrun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ class TestBufferOverrun : public TestFixture {
TEST_CASE(minsize_strlen);
TEST_CASE(minsize_mul);
TEST_CASE(minsize_overflow);
TEST_CASE(arraysize_overflow);
TEST_CASE(unknownType);

TEST_CASE(terminateStrncpy1);
Expand Down Expand Up @@ -4990,6 +4991,51 @@ class TestBufferOverrun : public TestFixture {
ASSERT_EQUALS("[test.cpp:3:19]: (error) Buffer is accessed out of bounds: buf [bufferAccessOutOfBounds]\n", errout_str());
}

void arraysize_overflow() {
// the number of array elements must not wrap around
check("void f() {\n"
" char a[0x4000000000000001][4];\n"
" memset(a, 0, 10);\n"
"}\n", settings0);
ASSERT_EQUALS("", errout_str());

check("void f() {\n"
" char a[0x100000000][0x100000000];\n"
" memset(a, 0, 10);\n"
"}\n", settings0);
ASSERT_EQUALS("", errout_str());

// the size in bytes must not wrap around
check("void f() {\n"
" int a[0x4000000000000001];\n"
" memset(a, 0, 10);\n"
"}\n", settings0);
ASSERT_EQUALS("", errout_str());

check("void f() {\n"
" int a[0x4000000000000001][1];\n"
" memset(a, 0, 10);\n"
"}\n", settings0);
ASSERT_EQUALS("", errout_str());

// the size of a derived class must not wrap around
check("struct B { int i; };\n"
"struct D : B { char m[0x100000000][0x100000000]; };\n"
"void f() {\n"
" D d;\n"
" memset(&d, 0, 10);\n"
"}\n", settings0);
ASSERT_EQUALS("", errout_str());

check("struct B { int i; };\n"
"struct D : B { char b[0x6000000000000000]; char c[0x6000000000000000]; };\n"
"int f() {\n"
" char buf[2] = {0};\n"
" return buf[sizeof(D)];\n"
"}\n", settings0);
ASSERT_EQUALS("", errout_str());
}

// extracttests.enable

void unknownType() {
Expand Down
52 changes: 52 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,58 @@ class TestValueFlow : public TestFixture {
ASSERT_EQUALS(1U, values.size());
ASSERT_EQUALS(settings.platform.sizeof_int, values.back().intvalue);
ASSERT_EQUALS_ENUM(ValueFlow::Value::ValueKind::Known, values.back().valueKind);

// the number of array elements does not fit in a MathLib::bigint
code = "struct S { char m[0x4000000000000001][4]; };\n"
"x = sizeof(S);\n";
values = tokenValues(code, "( S");
ASSERT_EQUALS(1U, values.size());
ASSERT(values.back().isImpossible());

// the size in bytes does not fit in a MathLib::bigint
code = "struct S { int m[0x4000000000000001]; };\n"
"x = sizeof(S);\n";
values = tokenValues(code, "( S");
ASSERT_EQUALS(1U, values.size());
ASSERT(values.back().isImpossible());

// the overflow must not be lost when base classes are added
code = "struct B { int i; };\n"
"struct D : B { int m[0x4000000000000001]; };\n"
"x = sizeof(D);\n";
values = tokenValues(code, "( D");
ASSERT_EQUALS(1U, values.size());
ASSERT(values.back().isImpossible());

code = "struct B { int i; };\n"
"struct D : B { char m[0x100000000][0x100000000]; };\n"
"x = sizeof(D);\n";
values = tokenValues(code, "( D");
ASSERT_EQUALS(1U, values.size());
ASSERT(values.back().isImpossible());

code = "struct B { int i; };\n"
"struct D : B { char b[0x6000000000000000]; char c[0x6000000000000000]; };\n"
"x = sizeof(D);\n";
values = tokenValues(code, "( D");
ASSERT_EQUALS(1U, values.size());
ASSERT(values.back().isImpossible());

code = "struct B { char m[0x100000000][0x100000000]; };\n"
"struct D : B { int i; };\n"
"x = sizeof(D);\n";
values = tokenValues(code, "( D");
ASSERT_EQUALS(1U, values.size());
ASSERT(values.back().isImpossible());

// the sum of the base class sizes does not fit in a MathLib::bigint
code = "struct B1 { char m[0x6000000000000000]; };\n"
"struct B2 { char m[0x6000000000000000]; };\n"
"struct D : B1, B2 { char c; };\n"
"x = sizeof(D);\n";
values = tokenValues(code, "( D");
ASSERT_EQUALS(1U, values.size());
ASSERT(values.back().isImpossible());
}

void valueFlowComma()
Expand Down
Loading