Bug 2547 – Array Ops should check length, at least when bounds checking is on
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2008-12-30T16:33:35Z
Last change time
2022-02-28T03:30:25Z
Keywords
pull, wrong-code
Assigned to
yebblies
Creator
David Simcha
Comments
Comment #0 by dsimcha — 2008-12-30T16:33:35Z
import std.stdio;
void main() {
double[] foo = [1.0,2,3,4,5].dup;
double[] bar = [6.0,7,8,9,10].dup;
double[] baz = new double[1];
baz[] = foo[] + bar[];
writeln(baz); // Prints [7].
double[] waldo = new double[10]; // Should be all Nans.
waldo[] = foo[] + bar[];
writeln(waldo); // Prints [7 9 11 13 15 0 0 0 7 9].
double[] stuff = new double[10];
stuff[] = foo[] + waldo[];
writeln(stuff); // Prints [8 11 14 17 20 0 0 0 8 11].
}
The array ops should either automatically change the lengths of baz[] and waldo[] to match foo[] and bar[] (both in release and debug mode), or throw a RangeError when bounds checking is enabled.
In the last example, adding two arrays of different lengths either should not be allowed and should throw a RangeError when bounds checking is enabled, or should have reasonably well-defined behavior instead of the seemingly arbitrary behavior exhibited.
Comment #1 by clugdbug — 2010-08-09T00:05:25Z
*** Issue 4599 has been marked as a duplicate of this issue. ***
Comment #2 by simen.kjaras — 2011-02-21T14:36:08Z
*** Issue 3816 has been marked as a duplicate of this issue. ***
Comment #10 by verylonglogin.reg — 2013-11-17T23:42:53Z
I was wrong. It's not fixed. The pull just fixed druntime but dmd sometimes generate vector functions itself like `_arraySliceSliceSubass_i` for `a[] -= b[]` and doesn't check arrays for being conformable. E.g. this program does not throw any errors:
---
void main()
{
int[] a = new int[3];
int[] b = new int[7]; b[] = 1;
// a[] += b[]; // ok, calls _arraySliceSliceAddass_i and throws
// a[] *= b[]; // ok, calls _arraySliceSliceMulass_i and throws
a[] -= b[];
a[] /= b[];
a[] &= b[];
a[] |= b[];
a[] ^= b[];
}
---
To fix this we should make `enforce*ArraysConformable` API from `rt.util.array` public and dmd should call these functions in generated vector functions just like druntime do.
Comment #11 by verylonglogin.reg — 2014-08-10T12:54:09Z
*** Issue 13276 has been marked as a duplicate of this issue. ***
Comment #12 by iamthewilsonator — 2022-02-28T03:30:25Z
All array operation in this thread now yield assert error due to the mismatched lengths.