Bug 11576 – std.algorithm.remove!(SwapStrategy.unstable) overruns array bounds

Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-11-21T19:15:00Z
Last change time
2013-11-22T07:39:06Z
Keywords
pull
Assigned to
nobody
Creator
hsteoh

Comments

Comment #0 by hsteoh — 2013-11-21T19:15:09Z
CODE: ------- import std.algorithm, std.stdio; void main() { auto arr = [1,2,3,4]; arr = arr.remove!(SwapStrategy.unstable)(1); // OK assert(arr == [1,4,3]); arr = arr.remove!(SwapStrategy.unstable)(1); // OK assert(arr == [1,3]); arr = arr.remove!(SwapStrategy.unstable)(1); // NG assert(arr == [1]); } ------- OUTPUT: ------- [email protected](8139): Range violation ---------------- ./test(_d_array_bounds+0x26) [0x43e84e] ./test() [0x438b12] ./test(pure nothrow @safe int[] std.algorithm.__T6removeVE3std9algorithm12SwapStrategy0TAiTiZ.remove(int[], int)+0xab) [0x42e11b] ./test(_Dmain+0x154) [0x42dde4] ./test(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll().void __lambda1()+0x18) [0x435b98] ./test(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x435af2] ./test(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll()+0x30) [0x435b58] ./test(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x435af2] ./test(_d_run_main+0x1a3) [0x435a73] ./test(main+0x17) [0x433d67] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5) [0x7f17abe1d995] ------- The range violation occurs at the line marked // NG. This bug consistently happens every time you attempt to remove the last element from an array. In earlier versions of Phobos, this bug does not occur.
Comment #1 by hsteoh — 2013-11-21T19:15:49Z
P.S. This is on Phobos git HEAD. I haven't checked the latest release to see if it also happens there.
Comment #2 by hsteoh — 2013-11-21T19:37:11Z
Comment #3 by github-bugzilla — 2013-11-22T00:31:41Z
Comment #4 by hsteoh — 2013-11-22T07:39:06Z
Confirmed fixed in git HEAD.