aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorBenjamin Barenblat <bbaren@google.com>2018-11-07 15:10:40 -0500
committerBenjamin Barenblat <bbaren@google.com>2018-11-07 15:10:40 -0500
commit765c39bea005eaa49852ac1f877ed042ab5b68e6 (patch)
treecd73d7eea5432f111d04a4d182c839bf1a8c01c4
parent096fbda34b67cccd2026c44006bd9bc98d28c98c (diff)
Use our own memmem only when libc doesn’t have oneHEADmaster
Systems without memmem(3) are getting rarer every day. We can improve clarity and efficiency by relying on libc’s memmem whenever possible. Detect at compile time whether the system supports memmem(3); if it does, simply reexport its prototype through memmem.h and emit no code. If it doesn’t, actually build in the memmem code in memmem.c. Along the way, undo the renaming from commit 6dad7c645d8fdb7b7237c89ff7b34e90adbb86b1. Since we’re only creating a memmem prototype if libc doesn’t define the symbol, our prototype should never clash with libc’s. As before, authors should not assume string.h provides a prototype for memmem; they should `#include "memmem.h"` if they use the function.
-rw-r--r--configure.ac2
-rw-r--r--src/c/memmem.c15
-rw-r--r--src/c/memmem.h23
-rw-r--r--src/c/request.c5
4 files changed, 33 insertions, 12 deletions
diff --git a/configure.ac b/configure.ac
index 54eac40e..75bba230 100644
--- a/configure.ac
+++ b/configure.ac
@@ -111,6 +111,8 @@ pthread_t a;
AC_MSG_RESULT(yes),
AC_MSG_RESULT(no))
+AC_CHECK_FUNCS_ONCE([memmem])
+
AC_SUBST(CC)
AC_SUBST(BIN)
AC_SUBST(LIB)
diff --git a/src/c/memmem.c b/src/c/memmem.c
index f31f4e31..efddd0c1 100644
--- a/src/c/memmem.c
+++ b/src/c/memmem.c
@@ -1,4 +1,6 @@
-#include "config.h"
+#include "memmem.h"
+
+#ifndef HAVE_MEMMEM
/* $NetBSD$ */
@@ -38,8 +40,6 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
-// Function renamed by Adam Chlipala in 2016.
-
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD$");
@@ -54,13 +54,8 @@ __RCSID("$NetBSD$");
#define NULL ((char *)0)
#endif
-/*
- * urweb_memmem() returns the location of the first occurence of data
- * pattern b2 of size len2 in memory block b1 of size len1 or
- * NULL if none is found.
- */
void *
-urweb_memmem(const void *b1, size_t len1, const void *b2, size_t len2)
+memmem(const void *b1, size_t len1, const void *b2, size_t len2)
{
/* Sanity check */
if(!(b1 != NULL && b2 != NULL && len1 != 0 && len2 != 0))
@@ -85,3 +80,5 @@ urweb_memmem(const void *b1, size_t len1, const void *b2, size_t len2)
return NULL;
}
+
+#endif // !defined(HAVE_MEMMEM)
diff --git a/src/c/memmem.h b/src/c/memmem.h
new file mode 100644
index 00000000..0ddbb494
--- /dev/null
+++ b/src/c/memmem.h
@@ -0,0 +1,23 @@
+#ifndef URWEB_MEMMEM_H
+#define URWEB_MEMMEM_H
+
+#include "config.h"
+
+#ifdef HAVE_MEMMEM
+
+#include <string.h>
+
+#else // !defined(HAVE_MEMMEM)
+
+#include <stddef.h>
+
+/*
+ * memmem() returns the location of the first occurence of data
+ * pattern b2 of size len2 in memory block b1 of size len1 or
+ * NULL if none is found.
+ */
+void *memmem(const void *b1, size_t len1, const void *b2, size_t len2);
+
+#endif // !defined(HAVE_MEMMEM)
+
+#endif // URWEB_MEMMEM_H
diff --git a/src/c/request.c b/src/c/request.c
index 3e7ac34c..195b3cdc 100644
--- a/src/c/request.c
+++ b/src/c/request.c
@@ -11,13 +11,12 @@
#include <pthread.h>
+#include "memmem.h"
#include "urweb.h"
#include "request.h"
#define MAX_RETRIES 5
-void *urweb_memmem(const void *b1, size_t len1, const void *b2, size_t len2);
-
static int try_rollback(uw_context ctx, int will_retry, void *logger_data, uw_logger log_error) {
int r = uw_rollback(ctx, will_retry);
@@ -422,7 +421,7 @@ request_result uw_request(uw_request_context rc, uw_context ctx,
}
}
- part = urweb_memmem(after_sub_headers, body + body_len - after_sub_headers, boundary, boundary_len);
+ part = memmem(after_sub_headers, body + body_len - after_sub_headers, boundary, boundary_len);
if (!part) {
log_error(logger_data, "Missing boundary after multipart payload\n");
return FAILED;