fix incorrect comparison loop condition in memmem
authorRich Felker <dalias@aerifal.cx>
Thu, 19 Jun 2014 04:42:28 +0000 (00:42 -0400)
committerRich Felker <dalias@aerifal.cx>
Thu, 19 Jun 2014 04:42:28 +0000 (00:42 -0400)
the logic for this loop was copied from null-terminated-string logic
in strstr without properly adapting it to work with explicit lengths.

presumably this error could result in false negatives (wrongly
comparing past the end of the needle/haystack), false positives
(stopping comparison early when the needle contains null bytes), and
crashes (from runaway reads past the end of mapped memory).

src/string/memmem.c

index 3b1ae18..d7e1221 100644 (file)
@@ -112,8 +112,8 @@ static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const
                }
 
                /* Compare right half */
-               for (k=MAX(ms+1,mem); n[k] && n[k] == h[k]; k++);
-               if (n[k]) {
+               for (k=MAX(ms+1,mem); k<l && n[k] == h[k]; k++);
+               if (k < l) {
                        h += k-ms;
                        mem = 0;
                        continue;