about summary refs log tree commit diff
path: root/src/string/memmem.c
diff options
context:
space:
mode:
authorTimo Teräs <timo.teras@iki.fi>2014-04-09 21:06:17 -0400
committerRich Felker <dalias@aerifal.cx>2014-04-09 21:06:17 -0400
commit6fbdeff0e51f6afc38fbb1476a4db81322779da4 (patch)
tree9f8efc8cfbdb13ec99fa396bdf68b1d304cedc30 /src/string/memmem.c
parente94d0692864ecf9522fd6a97610a47a2f718d3de (diff)
downloadmusl-6fbdeff0e51f6afc38fbb1476a4db81322779da4.tar.gz
musl-6fbdeff0e51f6afc38fbb1476a4db81322779da4.tar.xz
musl-6fbdeff0e51f6afc38fbb1476a4db81322779da4.zip
fix search past the end of haystack in memmem
to optimize the search, memchr is used to find the first occurrence of
the first character of the needle in the haystack before switching to
a search for the full needle. however, the number of characters
skipped by this first step were not subtracted from the haystack
length, causing memmem to search past the end of the haystack.
Diffstat (limited to 'src/string/memmem.c')
-rw-r--r--src/string/memmem.c1
1 files changed, 1 insertions, 0 deletions
diff --git a/src/string/memmem.c b/src/string/memmem.c
index 5211d759..a5a249f2 100644
--- a/src/string/memmem.c
+++ b/src/string/memmem.c
@@ -139,6 +139,7 @@ void *memmem(const void *h0, size_t k, const void *n0, size_t l)
 	/* Use faster algorithms for short needles */
 	h = memchr(h0, *n, k);
 	if (!h || l==1) return (void *)h;
+	k -= h - (const unsigned char *)h0;
 	if (l==2) return twobyte_memmem(h, k, n);
 	if (l==3) return threebyte_memmem(h, k, n);
 	if (l==4) return fourbyte_memmem(h, k, n);