9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] c compiler bug, and fix
@ 2011-08-06 15:58 erik quanstrom
  0 siblings, 0 replies; only message in thread
From: erik quanstrom @ 2011-08-06 15:58 UTC (permalink / raw)
  To: 9fans

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



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2011-08-06 15:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-06 15:58 [9fans] c compiler bug, and fix erik quanstrom

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).