[comp.os.minix] "Bug" in strstr

nfs@notecnirp.Princeton.EDU (Norbert Schlenker) (10/04/89)

There is a bug (actually a misfeature, since the comments seem to
indicate intention) in Henry Spencer's strstr() routine.  The most
recent draft of the ANSI C standard (in 4.11.5.7) says that, if the
second argument is a zero length string, strstr() should return its
first argument.  As written, the code returns a pointer to the zero
byte ending the first string.

The fix is enclosed below as a cdiff.

------------------------------  Cut here  -----------------------------
*** strstr.c.orig	Sun Oct  1 15:18:38 1989
--- strstr.c	Wed Oct  4 06:04:48 1989
***************
*** 16,29 ****
  	extern SIZET strlen();
  
  	/*
! 	 * The odd placement of the two tests is so "" is findable.
! 	 * Also, we inline the first char for speed.
! 	 * The ++ on scan has been moved down for optimization.
! 	 */
! 	firstc = *wanted;
! 	len = strlen(wanted);
! 	for (scan = s; *scan != firstc || strncmp(scan, wanted, len) != 0; )
! 		if (*scan++ == '\0')
! 			return(NULL);
! 	return(scan);
! }
--- 16,28 ----
  	extern SIZET strlen();
  
  	/*
! 	 * We inline the first char for speed.
! 	 */
! 	if ((firstc = *wanted) == '\0')
! 		return(s);
! 	len = strlen(wanted);
! 	for (scan = s; *scan != '\0'; scan++)
! 		if (*scan == firstc && strncmp(scan, wanted, len) == 0)
! 			return(scan);
! 	return(NULL);
! }

henry@utzoo.uucp (Henry Spencer) (10/05/89)

In article <19847@princeton.Princeton.EDU> nfs@notecnirp.UUCP (Norbert Schlenker) writes:
>There is a bug (actually a misfeature, since the comments seem to
>indicate intention) in Henry Spencer's strstr() routine.  The most
>recent draft of the ANSI C standard (in 4.11.5.7) says...

Note well that my string functions were built to match an early draft
of ANSI C, and have not been updated.  (An update is now in the works.)
-- 
Nature is blind; Man is merely |     Henry Spencer at U of Toronto Zoology
shortsighted (and improving).  | uunet!attcan!utzoo!henry henry@zoo.toronto.edu