i *= 0.5 revisited

Allen McIntosh aam at hcrvx1.UUCP
Thu Oct 18 07:11:13 AEST 1984


The System 5.2 C compiler for the VAX contains a bug "fix" for the infamous
"i *= 0.5" bug discussed recently on the net.  Alas, the fix introduces
yet another bug.  To demonstrate it, try running the following C program:

int a[2] = { 2, 9 };
main()
{
	int i;
	i = 0;
	a[i++] *= 0.5;
	printf("i = %d, a[0] = %d, a[1] = %d\n", i, a[0], a[1]);
	if( i != 1  ||  a[0] != 1 || a[2] != 9 )
		printf("Wrong.  Correct answer is i=1, a[0]=1, a[1]=9\n");
}

On our VAX running System 5.2, the output looks like this:

i = 2, a[0] = 2, a[1] = 1
Wrong.  Correct answer is i=1, a[0]=1, a[1]=9

The problem is the routine assign() added to local.c to fix the original
problem.  It blindly makes a copy of the left subtree of the "*=" without
checking for side effects.

Does anyone have a working fix for this that does not degrade code quality?



More information about the Comp.lang.c mailing list