switch.and.case

Jim Crammond jim at hwcs.UUCP
Thu Jun 14 23:33:46 AEST 1984


We have a 32-bit "Orion" computer made by High Level Hardware and
I discovered a compiler bug when trying to put sendmail on it:

The offending C code was a switch statement in parseaddr.c of the form -

	switch (*rp)
	{
		register STAB *s;

	case MATCHCLASS:
	case MATCHNCLASS:
		s = stab(ap, ST_CLASS, ST_FIND);
		.......

This got compiled to something like:

	jump to the case selection code		/* switch */
	increase the local stack size		/* declaration */
label1:
label2: code for calling stab 			/* first case */
	.....
	case selection code
	.....

Anything (declarations or code) above the first case statement gets jumped over,
never to be executed. Thus the "increase the stack size" instruction
(n.b. its microprogrammed as a stack machine) was never executed and this had
the side effect that if the first statement of any case statement was a function
call, the first argument was lost and the second became the first etc.

Thus in the above example stab was called as "stab(ST_CLASS, ST_FIND, garbage)"

I would say that a declaration such as that above perhaps makes sense,
though I always prefer declarations to be at the beginning of routines,
but putting code before the first case is decidedly bad style.

It took a long time to track down this bug, compiler bugs are hard to find!
But this kind of code is obscure so that it's not really surprising that
no one had spotted it before.

If people avoided the type of code where you have to delve into the back of
K+R to find out its meaning, then a lot of time could be saved by others
who are trying to port it.

	- Jim Crammond

p.s. The Orion does not support signed characters - please don't use them
     in any code you want to distribute.



More information about the Comp.lang.c mailing list