From mboxrd@z Thu Jan 1 00:00:00 1970 From: erik quanstrom Date: Sat, 6 Aug 2011 11:58:31 -0400 To: 9fans@9fans.net Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Subject: [9fans] c compiler bug, and fix Topicbox-Message-UUID: 0bf71e18-ead7-11e9-9d60-3106f5b1d025 here's the test case. #include #include 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 #include 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