here's the test case.

        #include <u.h>
        #include <libc.h>

        void
        main(void)
        {
                int i;

                i = 0;
                i /= 1 / 100;
        }

which generates this output
        minooka; 8c ccbug.c
        8c 117738: suicide: sys: trap: divide error pc=0x33c0a
the real code used an
enumerated type instead of 100, making the
bug a bit hard to spot.

i've attached a diff and a testcase.  note that the divisor is set to something
bogus (but not zero) to allow additional diagnostics to be issued.  since diag()
prevents code generation, no bogus code will be emitted.

diffy -c /sys/src/cmd/cc/com.c
/n/dump/2011/0806/sys/src/cmd/cc/com.c:921,926 - /sys/src/cmd/cc/com.c:921,935
        case OASADD:
                ccom(l);
                ccom(r);
+               if(n->op == OASMOD || n->op == OASLMOD || n->op == OASDIV || 
n->op == OASLDIV)
+               if(r->op == OCONST)
+               if(r->vconst == 0) {
+                       if(n->op == OASMOD || n->op == OASLMOD)
+                               diag(n, "modulo by zero %d", n->op);
+                       else
+                               diag(n, "divide by zero");
+                       r->vconst = ~0;
+               }
                if(n->op == OASLSHR || n->op == OASASHR || n->op == OASASHL)
                if(r->op == OCONST) {
                        t = n->type->width * 8; /* bits per byte */

#include <u.h>
#include <libc.h>

void
main(void)
{
        int i;
        ulong u;
        uvlong v;

        i = 0;
        i /= 1 / 100;
        i %= 1 / 100;

        u = 0;
        u /= 1 / 100;
        u %= 1 / 100;

        v = 0;
        v /= 1 / 100;
        v %= 1/100;
}

- erik

Reply via email to