fix false negatives with periodic needles in strstr, wcsstr, and memmem
authorRich Felker <dalias@aerifal.cx>
Fri, 18 Apr 2014 21:38:35 +0000 (17:38 -0400)
committerRich Felker <dalias@aerifal.cx>
Fri, 18 Apr 2014 21:38:35 +0000 (17:38 -0400)
in cases where the memorized match range from the right factor
exceeded the length of the left factor, it was wrongly treated as a
mismatch rather than a match.

issue reported by Yves Bastide.

src/string/memmem.c
src/string/strstr.c
src/string/wcsstr.c

index a5a249f..3b1ae18 100644 (file)
@@ -120,7 +120,7 @@ static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const
                }
                /* Compare left half */
                for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
-               if (k == mem) return (char *)h;
+               if (k <= mem) return (char *)h;
                h += p;
                mem = mem0;
        }
index 915c0a2..cd06912 100644 (file)
@@ -130,7 +130,7 @@ static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
                }
                /* Compare left half */
                for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
-               if (k == mem) return (char *)h;
+               if (k <= mem) return (char *)h;
                h += p;
                mem = mem0;
        }
index 3e28e28..4caaef3 100644 (file)
@@ -84,7 +84,7 @@ static wchar_t *twoway_wcsstr(const wchar_t *h, const wchar_t *n)
                }
                /* Compare left half */
                for (k=ms+1; k>mem && n[k-1] == h[k-1]; k--);
-               if (k == mem) return (wchar_t *)h;
+               if (k <= mem) return (wchar_t *)h;
                h += p;
                mem = mem0;
        }