Bug in "comm" utility

Maarten Litmaath maart at cs.vu.nl
Thu Jun 15 05:00:35 AEST 1989


gwyn at smoke.BRL.MIL (Doug Gwyn) writes:
[quick kludge deleted, sorry Doug]

Why didn't you change compare()? The new version should be faster too!

/*
 * old version
 */
compare(a,b)
char *a,*b;
{
	register char *ra,*rb;

	/*
	 * it's non-portable to move a pointer one position before the begin
	 * of the array: on segmented architectures &array[0] could be the
	 * start of a segment
	 */
	ra = --a;
	rb = --b;
	while(*++ra == *++rb)		/* pre-increment instructions?! */
		if(*ra == '\0')
			return(0);
	if(*ra < *rb)
		return(1);
	return(2);
}


/*
 * new version - according to the ANSI standard a pointer may be moved one
 * position PAST the end of the array
 */
int	compare(a, b)
char	*a, *b;
{
	register char	*ra, *rb;

	ra = a;
	rb = b;

	while (*ra == *rb++)		/* post-increment instructions!! */
		if (!*ra++)
			return 0;

	return *ra < *--rb ? 1 : 2;
}
-- 
"I HATE arbitrary limits, especially when |Maarten Litmaath @ VU Amsterdam:
   they're small."  (Stephen Savitzky)    |maart at cs.vu.nl, mcvax!botter!maart



More information about the Comp.bugs.4bsd.ucb-fixes mailing list