mirror of
https://github.com/dlang/phobos.git
synced 2025-04-29 14:40:30 +03:00
Fix Issue 20799 - schwartzSort does not pin transformation results with indirections, leading to memory corruption
This commit is contained in:
parent
de58d71516
commit
793a78c588
1 changed files with 26 additions and 1 deletions
|
@ -2989,7 +2989,13 @@ if (isRandomAccessRange!R && hasLength!R && hasSwappableElements!R &&
|
|||
bool overflow;
|
||||
const nbytes = mulu(len, T.sizeof, overflow);
|
||||
if (overflow) assert(false, "multiplication overflowed");
|
||||
return (cast(T*) malloc(nbytes))[0 .. len];
|
||||
T[] result = (cast(T*) malloc(nbytes))[0 .. len];
|
||||
static if (hasIndirections!T)
|
||||
{
|
||||
import core.memory : GC;
|
||||
GC.addRange(result.ptr, nbytes);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
auto xform1 = trustedMalloc(r.length);
|
||||
|
||||
|
@ -3003,6 +3009,11 @@ if (isRandomAccessRange!R && hasLength!R && hasSwappableElements!R &&
|
|||
static void trustedFree(T[] p) @trusted
|
||||
{
|
||||
import core.stdc.stdlib : free;
|
||||
static if (hasIndirections!T)
|
||||
{
|
||||
import core.memory : GC;
|
||||
GC.removeRange(p.ptr);
|
||||
}
|
||||
free(p.ptr);
|
||||
}
|
||||
trustedFree(xform1);
|
||||
|
@ -3132,6 +3143,20 @@ if (isRandomAccessRange!R && hasLength!R && hasSwappableElements!R)
|
|||
assert(isSorted!("a < b")(map!(entropy)(arr)));
|
||||
}
|
||||
|
||||
// https://issues.dlang.org/show_bug.cgi?id=20799
|
||||
@safe unittest
|
||||
{
|
||||
import std.range : iota, retro;
|
||||
import std.array : array;
|
||||
|
||||
auto arr = 1_000_000.iota.retro.array;
|
||||
arr.schwartzSort!(
|
||||
n => new int(n),
|
||||
(a, b) => *a < *b
|
||||
);
|
||||
assert(arr.isSorted());
|
||||
}
|
||||
|
||||
// partialSort
|
||||
/**
|
||||
Reorders the random-access range `r` such that the range `r[0 .. mid]`
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue