[net.bugs.v7] Three cautionary tales

ken@rochester.UUCP (De onbekende kat) (10/05/85)

Here are three war stories from porting programs to a Pyramid 90X:

1. Program 1:

	short buf[3];
	...
	gtty(0, buf);
	...

	This caused a later call to getenv to fail with segmentation
	violation and a strange stack trace. Changing the program to read:

	struct sgttyb buf;
	...
	gtty(0, &buf);

	fixed the problem.

	Moral: Use system provided defines, don't try to "know" about
	the sizes of things. Also, symptoms can appear in unrelated places
	if the stack is messed up.

2. Program 2:

	char *malloc(n)
		unsigned n;
	{
		...
		n = (n + 1) & ~01;
		...
	}

	This caused a pointer dereference *much* later to fail with a
	bus error. The problem is that Pyramid requires word allocation
	to be aligned on 4 byte boundaries while the VAX is happy with
	2 byte boundaries. Changing the line to:

		n = (n + 3) & ~03;

	fixed the problem.

	Moral: If you are going to replace the highly system dependent
	storage allocation routines, make sure you annotate it or
	parameterize it. In the case above, to take my own advice,
	I should really have replaced it with something like:

		n = (n + WORDALIGNSIZE - 1) & ~(WORDALIGNSIZE - 1);

Program 3:

	struct blah *getpath(...)
		...
	{
		struct blah *work;
		...
		...
		work->field = foo;
	}

	This worked on the VAX by pure accident because work was the
	last thing left in r0. On the Pyramid a garbage pointer resulted.

	Moral: Pay attention when lint says: function blah has return
	and return(e). Do you have a reason for not always returning
	a value?

In all cases above, lint would have given a warning, except where
the malloc value was cast. Remember when you cast a pointer, you are
assuming responsibility for pointer compatibility.

None of this is to be construed as criticism of Pyramid 90X. On
any new machine, familiar assumptions not supported by the language
definition can fall flat.

I would love to hear more discussion of portability tales, if not
in this newsgroup, perhaps in a more appropriate group.

	Ken
-- 
UUCP: ..!{allegra,decvax,seismo}!rochester!ken ARPA: ken@rochester.arpa
USnail:	Dept. of Comp. Sci., U. of Rochester, NY 14627. Voice: Ken!