Array operation doesn't check array bounds

Jonathan M Davis jmdavisProg at gmx.com
Sun Apr 3 16:06:28 PDT 2011


On 2011-04-03 04:10, simendsjo wrote:
> 	int[] a = [1,2,3];
> 
> 	int[4] b;
> 	assert(b == [0,0,0,0]);
> 	b = a[] * 3; // oops... a[] * 3 takes element outside a's bounds
> 	assert(b[$-1] == 0); // fails.. last element is *(a.ptr+3) * 3

Array bounds checking is done on code which is not compiled with the -
noboundscheck flag and which is either not built with -release or is @safe.

I assume that you're not compiling with -noboundscheck (which turns off all 
array bounds checking). So, you're likely compiling with -release on code 
which isn't @safe. @system is the default, so unless you've marked your code 
@safe or you're not compiling with -release, I wouldn't expect there to be any 
bounds checking. If you want to guarantee that there's always bounds checking, 
then you need to mark your code @safe and not use -noboundscheck. However, 
given how little of Phobos is currently @safe or @trusted, odds are that 
trying to mark your code @safe will get _really_ annoying at this point. And 
to fix that, we'd likely need conditional @safe and conditional @trusted for 
the same reasons that we need conditional pure. And those haven't been taken 
care of yet (there isn't even an official plan to as far as I know - though 
hopefully there will be).

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list