Bug 10516 – Array length is not checked when array is a manifest constant
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-06-30T18:38:00Z
Last change time
2013-06-30T19:01:18Z
Assigned to
nobody
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2013-06-30T18:38:31Z
-----
void main()
{
enum HexSize = 5;
// note: smaller initializer count
enum char[HexSize] srcHex = "1234";
char[HexSize] tgtHex = "12345";
// should error with: lengths don't match for array copy
assert(tgtHex[0 .. 4] == srcHex);
}
-----
$ dmd test.d
> core.exception.AssertError@test(11): Assertion failure
The problem here is that the runtime allowed the left-hand side and right-hand side arrays to be compared even though their lengths do not match.
If you convert the source array from a manifest constant into a regular array (by removing enum) then the error is proper:
-----
void main()
{
enum HexSize = 5;
// note: smaller initializer count
/* enum */ char[HexSize] srcHex = "1234";
char[HexSize] tgtHex = "12345";
assert(tgtHex[0 .. 4] == srcHex);
}
-----
$ dmd test.d
> object.Error: lengths don't match for array copy, 5 = 4
Comment #1 by andrej.mitrovich — 2013-06-30T19:01:18Z
> -----
> void main()
> {
> enum HexSize = 5;
>
> // note: smaller initializer count
> /* enum */ char[HexSize] srcHex = "1234";
>
> char[HexSize] tgtHex = "12345";
>
> assert(tgtHex[0 .. 4] == srcHex);
> }
> -----
>
> $ dmd test.d
> > object.Error: lengths don't match for array copy, 5 = 4
Actually this test-case is invalid because the error is thrown in the initializer line, not the comparison line.
And another thing I just realized is that it's perfectly ok to compare static arrays of non-matching sizes, which makes the whole report invalid.. but I don't understand why this is allowed:
void main()
{
char[5] srcHex = "12345";
char[3] tgtHex = "123";
assert(srcHex == tgtHex);
}
$ dmd test.d
> core.exception.AssertError@test(5): Assertion failure
I would have hoped to get a length mismatch error, or even a compile-time error. Oh well..