Skip to content

Commit

Permalink
std.array: check for overflow on allocation sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Jul 28, 2016
1 parent 6db08d3 commit 842f583
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions std/array.d
Expand Up @@ -629,9 +629,15 @@ private auto arrayAllocImpl(bool minimallyInitialized, T, I...)(I sizes) nothrow
{
import core.stdc.string : memset;
import core.memory : GC;
auto ptr = cast(E*) GC.malloc(sizes[0] * E.sizeof, blockAttribute!E);

import core.checkedint : mulu;
bool overflow;
const nbytes = mulu(size, E.sizeof, overflow);
if (overflow) assert(0);

auto ptr = cast(E*) GC.malloc(nbytes, blockAttribute!E);
static if (minimallyInitialized && hasIndirections!E)
memset(ptr, 0, size * E.sizeof);
memset(ptr, 0, nbytes);
ret = ptr[0 .. size];
}
}
Expand Down Expand Up @@ -2773,8 +2779,14 @@ if (isDynamicArray!A)
}
}


// didn't work, must reallocate
auto bi = GC.qalloc(newlen * T.sizeof, blockAttribute!T);
import core.checkedint : mulu;
bool overflow;
const nbytes = mulu(newlen, T.sizeof, overflow);
if (overflow) assert(0);

auto bi = GC.qalloc(nbytes, blockAttribute!T);
_data.capacity = bi.size / T.sizeof;
import core.stdc.string : memcpy;
if (len)
Expand Down

0 comments on commit 842f583

Please sign in to comment.