The TimSort implementation creates a temporary uninitialized array for copying ranges of elements to. While this works fine for POD values, element types with an elaborate destructor/postblit/copy constructor will be invoked with uninitialized data, possibly leading to crashes or data corruption. Test case: --- import std.algorithm; struct S { int i = 42; ~this() { assert(i == 42); } } void main() { auto array = new S[](400); array.sort!((a, b) => false, SwapStrategy.stable); } ---
@s-ludwig created dlang/phobos pull request #9049 "Bugzilla 24773 - don't invoke destructors on uninitialized elements in stable sort" mentioning this issue: - Bugzilla 24773 - don't invoke destructors on uninitialized elements in stable sort Uses a regular initialized temporary array when sorting elements with an elaborate assignment to avoid undefined behavior when destructors, postblits or copy constructors are invoked during the array assignment. https://github.com/dlang/phobos/pull/9049
dlang/phobos pull request #9049 "Fix Bugzilla 24773: don't invoke destructors on uninitialized elements in stable sort" was merged into master: - cfd577b28dead189f08bdf5d2b6c94b3352d0af5 by Sönke Ludwig: Fix Bugzilla 24773: Don't invoke destructors on uninitialized elements in stable sort Uses a regular initialized temporary array when sorting elements with an elaborate assignment to avoid undefined behavior when destructors, postblits or copy constructors are invoked during the array assignment. https://github.com/dlang/phobos/pull/9049
dlang/phobos pull request #9076 "[stable] Cherry-pick 2 master fixes" was merged into stable: - 9e78de4d86ec1b61cc39ed90083373bfa111ccdd by Sönke Ludwig: Fix Bugzilla 24773: Don't invoke destructors on uninitialized elements in stable sort Uses a regular initialized temporary array when sorting elements with an elaborate assignment to avoid undefined behavior when destructors, postblits or copy constructors are invoked during the array assignment. https://github.com/dlang/phobos/pull/9076