From: Gurusamy Sarathy <gsar@cpan.org>
Date: Thu, 30 Sep 1999 08:36:27 +0000 (+0000)
Subject: off-by-one in fbm_compile() (spotted by John Bley
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7506f9c3f6b048fdb2fd155faf2d2e318605503c;p=p5sagit%2Fp5-mst-13.2.git

off-by-one in fbm_compile() (spotted by John Bley
<jbley@cs.cmu.edu>); whitespace adjustments

p4raw-id: //depot/perl@4253
---

diff --git a/util.c b/util.c
index a92c4db..97401ab 100644
--- a/util.c
+++ b/util.c
@@ -912,7 +912,7 @@ Perl_mem_collxfrm(pTHX_ const char *s, STRLEN len, STRLEN *xlen)
    If FBMcf_TAIL, the table is created as if the string has a trailing \n. */
 
 void
-Perl_fbm_compile(pTHX_ SV *sv, U32 flags /* not used yet */)
+Perl_fbm_compile(pTHX_ SV *sv, U32 flags)
 {
     register U8 *s;
     register U8 *table;
@@ -928,23 +928,23 @@ Perl_fbm_compile(pTHX_ SV *sv, U32 flags /* not used yet */)
     if (len == 0)		/* TAIL might be on on a zero-length string. */
 	return;
     if (len > 2) {
-	I32 mlen = len;
+	U8 mlen;
 	unsigned char *sb;
 
-	if (mlen > 255)
+	if (len > 255)
 	    mlen = 255;
-	Sv_Grow(sv,len + 256 + FBM_TABLE_OFFSET);
+	else
+	    mlen = (U8)len;
+	Sv_Grow(sv, len + 256 + FBM_TABLE_OFFSET);
 	table = (unsigned char*)(SvPVX(sv) + len + FBM_TABLE_OFFSET);
-	s = table - 1 - FBM_TABLE_OFFSET; /* Last char */
-	for (i = 0; i < 256; i++) {
-	    table[i] = mlen;
-	}
-	table[-1] = flags;		/* Not used yet */
+	s = table - 1 - FBM_TABLE_OFFSET;	/* last char */
+	memset((void*)table, mlen, 256);
+	table[-1] = (U8)flags;
 	i = 0;
-	sb = s - mlen;
+	sb = s - mlen + 1;			/* first char (maybe) */
 	while (s >= sb) {
 	    if (table[*s] == mlen)
-		table[*s] = i;
+		table[*s] = (U8)i;
 	    s--, i++;
 	}
     }
@@ -963,7 +963,8 @@ Perl_fbm_compile(pTHX_ SV *sv, U32 flags /* not used yet */)
     BmUSEFUL(sv) = 100;			/* Initial value */
     if (flags & FBMcf_TAIL)
 	SvTAIL_on(sv);
-    DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",BmRARE(sv),BmPREVIOUS(sv)));
+    DEBUG_r(PerlIO_printf(Perl_debug_log, "rarest char %c at %d\n",
+			  BmRARE(sv),BmPREVIOUS(sv)));
 }
 
 /* If SvTAIL(littlestr), it has a fake '\n' at end. */
@@ -1075,15 +1076,17 @@ Perl_fbm_instr(pTHX_ unsigned char *big, register unsigned char *bigend, SV *lit
     }
     if (SvTAIL(littlestr) && !multiline) {	/* tail anchored? */
 	s = bigend - littlelen;
-	if (s >= big
-	    && bigend[-1] == '\n' 
-	    && *s == *little 
+	if (s >= big && bigend[-1] == '\n' && *s == *little 
 	    /* Automatically of length > 2 */
 	    && memEQ((char*)s + 1, (char*)little + 1, littlelen - 2))
+	{
 	    return (char*)s;		/* how sweet it is */
-	if (s[1] == *little && memEQ((char*)s + 2,(char*)little + 1,
-				     littlelen - 2))
+	}
+	if (s[1] == *little
+	    && memEQ((char*)s + 2, (char*)little + 1, littlelen - 2))
+	{
 	    return (char*)s + 1;	/* how sweet it is */
+	}
 	return Nullch;
     }
     if (SvTYPE(littlestr) != SVt_PVBM || !SvVALID(littlestr)) {
@@ -1093,9 +1096,11 @@ Perl_fbm_instr(pTHX_ unsigned char *big, register unsigned char *bigend, SV *lit
 	if (!b && SvTAIL(littlestr)) {	/* Automatically multiline!  */
 	    /* Chop \n from littlestr: */
 	    s = bigend - littlelen + 1;
-	    if (*s == *little && memEQ((char*)s + 1, (char*)little + 1,
-				       littlelen - 2))
+	    if (*s == *little
+		&& memEQ((char*)s + 1, (char*)little + 1, littlelen - 2))
+	    {
 		return (char*)s;
+	    }
 	    return Nullch;
 	}
 	return b;
@@ -1117,7 +1122,7 @@ Perl_fbm_instr(pTHX_ unsigned char *big, register unsigned char *bigend, SV *lit
 
 	  top2:
 	    /*SUPPRESS 560*/
-	    if (tmp = table[*s]) {
+	    if ((tmp = table[*s])) {
 #ifdef POINTERRIGOR
 		if (bigend - s > tmp) {
 		    s += tmp;