Skip to content

Commit

Permalink
Merge pull request #4654 from WalterBright/functional-overflow
Browse files Browse the repository at this point in the history
std.functional: add overflow checks
  • Loading branch information
schveiguy authored Aug 2, 2016
2 parents 6784f63 + e99a7f6 commit ef82829
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions std/functional.d
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,10 @@ template memoize(alias fun, uint maxSize)
{
import core.memory : GC;

// Ensure no allocation overflows
static assert(maxSize < size_t.max / Value.sizeof);
static assert(maxSize < size_t.max - (8 * size_t.sizeof - 1));

enum attr = GC.BlkAttr.NO_INTERIOR | (hasIndirections!Value ? 0 : GC.BlkAttr.NO_SCAN);
memo = (cast(Value*)GC.malloc(Value.sizeof * maxSize, attr))[0 .. maxSize];
enum nwords = (maxSize + 8 * size_t.sizeof - 1) / (8 * size_t.sizeof);
Expand Down

0 comments on commit ef82829

Please sign in to comment.