mirror of
https://github.com/dlang/phobos.git
synced 2025-05-01 07:30:33 +03:00
comparison.d: add overflow checks
This commit is contained in:
parent
ee30556a34
commit
8f69e217bf
1 changed files with 8 additions and 2 deletions
|
@ -1001,13 +1001,19 @@ private:
|
||||||
ref CostType matrix(size_t row, size_t col) { return _matrix[row * cols + col]; }
|
ref CostType matrix(size_t row, size_t col) { return _matrix[row * cols + col]; }
|
||||||
|
|
||||||
void AllocMatrix(size_t r, size_t c) @trusted {
|
void AllocMatrix(size_t r, size_t c) @trusted {
|
||||||
|
import core.checkedint : mulu;
|
||||||
|
bool overflow;
|
||||||
|
const rc = mulu(r, c, overflow);
|
||||||
|
if (overflow) assert(0);
|
||||||
rows = r;
|
rows = r;
|
||||||
cols = c;
|
cols = c;
|
||||||
if (_matrix.length < r * c)
|
if (_matrix.length < rc)
|
||||||
{
|
{
|
||||||
import core.stdc.stdlib : realloc;
|
import core.stdc.stdlib : realloc;
|
||||||
import core.exception : onOutOfMemoryError;
|
import core.exception : onOutOfMemoryError;
|
||||||
auto m = cast(CostType *)realloc(_matrix.ptr, r * c * _matrix[0].sizeof);
|
const nbytes = mulu(rc, _matrix[0].sizeof, overflow);
|
||||||
|
if (overflow) assert(0);
|
||||||
|
auto m = cast(CostType *)realloc(_matrix.ptr, nbytes);
|
||||||
if (!m)
|
if (!m)
|
||||||
onOutOfMemoryError();
|
onOutOfMemoryError();
|
||||||
_matrix = m[0 .. r * c];
|
_matrix = m[0 .. r * c];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue