Skip to content

Commit 1906bfa

Browse files
committed
check the array byte size for overflow too and share mulOverflows
1 parent e208afc commit 1906bfa

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
@@ -635,33 +635,23 @@ ValueFlow::Value CheckBufferOverrunImpl::getBufferSize(const Token *bufTok, cons
635635
if (!var->dimensionsProduct(dim))
636636
return ValueFlow::Value(-1);
637637

638+
MathLib::bigint typeSize;
639+
if (var->isPointerArray())
640+
typeSize = mSettings.platform.sizeof_pointer;
641+
else
642+
typeSize = bufTok->valueType()->getSizeOf(mSettings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
643+
if (MathLib::mulOverflows(dim - index, typeSize))
644+
return ValueFlow::Value(-1);
645+
638646
ValueFlow::Value v;
639647
v.setKnown();
640648
v.valueType = ValueFlow::Value::ValueType::BUFFER_SIZE;
641-
642-
if (var->isPointerArray())
643-
v.intvalue = (dim - index) * mSettings.platform.sizeof_pointer;
644-
else {
645-
const size_t typeSize = bufTok->valueType()->getSizeOf(mSettings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
646-
v.intvalue = (dim - index) * typeSize;
647-
}
649+
v.intvalue = (dim - index) * typeSize;
648650

649651
return v;
650652
}
651653
//---------------------------------------------------------------------------
652654

653-
// Return true if a * b does not fit in a MathLib::bigint
654-
static bool mulOverflows(MathLib::bigint a, MathLib::bigint b)
655-
{
656-
const MathLib::bigint maxValue = std::numeric_limits<MathLib::bigint>::max();
657-
const MathLib::bigint minValue = std::numeric_limits<MathLib::bigint>::min();
658-
if (a > 0)
659-
return b > 0 ? a > maxValue / b : b < minValue / a;
660-
if (a < 0)
661-
return b > 0 ? a < minValue / b : (b < 0 && a < maxValue / b);
662-
return false;
663-
}
664-
665655
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)
666656
{
667657
const Token * const arg = (minsize.arg > 0 && minsize.arg - 1 < args.size()) ? args[minsize.arg - 1] : nullptr;
@@ -686,7 +676,7 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
686676
break;
687677
MathLib::bigint myMinsize = argVal->intvalue;
688678
const int baseSize = tokenizer->sizeOfType(minsize.baseType);
689-
const bool overflow = baseSize != 0 && mulOverflows(myMinsize, baseSize);
679+
const bool overflow = baseSize != 0 && MathLib::mulOverflows(myMinsize, baseSize);
690680
if (baseSize != 0 && !overflow)
691681
myMinsize *= baseSize;
692682
const bool ok = !overflow && myMinsize <= bufferSize.intvalue;
@@ -704,7 +694,7 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
704694
break;
705695
case Library::ArgumentChecks::MinSize::Type::MUL:
706696
if (arg && arg2 && arg->hasKnownIntValue() && arg2->hasKnownIntValue()) {
707-
if (mulOverflows(arg->getKnownIntValue(), arg2->getKnownIntValue()))
697+
if (MathLib::mulOverflows(arg->getKnownIntValue(), arg2->getKnownIntValue()))
708698
return false;
709699
return (arg->getKnownIntValue() * arg2->getKnownIntValue()) <= bufferSize.intvalue;
710700
}
@@ -713,7 +703,7 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
713703
MathLib::bigint myMinsize = minsize.value;
714704
const int baseSize = tokenizer->sizeOfType(minsize.baseType);
715705
if (baseSize != 0) {
716-
if (mulOverflows(myMinsize, baseSize))
706+
if (MathLib::mulOverflows(myMinsize, baseSize))
717707
return false;
718708
myMinsize *= baseSize;
719709
}

‎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
@@ -2518,16 +2518,11 @@ bool Variable::isMember() const {
25182518

25192519
bool Variable::dimensionsProduct(MathLib::bigint &product) const
25202520
{
2521-
const MathLib::bigint maxValue = std::numeric_limits<MathLib::bigint>::max();
2522-
const MathLib::bigint minValue = std::numeric_limits<MathLib::bigint>::min();
25232521
product = 1;
25242522
for (const Dimension &dim : mDimensions) {
2525-
const MathLib::bigint n = dim.num;
2526-
if (product > 0 && (n > 0 ? product > maxValue / n : n < minValue / product))
2523+
if (MathLib::mulOverflows(product, dim.num))
25272524
return false;
2528-
if (product < 0 && (n > 0 ? product < minValue / n : (n < 0 && product < maxValue / n)))
2529-
return false;
2530-
product *= n;
2525+
product *= dim.num;
25312526
}
25322527
return true;
25332528
}
@@ -8844,8 +8839,14 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
88448839
currentBitCount += nBits;
88458840
return ret;
88468841
}
8842+
// the size must fit in a MathLib::bigint
8843+
const size_t maxSize = std::numeric_limits<MathLib::bigint>::max();
8844+
if (MathLib::mulOverflows(static_cast<MathLib::bigint>(n), static_cast<MathLib::bigint>(dim)))
8845+
return accuracy == Accuracy::ExactOrZero ? 0 : total;
88478846
n *= dim;
88488847
size_t padding = (a - (total % a)) % a;
8848+
if (total + padding > maxSize || n > maxSize - total - padding)
8849+
return accuracy == Accuracy::ExactOrZero ? 0 : total;
88498850
if (currentBitCount > 0) {
88508851
bool fitsInBitfield = currentBitCount + (n * charBit) <= currentBitfieldAlloc * charBit;
88518852
bool isAligned = currentBitCount % (charBit * a) == 0;

‎test/testbufferoverrun.cpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5004,6 +5004,19 @@ class TestBufferOverrun : public TestFixture {
50045004
" memset(a, 0, 10);\n"
50055005
"}\n", settings0);
50065006
ASSERT_EQUALS("", errout_str());
5007+
5008+
// the size in bytes must not wrap around
5009+
check("void f() {\n"
5010+
" int a[0x4000000000000001];\n"
5011+
" memset(a, 0, 10);\n"
5012+
"}\n", settings0);
5013+
ASSERT_EQUALS("", errout_str());
5014+
5015+
check("void f() {\n"
5016+
" int a[0x4000000000000001][1];\n"
5017+
" memset(a, 0, 10);\n"
5018+
"}\n", settings0);
5019+
ASSERT_EQUALS("", errout_str());
50075020
}
50085021

50095022
// extracttests.enable

‎test/testvalueflow.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,13 @@ class TestValueFlow : public TestFixture {
18711871
values = tokenValues(code, "( S");
18721872
ASSERT_EQUALS(1U, values.size());
18731873
ASSERT(values.back().isImpossible());
1874+
1875+
// the size in bytes does not fit in a MathLib::bigint
1876+
code = "struct S { int m[0x4000000000000001]; };\n"
1877+
"x = sizeof(S);\n";
1878+
values = tokenValues(code, "( S");
1879+
ASSERT_EQUALS(1U, values.size());
1880+
ASSERT(values.back().isImpossible());
18741881
}
18751882

18761883
void valueFlowComma()

0 commit comments

Comments
 (0)