summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoremaste <emaste@FreeBSD.org>2015-05-26 21:16:07 +0000
committeremaste <emaste@FreeBSD.org>2015-05-26 21:16:07 +0000
commitd45a21328b20ce30a629a8ae4a01e8e3434e7000 (patch)
tree85b8aed82ce3e7f57eb4b089de73b2bf3c567b5f
parent2f93a7904f7b9a0fb327e9a909adad7c177df8e5 (diff)
downloadFreeBSD-src-d45a21328b20ce30a629a8ae4a01e8e3434e7000.zip
FreeBSD-src-d45a21328b20ce30a629a8ae4a01e8e3434e7000.tar.gz
memmem(3): empty little string matches the beginning of the big string
This function originated in glibc, and this matches their behaviour (and NetBSD, OpenBSD, and musl). An empty big string (arg "l") is handled by the existing l_len < s_len test. Reviewed by: bapt, ngie Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D2657
-rw-r--r--contrib/netbsd-tests/lib/libc/string/t_memmem.c2
-rw-r--r--lib/libc/string/memmem.322
-rw-r--r--lib/libc/string/memmem.c6
3 files changed, 18 insertions, 12 deletions
diff --git a/contrib/netbsd-tests/lib/libc/string/t_memmem.c b/contrib/netbsd-tests/lib/libc/string/t_memmem.c
index 8734bc3..5807662 100644
--- a/contrib/netbsd-tests/lib/libc/string/t_memmem.c
+++ b/contrib/netbsd-tests/lib/libc/string/t_memmem.c
@@ -75,7 +75,7 @@ ATF_TC_HEAD(memmem_basic, tc)
ATF_TC_BODY(memmem_basic, tc)
{
-#if defined(__darwin__) || defined(__FreeBSD__)
+#if defined(__darwin__)
expect(memmem(b2, lb2, p0, lp0) == NULL);
expect(memmem(b0, lb0, p0, lp0) == NULL);
#else
diff --git a/lib/libc/string/memmem.3 b/lib/libc/string/memmem.3
index 31cdf77..a0db94f 100644
--- a/lib/libc/string/memmem.3
+++ b/lib/libc/string/memmem.3
@@ -26,7 +26,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd August 24, 2005
+.Dd May 26, 2015
.Dt MEMMEM 3
.Os
.Sh NAME
@@ -51,14 +51,12 @@ in the byte string
.Fa big .
.Sh RETURN VALUES
If
-.Fa big_len
-is smaller than
-.Fa little_len ,
-if
.Fa little_len
-is 0, if
-.Fa big_len
-is 0 or if
+is zero
+.Fa big
+is returned (that is, an empty little is deemed to match at the beginning of
+big);
+if
.Fa little
occurs nowhere in
.Fa big ,
@@ -84,3 +82,11 @@ function first appeared in
.Sh BUGS
This function was broken in Linux libc up to and including version 5.0.9
and in GNU libc prior to version 2.1.
+Prior to
+.Fx 11.0
+.Nm
+returned
+.Dv NULL
+when
+.Fa little_len
+equals 0.
diff --git a/lib/libc/string/memmem.c b/lib/libc/string/memmem.c
index 72e6517..3fd05df 100644
--- a/lib/libc/string/memmem.c
+++ b/lib/libc/string/memmem.c
@@ -42,9 +42,9 @@ memmem(const void *l, size_t l_len, const void *s, size_t s_len)
const char *cl = (const char *)l;
const char *cs = (const char *)s;
- /* we need something to compare */
- if (l_len == 0 || s_len == 0)
- return NULL;
+ /* empty "s" matches the beginning of "l" */
+ if (s_len == 0)
+ return (void *)cl;
/* "s" must be smaller or equal to "l" */
if (l_len < s_len)
OpenPOWER on IntegriCloud