Skip to content

Commit 306a37e

Browse files
Nussu06metsw24-max
authored andcommitted
fix signed overflow when multiplying array dimensions
1 parent c3f3b79 commit 306a37e

6 files changed

Lines changed: 58 additions & 10 deletions

File tree

‎lib/checkbufferoverrun.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,9 @@ ValueFlow::Value CheckBufferOverrunImpl::getBufferSize(const Token *bufTok, cons
635635
if (!var || var->isPointer() || (astIsContainer(bufTok) && var->getTypeName() != "std::array"))
636636
return ValueFlow::Value(-1);
637637

638-
const MathLib::bigint dim = std::accumulate(var->dimensions().cbegin(), var->dimensions().cend(), MathLib::bigint(1), [](MathLib::bigint i1, const Dimension &dim) {
639-
return i1 * dim.num;
640-
});
638+
MathLib::bigint dim = 0;
639+
if (!var->dimensionsProduct(dim))
640+
return ValueFlow::Value(-1);
641641

642642
ValueFlow::Value v;
643643
v.setKnown();

‎lib/clangimport.cpp‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#include <string>
4444
#include <utility>
4545
#include <vector>
46-
#include <numeric>
4746

4847
class Settings;
4948

@@ -1638,9 +1637,11 @@ static void setValues(const Tokenizer &tokenizer, const SymbolDatabase *symbolDa
16381637

16391638
MathLib::bigint typeSize = 0;
16401639
for (const Variable &var: scope.varlist) {
1641-
const MathLib::bigint mul = std::accumulate(var.dimensions().cbegin(), var.dimensions().cend(), MathLib::bigint(1), [](MathLib::bigint v, const Dimension& dim) {
1642-
return v * dim.num;
1643-
});
1640+
MathLib::bigint mul = 0;
1641+
if (!var.dimensionsProduct(mul)) {
1642+
typeSize = 0;
1643+
break;
1644+
}
16441645
if (var.valueType())
16451646
typeSize += mul * var.valueType()->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer);
16461647
}

‎lib/symboldatabase.cpp‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <iomanip>
4747
#include <iostream>
4848
#include <iterator>
49+
#include <limits>
4950
#include <numeric>
5051
#include <sstream>
5152
#include <stack>
@@ -2524,6 +2525,22 @@ bool Variable::isMember() const {
25242525
return mScope && mScope->isClassOrStructOrUnion();
25252526
}
25262527

2528+
bool Variable::dimensionsProduct(MathLib::bigint &product) const
2529+
{
2530+
const MathLib::bigint maxValue = std::numeric_limits<MathLib::bigint>::max();
2531+
const MathLib::bigint minValue = std::numeric_limits<MathLib::bigint>::min();
2532+
product = 1;
2533+
for (const Dimension &dim : mDimensions) {
2534+
const MathLib::bigint n = dim.num;
2535+
if (product > 0 && (n > 0 ? product > maxValue / n : n < minValue / product))
2536+
return false;
2537+
if (product < 0 && (n > 0 ? product < minValue / n : (n < 0 && product < maxValue / n)))
2538+
return false;
2539+
product *= n;
2540+
}
2541+
return true;
2542+
}
2543+
25272544
bool Variable::isPointerArray() const
25282545
{
25292546
if (!isArray())
@@ -8715,9 +8732,9 @@ static Result accumulateStructMembers(const Scope* scope, F f, ValueType::Accura
87158732
if (const ValueType* vt = var.valueType()) {
87168733
if (vt->type == ValueType::Type::RECORD && vt->typeScope == scope)
87178734
return {0, false};
8718-
const MathLib::bigint dim = std::accumulate(var.dimensions().cbegin(), var.dimensions().cend(), MathLib::bigint(1), [](MathLib::bigint i1, const Dimension& dim) {
8719-
return i1 * dim.num;
8720-
});
8735+
MathLib::bigint dim = 0;
8736+
if (!var.dimensionsProduct(dim))
8737+
return {0, false};
87218738
if (var.nameToken()->scope() != scope && var.nameToken()->scope()->definedType) { // anonymous union
87228739
const auto ret = anonScopes.insert(var.nameToken()->scope());
87238740
if (ret.second)

‎lib/symboldatabase.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,13 @@ class CPPCHECKLIB Variable {
553553
return mDimensions;
554554
}
555555

556+
/**
557+
* Get the number of array elements, the product of all dimension lengths.
558+
* @param[out] product the number of elements
559+
* @return false if the product does not fit in a MathLib::bigint
560+
*/
561+
bool dimensionsProduct(MathLib::bigint &product) const;
562+
556563
/**
557564
* Get array dimension length.
558565
* @return length of dimension

‎test/testbufferoverrun.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ class TestBufferOverrun : public TestFixture {
273273
TEST_CASE(minsize_strlen);
274274
TEST_CASE(minsize_mul);
275275
TEST_CASE(minsize_overflow);
276+
TEST_CASE(arraysize_overflow);
276277
TEST_CASE(unknownType);
277278

278279
TEST_CASE(terminateStrncpy1);
@@ -5044,6 +5045,21 @@ class TestBufferOverrun : public TestFixture {
50445045
ASSERT_EQUALS("[test.cpp:3:19]: (error) Buffer is accessed out of bounds: buf [bufferAccessOutOfBounds]\n", errout_str());
50455046
}
50465047

5048+
void arraysize_overflow() {
5049+
// the number of array elements must not wrap around
5050+
check("void f() {\n"
5051+
" char a[0x4000000000000001][4];\n"
5052+
" memset(a, 0, 10);\n"
5053+
"}\n", settings0);
5054+
ASSERT_EQUALS("", errout_str());
5055+
5056+
check("void f() {\n"
5057+
" char a[0x100000000][0x100000000];\n"
5058+
" memset(a, 0, 10);\n"
5059+
"}\n", settings0);
5060+
ASSERT_EQUALS("", errout_str());
5061+
}
5062+
50475063
// extracttests.enable
50485064

50495065
void unknownType() {

‎test/testvalueflow.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,13 @@ class TestValueFlow : public TestFixture {
19791979
ASSERT_EQUALS(1U, values.size());
19801980
ASSERT_EQUALS(settings.platform.sizeof_int, values.back().intvalue);
19811981
ASSERT_EQUALS_ENUM(ValueFlow::Value::ValueKind::Known, values.back().valueKind);
1982+
1983+
// the number of array elements does not fit in a MathLib::bigint
1984+
code = "struct S { char m[0x4000000000000001][4]; };\n"
1985+
"x = sizeof(S);\n";
1986+
values = tokenValues(code, "( S");
1987+
ASSERT_EQUALS(1U, values.size());
1988+
ASSERT(values.back().isImpossible());
19821989
}
19831990

19841991
void valueFlowComma()

0 commit comments

Comments
 (0)