Thread overview
[Issue 1685] New: Array index is evaluated twice
Nov 22, 2007
d-bugmail
Nov 23, 2007
d-bugmail
Mar 01, 2008
d-bugmail
Dec 17, 2008
d-bugmail
Dec 25, 2008
d-bugmail
November 22, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1685

           Summary: Array index is evaluated twice
           Product: D
           Version: 2.007
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: i.kasiuk@gmx.de


Under certain circumstances, an array index is evaluated twice:


$ cat test1.d
import std.stdio;
int f() {
    writefln("f()");
    return 0;
}
void main() {
    int[1] a;
    a[f()] += 42L;
}
$ dmd test1.d -oftest1
gcc test1.o -o test1 -m32 -Xlinker -L/opt/dmd/bin/../lib -lphobos2 -lpthread
-lm
$ ./test1
f()
f()
$


If the line "a[f()] += 42L;" is replaced by "a[f()] = 42L;" or "a[f()] += 42;"
then f() is called only once.


-- 

November 23, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1685


casio_fifty@yahoo.com.hk changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         OS/Version|Linux                       |All




------- Comment #1 from casio_fifty@yahoo.com.hk  2007-11-23 08:37 -------
Can be reproduce on Windows with DMD too. Both v1.023 & v2.007 are affected.

It seems that the line is interpreted as

/*1*/    a[f()]
/*2*/ +  cast(int)42L
/*3*/ -> a[f()]

f() is evaluated twice only when a downward cast is needed, so replacing 42L by
42.0 will trigger the 2nd f(), but simpler types such as bool and char won't.

Also affects other *= methods, and associative arrays.


-- 

March 01, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1685





------- Comment #2 from i.kasiuk@gmx.de  2008-03-01 12:40 -------
The bug is still present in version 2.011.

It's especially dangerous with float arrays, e.g.

f[i++] += 0.1;

will result in i being incremented by 2 (and the wrong element of f[] being
incremented by 0.1).


-- 

December 17, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1685


baryluk@smp.if.uj.edu.pl changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |baryluk@smp.if.uj.edu.pl
           Severity|normal                      |critical




------- Comment #3 from baryluk@smp.if.uj.edu.pl  2008-12-17 13:06 -------
f[i++] += 0.1; example is really critical for "float[] f".  Please add this to DStress.

I have many codes in template versions (both for float and double), but most literals and functions returns double which then need implicit cast for floats. This bug makes version for floats very very broken code. I have no idea in what places this bug was silently corrupting my data.


-- 

December 25, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1685


bugzilla@digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED




------- Comment #4 from bugzilla@digitalmars.com  2008-12-25 04:37 -------
Fixed dmd 1.038 amd 2.022


--