From ec8534d58b46401cbce1dbc04810e16c64a8308d Mon Sep 17 00:00:00 2001 From: Walter Bright Date: Fri, 5 Aug 2016 00:44:20 -0700 Subject: [PATCH] sorting.d: add overflow checks --- std/algorithm/sorting.d | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/std/algorithm/sorting.d b/std/algorithm/sorting.d index 59822317c0a..be215226004 100644 --- a/std/algorithm/sorting.d +++ b/std/algorithm/sorting.d @@ -2179,7 +2179,11 @@ schwartzSort(alias transform, alias less = "a < b", static trustedMalloc(size_t len) @trusted { import core.stdc.stdlib : malloc; - return (cast(T*) malloc(len * T.sizeof))[0 .. len]; + import core.checkedint : mulu; + bool overflow; + const nbytes = mulu(len, T.sizeof, overflow); + if (overflow) assert(0); + return (cast(T*) malloc(nbytes))[0 .. len]; } auto xform1 = trustedMalloc(r.length);