Skip to content

Commit

Permalink
container.array.sorting.d: add overflow checks
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Aug 5, 2016
1 parent ee30556 commit 0c8fd57
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions std/container/array.d
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,12 @@ if (!is(Unqual!T == bool))
}
// enlarge
auto startEmplace = length;
import core.checkedint : mulu;
bool overflow;
const nbytes = mulu(newLength, T.sizeof, overflow);
if (overflow) assert(0);
_payload = (cast(T*) realloc(_payload.ptr,
T.sizeof * newLength))[0 .. newLength];
nbytes))[0 .. newLength];
initializeAll(_payload.ptr[startEmplace .. length]);
}

Expand All @@ -340,7 +344,10 @@ if (!is(Unqual!T == bool))
void reserve(size_t elements)
{
if (elements <= capacity) return;
immutable sz = elements * T.sizeof;
import core.checkedint : mulu;
bool overflow;
const sz = mulu(elements, T.sizeof, overflow);
if (overflow) assert(0);
static if (hasIndirections!T) // should use hasPointers instead
{
/* Because of the transactional nature of this
Expand Down Expand Up @@ -421,7 +428,11 @@ Constructor taking a number of items
this(U)(U[] values...) if (isImplicitlyConvertible!(U, T))
{
import std.conv : emplace;
auto p = cast(T*) malloc(T.sizeof * values.length);
import core.checkedint : mulu;
bool overflow;
const nbytes = mulu(values.length, T.sizeof, overflow);
if (overflow) assert(0);
auto p = cast(T*) malloc(nbytes);
static if (hasIndirections!T)
{
if (p)
Expand Down Expand Up @@ -533,7 +544,10 @@ Complexity: $(BIGOH 1)
if (!_data.refCountedStore.isInitialized)
{
if (!elements) return;
immutable sz = elements * T.sizeof;
import core.checkedint : mulu;
bool overflow;
const sz = mulu(elements, T.sizeof, overflow);
if (overflow) assert(0);
auto p = enforce(malloc(sz));
static if (hasIndirections!T)
{
Expand Down

0 comments on commit 0c8fd57

Please sign in to comment.