[Mono-list] Bug in CSC (or goto considered harmful)

Serge serge@wildwestsoftware.com
Tue, 6 Aug 2002 23:37:15 +0300


While trying to produce a simple testcase for a bug in Mono runtime (that bug is already fixed),
I came across the following bug in CSC (not MCS!).
See code below.
When compiled without optimizations (csc -o- -unsafe bug.cs) it will print "1" (which is correct).
Compiling it with optimizations enabled (csc -o+ -unsafe bug.cs), it will print "15" (which is wrong).
In the latter case "data" just optimized away.
Actually, by moving some statements around or adding more local vars
one could achieve even more impressive effects.
For example changing "int i = 0;" to "int i; i = 0;" would produce
unverifiable binary (ldloca refering to non-existent local variable).

Here is the code:

using System;
using System.Runtime.InteropServices;

class X {
    [StructLayout(LayoutKind.Explicit, Size = 64, Pack = 1)]
    struct FloatArray16 {}

    unsafe public static void Main() {
        FloatArray16 data;
        int i = 0;

        loop: ((float*)&data)[i] = i;
        if (++i < 16) goto loop;

        float* p = (float*)&data;