Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!njin!princeton!notecnirp!nfs From: nfs@notecnirp.Princeton.EDU (Norbert Schlenker) Newsgroups: comp.os.minix Subject: "Bug" in strstr() and fix Message-ID: <19847@princeton.Princeton.EDU> Date: 4 Oct 89 11:13:51 GMT Sender: news@princeton.Princeton.EDU Reply-To: nfs@notecnirp.UUCP (Norbert Schlenker) Organization: Dept. of Computer Science, Princeton University Lines: 42 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); ! }