Skip to content

Commit d9eb5c7

Browse files
Nussu06metsw24-max
authored andcommitted
check the array byte size for overflow too and share mulOverflows
1 parent 306a37e commit d9eb5c7

7 files changed

Lines changed: 63 additions & 31 deletions

File tree

‎lib/checkbufferoverrun.cpp‎

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -639,33 +639,23 @@ ValueFlow::Value CheckBufferOverrunImpl::getBufferSize(const Token *bufTok, cons
639639
if (!var->dimensionsProduct(dim))
640640
return ValueFlow::Value(-1);
641641

642+
MathLib::bigint typeSize;
643+
if (var->isPointerArray())
644+
typeSize = mSettings.platform.sizeof_pointer;
645+
else
646+
typeSize = bufTok->valueType()->getSizeOf(mSettings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
647+
if (MathLib::mulOverflows(dim - index, typeSize))
648+
return ValueFlow::Value(-1);
649+
642650
ValueFlow::Value v;
643651
v.setKnown();
644652
v.valueType = ValueFlow::Value::ValueType::BUFFER_SIZE;
645-
646-
if (var->isPointerArray())
647-
v.intvalue = (dim - index) * mSettings.platform.sizeof_pointer;
648-
else {
649-
const size_t typeSize = bufTok->valueType()->getSizeOf(mSettings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
650-
v.intvalue = (dim - index) * typeSize;
651-
}
653+
v.intvalue = (dim - index) * typeSize;
652654

653655
return v;
654656
}
655657
//---------------------------------------------------------------------------
656658

657-
// Return true if a * b does not fit in a MathLib::bigint
658-
static bool mulOverflows(MathLib::bigint a, MathLib::bigint b)
659-
{
660-
const MathLib::bigint maxValue = std::numeric_limits<MathLib::bigint>::max();
661-
const MathLib::bigint minValue = std::numeric_limits<MathLib::bigint>::min();
662-
if (a > 0)
663-
return b > 0 ? a > maxValue / b : b < minValue / a;
664-
if (a < 0)
665-
return b > 0 ? a < minValue / b : (b < 0 && a < maxValue / b);
666-
return false;
667-
}
668-
669659
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)
670660
{
671661
const Token * const arg = (minsize.arg > 0 && minsize.arg - 1 < args.size()) ? args[minsize.arg - 1] : nullptr;
@@ -690,7 +680,7 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
690680
break;
691681
MathLib::bigint myMinsize = argVal->intvalue;
692682
const int baseSize = tokenizer->sizeOfType(minsize.baseType);
693-
const bool overflow = baseSize != 0 && mulOverflows(myMinsize, baseSize);
683+
const bool overflow = baseSize != 0 && MathLib::mulOverflows(myMinsize, baseSize);
694684
if (baseSize != 0 && !overflow)
695685
myMinsize *= baseSize;
696686
const bool ok = !overflow && myMinsize <= bufferSize.intvalue;
@@ -708,7 +698,7 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
708698
break;
709699
case Library::ArgumentChecks::MinSize::Type::MUL:
710700
if (arg && arg2 && arg->hasKnownIntValue() && arg2->hasKnownIntValue()) {
711-
if (mulOverflows(arg->getKnownIntValue(), arg2->getKnownIntValue()))
701+
if (MathLib::mulOverflows(arg->getKnownIntValue(), arg2->getKnownIntValue()))
712702
return false;
713703
return (arg->getKnownIntValue() * arg2->getKnownIntValue()) <= bufferSize.intvalue;
714704
}
@@ -717,7 +707,7 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
717707
MathLib::bigint myMinsize = minsize.value;
718708
const int baseSize = tokenizer->sizeOfType(minsize.baseType);
719709
if (baseSize != 0) {
720-
if (mulOverflows(myMinsize, baseSize))
710+
if (MathLib::mulOverflows(myMinsize, baseSize))
721711
return false;
722712
myMinsize *= baseSize;
723713
}

‎lib/clangimport.cpp‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <cstring>
3535
#include <iostream>
3636
#include <iterator>
37+
#include <limits>
3738
#include <list>
3839
#include <map>
3940
#include <memory>
@@ -1642,8 +1643,14 @@ static void setValues(const Tokenizer &tokenizer, const SymbolDatabase *symbolDa
16421643
typeSize = 0;
16431644
break;
16441645
}
1645-
if (var.valueType())
1646-
typeSize += mul * var.valueType()->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer);
1646+
if (var.valueType()) {
1647+
const MathLib::bigint size = var.valueType()->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer);
1648+
if (MathLib::mulOverflows(mul, size) || mul * size > std::numeric_limits<MathLib::bigint>::max() - typeSize) {
1649+
typeSize = 0;
1650+
break;
1651+
}
1652+
typeSize += mul * size;
1653+
}
16471654
}
16481655
scope.definedType->sizeOf = typeSize;
16491656
}

‎lib/mathlib.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,17 @@ bool MathLib::isOctalDigit(char c)
13341334
return (c >= '0' && c <= '7');
13351335
}
13361336

1337+
bool MathLib::mulOverflows(bigint a, bigint b)
1338+
{
1339+
const bigint maxValue = std::numeric_limits<bigint>::max();
1340+
const bigint minValue = std::numeric_limits<bigint>::min();
1341+
if (a > 0)
1342+
return b > 0 ? a > maxValue / b : b < minValue / a;
1343+
if (a < 0)
1344+
return b > 0 ? a < minValue / b : (b < 0 && a < maxValue / b);
1345+
return false;
1346+
}
1347+
13371348
MathLib::value operator+(const MathLib::value &v1, const MathLib::value &v2)
13381349
{
13391350
return MathLib::value::calc('+',v1,v2);

‎lib/mathlib.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ class CPPCHECKLIB MathLib {
170170
*/
171171
static bool isOctalDigit(char c);
172172

173+
/** @brief Return true if a * b does not fit in a bigint */
174+
static bool mulOverflows(bigint a, bigint b);
175+
173176
static unsigned int encodeMultiChar(const std::string& str);
174177
};
175178

‎lib/symboldatabase.cpp‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2527,16 +2527,11 @@ bool Variable::isMember() const {
25272527

25282528
bool Variable::dimensionsProduct(MathLib::bigint &product) const
25292529
{
2530-
const MathLib::bigint maxValue = std::numeric_limits<MathLib::bigint>::max();
2531-
const MathLib::bigint minValue = std::numeric_limits<MathLib::bigint>::min();
25322530
product = 1;
25332531
for (const Dimension &dim : mDimensions) {
2534-
const MathLib::bigint n = dim.num;
2535-
if (product > 0 && (n > 0 ? product > maxValue / n : n < minValue / product))
2532+
if (MathLib::mulOverflows(product, dim.num))
25362533
return false;
2537-
if (product < 0 && (n > 0 ? product < minValue / n : (n < 0 && product < maxValue / n)))
2538-
return false;
2539-
product *= n;
2534+
product *= dim.num;
25402535
}
25412536
return true;
25422537
}
@@ -8861,8 +8856,14 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
88618856
currentBitCount += nBits;
88628857
return ret;
88638858
}
8859+
// the size must fit in a MathLib::bigint
8860+
const size_t maxSize = std::numeric_limits<MathLib::bigint>::max();
8861+
if (MathLib::mulOverflows(static_cast<MathLib::bigint>(n), static_cast<MathLib::bigint>(dim)))
8862+
return accuracy == Accuracy::ExactOrZero ? 0 : total;
88648863
n *= dim;
88658864
size_t padding = (a - (total % a)) % a;
8865+
if (total + padding > maxSize || n > maxSize - total - padding)
8866+
return accuracy == Accuracy::ExactOrZero ? 0 : total;
88668867
if (currentBitCount > 0) {
88678868
bool fitsInBitfield = currentBitCount + (n * charBit) <= currentBitfieldAlloc * charBit;
88688869
bool isAligned = currentBitCount % (charBit * a) == 0;

‎test/testbufferoverrun.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5058,6 +5058,19 @@ class TestBufferOverrun : public TestFixture {
50585058
" memset(a, 0, 10);\n"
50595059
"}\n", settings0);
50605060
ASSERT_EQUALS("", errout_str());
5061+
5062+
// the size in bytes must not wrap around
5063+
check("void f() {\n"
5064+
" int a[0x4000000000000001];\n"
5065+
" memset(a, 0, 10);\n"
5066+
"}\n", settings0);
5067+
ASSERT_EQUALS("", errout_str());
5068+
5069+
check("void f() {\n"
5070+
" int a[0x4000000000000001][1];\n"
5071+
" memset(a, 0, 10);\n"
5072+
"}\n", settings0);
5073+
ASSERT_EQUALS("", errout_str());
50615074
}
50625075

50635076
// extracttests.enable

‎test/testvalueflow.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,13 @@ class TestValueFlow : public TestFixture {
19861986
values = tokenValues(code, "( S");
19871987
ASSERT_EQUALS(1U, values.size());
19881988
ASSERT(values.back().isImpossible());
1989+
1990+
// the size in bytes does not fit in a MathLib::bigint
1991+
code = "struct S { int m[0x4000000000000001]; };\n"
1992+
"x = sizeof(S);\n";
1993+
values = tokenValues(code, "( S");
1994+
ASSERT_EQUALS(1U, values.size());
1995+
ASSERT(values.back().isImpossible());
19891996
}
19901997

19911998
void valueFlowComma()

0 commit comments

Comments
 (0)