-release compiler switch and associative arrays

Don nospam at nospam.com
Tue Oct 11 13:23:30 PDT 2011


On 09.10.2011 13:24, Graham Cole wrote:
> I understand from the documentation that the "-release" compiler switch turns off "array bounds checking for system and trusted functions".
>
> Is it correct that the following code should seg fault when compiled with "-release" ?
>
> string[string] h;
> h["abc"] = "def";
> string s = h["aaa"];
>
> I.e. retrieving from an associative array for a non existent key.
>
> I would have thought that an exception should be generated in this case when compiled with
> "-release" (as it is when compiled without).
>
> This code behaves the same when compiled by both D1 and D2.
>
> Should I report this as a bug ?

In the compiler source (e2ir.c, IndexExp::toElem), AA checking is 
generated only when bounds checking is enabled. So it's intentional.

What really happens is that h[xxx] returns a pointer to the element, or 
null if none.
If it's null, then a bounds error is thrown. But this check is removed 
for -release.
Then string s= h["aaa"] dereferences the null pointer, and you get a 
segfault.

Here's a funny side-effect: the code below works fine with -release. p 
is null. But you get an array bounds error if not using -release.

  string[string] h;
  h["abc"] = "def";
  string *p = &h["aaa"];



More information about the Digitalmars-d-learn mailing list