www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1685] New: Array index is evaluated twice

reply d-bugmail puremagic.com writes:
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.


-- 
Nov 22 2007
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1685


casio_fifty yahoo.com.hk changed:

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





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.


-- 
Nov 23 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1685






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).


-- 
Mar 01 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
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





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.


-- 
Dec 17 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1685


bugzilla digitalmars.com changed:

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





Fixed dmd 1.038 amd 2.022


-- 
Dec 25 2008