Repository navigation
fix signed overflow when multiplying array dimensions #9027
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,7 +43,6 @@ | |
| #include <string> | ||
| #include <utility> | ||
| #include <vector> | ||
| #include <numeric> | ||
|
|
||
| class Settings; | ||
|
|
||
|
|
@@ -1638,9 +1637,11 @@ 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; | ||
| }); | ||
| MathLib::bigint mul = 0; | ||
| if (!var.dimensionsProduct(mul)) { | ||
| typeSize = 0; | ||
| break; | ||
| } | ||
| if (var.valueType()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AI review The same applies here: |
||
| typeSize += mul * var.valueType()->getSizeOf(settings, ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointer); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ | |
| #include <iomanip> | ||
| #include <iostream> | ||
| #include <iterator> | ||
| #include <limits> | ||
| #include <numeric> | ||
| #include <sstream> | ||
| #include <stack> | ||
|
|
@@ -2515,6 +2516,22 @@ bool Variable::isMember() const { | |
| return mScope && mScope->isClassOrStructOrUnion(); | ||
| } | ||
|
|
||
| bool Variable::dimensionsProduct(MathLib::bigint &product) const | ||
| { | ||
| const MathLib::bigint maxValue = std::numeric_limits<MathLib::bigint>::max(); | ||
| const MathLib::bigint minValue = std::numeric_limits<MathLib::bigint>::min(); | ||
| product = 1; | ||
| for (const Dimension &dim : mDimensions) { | ||
| const MathLib::bigint n = dim.num; | ||
| if (product > 0 && (n > 0 ? product > maxValue / n : n < minValue / product)) | ||
| return false; | ||
| if (product < 0 && (n > 0 ? product < minValue / n : (n < 0 && product < maxValue / n))) | ||
| return false; | ||
| product *= n; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| bool Variable::isPointerArray() const | ||
| { | ||
| if (!isArray()) | ||
|
|
@@ -8698,9 +8715,9 @@ static Result accumulateStructMembers(const Scope* scope, F f, ValueType::Accura | |
| 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; | ||
| }); | ||
| MathLib::bigint dim = 0; | ||
| if (!var.dimensionsProduct(dim)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AI review
|
||
| return {0, false}; | ||
| if (var.nameToken()->scope() != scope && var.nameToken()->scope()->definedType) { // anonymous union | ||
| const auto ret = anonScopes.insert(var.nameToken()->scope()); | ||
| if (ret.second) | ||
|
|
||
There was a problem hiding this comment.
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) * typeSizecan still wrap, because it is size_t arithmetic.void f() { int a[0x4000000000000001]; memset(a, 0, 10); }still gives a false positivebufferAccessOutOfBounds, and so doesint a[0x4000000000000001][1]. The byte size needs the same check. The existingmulOverflows()a few lines below in this file could be used for both, instead of adding a second implementation inVariable::dimensionsProduct.