[Issue 2632] New: Setting length on invalid arrays causes assertion failure with a debug runtime

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jan 29 09:11:47 PST 2009


http://d.puremagic.com/issues/show_bug.cgi?id=2632

           Summary: Setting length on invalid arrays causes assertion
                    failure with a debug runtime
           Product: D
           Version: 1.039
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: major
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: matti.niemenmaa+dbugzilla at iki.fi


Both of the below test cases work unless compiled against a debug runtime.

Test case 1:

void main() {
        int[] a = (cast(int*)null)[0..1];
        a.length = 1;
        assert (a.length == 1);
        assert (a[0] == 0);
}

Test case 2:

union Union {
        int i;
        ubyte[] a;
}
void main() {
        Union u;
        u.i = 10;
        u.a.length = 1;
        assert (u.a.length == 1);
        assert (u.a[0] == 1);
}

The problematic assertion is assert(!p.length || p.data). It occurs in three
functions:

- _d_delarray
- _d_arraysetlengthT
- _d_arraysetlengthiT

The second one of the above is triggered by the given test cases. It'd be easy
to come up with ones that trigger the other two as well.

This behaviour doesn't seem to be specified, so I'm not completely sure whether
this should be fixed or not. I have two arguments in favour of removing the
assertions:

1. They're only sanity checks: they don't actually matter for behaviour. Code
which trips the asserts on a debug runtime runs fine on a release runtime.
Nothing actually expects the assertions to succeed.

2. Code which relies on such behaviour actually exists: test case 2 was reduced
from tango.text.Regex.

In Phobos, the functions are in phobos/internal/gc/gc.d; in Tango, they can be
found in tango/lib/compiler/{dmd,gdc}/lifetime.d; in LDC, they're in
runtime/internal/lifetime.d. This issue affects all of them.


-- 



More information about the Digitalmars-d-bugs mailing list