From 765c39bea005eaa49852ac1f877ed042ab5b68e6 Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Wed, 7 Nov 2018 15:10:40 -0500 Subject: Use our own memmem only when libc doesn’t have one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- configure.ac | 2 ++ src/c/memmem.c | 15 ++++++--------- src/c/memmem.h | 23 +++++++++++++++++++++++ src/c/request.c | 5 ++--- 4 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 src/c/memmem.h 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 #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 + +#else // !defined(HAVE_MEMMEM) + +#include + +/* + * 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 +#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; -- cgit v1.2.3