bug in bm

James Frew frew at ucsbcsl.UUCP
Wed Dec 4 04:49:01 AEST 1985


There is a bug in the "bm" program posted here a while back that causes
it to produce scrambled output when reading from a pipe (typically, the
first few characters of random output lines get eaten).  I haven't
tracked the bug down, but it appears to be due to the common, erroneous
assumption that UNIX pipes are true buffers; i.e., if you ask for N
bytes, your read will hang until n bytes appear.  Well, that ain't so -
on 4.2BSD a read on a pipe will return whenever there's SOMETHING in
the pipe - not necessarily as much as you asked for - and looking back
as far as version 7 I find nothing written that guarantees any other
sort of behavior.  Anyway, the bug in "bm" disappears when the read()
in Execute.c is changed to uread(), which is:
-----------------------------------cut here------------------------------------
/* uread - UNIX read, guaranteed to return nbytes unless error or EOF */

#define	ERROR	(-1)

uread(fd, buf, nbytes)
	int		fd;
	char		*buf;
	int		nbytes;
{
	register int	ngot;
	register int	nleft;

	for (nleft = nbytes; nleft > 0; nleft -= ngot) {
		ngot = read(fd, buf, nleft);

		if (ngot == ERROR) {
			return (ERROR);
		}

		if (ngot == 0) {
			break;
		}

		buf += ngot;
	}

	return (nbytes - nleft);
}
-- 
James Frew	ucbvax!ucsbcsl!frew
Computer Systems Lab., Univ. of Calif., Santa Barbara, CA 93106
(805) 961-2309



More information about the Comp.sources.bugs mailing list