D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2547 - Array Ops should check length, at least when bounds checking is on
Summary: Array Ops should check length, at least when bounds checking is on
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: yebblies
URL:
Keywords: pull, wrong-code
: 3816 4599 8650 10384 11416 13276 (view as issue list)
Depends on:
Blocks:
 
Reported: 2008-12-30 16:33 UTC by David Simcha
Modified: 2022-02-28 03:30 UTC (History)
8 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description David Simcha 2008-12-30 16:33:35 UTC
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 Don 2010-08-09 00:05:25 UTC
*** Issue 4599 has been marked as a duplicate of this issue. ***
Comment 2 Simen Kjaeraas 2011-02-21 14:36:08 UTC
*** Issue 3816 has been marked as a duplicate of this issue. ***
Comment 4 github-bugzilla 2012-07-20 23:37:16 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/9c80b3ce49a77f960674ff38df492ec7413376e1
Issue 2547 - Array Ops should check length, at least when bounds checking is on

When array bounds checking is enabled, turn

`arrayOp(a, b, ...)`

into

```
auto tmpa = a, tmpb = b, tmpc = c;
assert(tmpa.length == tmpb.length && tmpa.length == tmpc.length && ..., "length mismatch for <array op name>");
arrayOp(tmpa, tmpb, tmpc, ...)
```

https://github.com/D-Programming-Language/dmd/commit/34b0f7eba7ada6c39aafc8d19eee3887fa84bf3e
Merge pull request #693 from yebblies/issue2547

Issue 2547 - Array Ops should check length, at least when bounds checking is on
Comment 5 github-bugzilla 2012-07-21 00:07:04 UTC
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/b6ed591f117f6ac150089585c6965c14b5c5b43c
Revert "Merge pull request #693 from yebblies/issue2547"

This reverts commit 34b0f7eba7ada6c39aafc8d19eee3887fa84bf3e, reversing
changes made to 162cf303c5e70bbb7b0a397b847e0b76c923ddd9.
Comment 6 Don 2013-06-28 02:31:59 UTC
*** Issue 10384 has been marked as a duplicate of this issue. ***
Comment 7 yebblies 2013-11-14 21:38:49 UTC
*** Issue 11416 has been marked as a duplicate of this issue. ***
Comment 8 yebblies 2013-11-14 21:39:46 UTC
*** Issue 8650 has been marked as a duplicate of this issue. ***
Comment 9 Denis Shelomovskii 2013-11-15 08:43:35 UTC
Fixed in pull
https://github.com/D-Programming-Language/druntime/pull/661
Comment 10 Denis Shelomovskii 2013-11-17 23:42:53 UTC
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 Denis Shelomovskii 2014-08-10 12:54:09 UTC
*** Issue 13276 has been marked as a duplicate of this issue. ***
Comment 12 Nicholas Wilson 2022-02-28 03:30:25 UTC
All array operation in this thread now yield assert error due to the mismatched lengths.