From b389733ab0fa46f2cecd302cc5826fc7dc322c8e Mon Sep 17 00:00:00 2001 From: Randy Palamar Date: Sat, 6 Dec 2025 08:54:34 -0700 Subject: util: replace memrchr with internal version The amount of code we need to detect if this is present and handle the fallback is more than if we just provide it ourselves. Also we are passing in a difference of pointers so the argument type should be ptrdiff_t. This avoids a C brain damage of having unsigned size type which can wrap around if the caller is careful. --- Makefile | 5 +---- configure | 20 -------------------- test/core/Makefile | 8 ++++---- test/fuzz/Makefile | 6 +++--- text-iterator.c | 15 +++++++++++---- util.h | 11 ----------- 6 files changed, 19 insertions(+), 46 deletions(-) diff --git a/Makefile b/Makefile index bdc1ba0..269d45f 100644 --- a/Makefile +++ b/Makefile @@ -52,11 +52,8 @@ CFLAGS_STD ?= -std=c99 -U_XOPEN_SOURCE -D_XOPEN_SOURCE=700 -DNDEBUG -MMD CFLAGS_STD += -DVERSION=\"${VERSION}\" LDFLAGS_STD ?= -lc -CFLAGS_LIBC ?= -DHAVE_MEMRCHR=0 - CFLAGS_VIS = $(CFLAGS_AUTO) $(CFLAGS_TERMKEY) $(CFLAGS_CURSES) $(CFLAGS_ACL) \ - $(CFLAGS_SELINUX) $(CFLAGS_TRE) $(CFLAGS_LUA) $(CFLAGS_LPEG) $(CFLAGS_STD) \ - $(CFLAGS_LIBC) + $(CFLAGS_SELINUX) $(CFLAGS_TRE) $(CFLAGS_LUA) $(CFLAGS_LPEG) $(CFLAGS_STD) CFLAGS_VIS += -DVIS_PATH=\"${SHAREPREFIX}/vis\" CFLAGS_VIS += -DCONFIG_HELP=${CONFIG_HELP} diff --git a/configure b/configure index a70eac1..cc00cd7 100755 --- a/configure +++ b/configure @@ -613,25 +613,6 @@ EOF fi fi -printf "checking for memrchr... " - -cat > "$tmpc" < - -int main(int argc, char *argv[]) { - return !memrchr("\n", '\n', 1); -} -EOF - -if $CC $CFLAGS $CFLAGS_STD "$tmpc" $LDFLAGS -o "$tmpo" >/dev/null 2>&1; then - HAVE_MEMRCHR=1 - printf "%s\n" "yes" -else - HAVE_MEMRCHR=0 - printf "%s\n" "no" -fi - printf "completing config.mk... " exec 3>&1 1>>config.mk @@ -659,7 +640,6 @@ LDFLAGS_ACL = $LDFLAGS_ACL CONFIG_SELINUX = $CONFIG_SELINUX CFLAGS_SELINUX = $CFLAGS_SELINUX LDFLAGS_SELINUX = $LDFLAGS_SELINUX -CFLAGS_LIBC = -DHAVE_MEMRCHR=$HAVE_MEMRCHR EOF exec 1>&3 3>&- diff --git a/test/core/Makefile b/test/core/Makefile index b0284b8..5f1e965 100644 --- a/test/core/Makefile +++ b/test/core/Makefile @@ -16,19 +16,19 @@ config.h: text-test: config.h text-test.c ../../text.c ../../text-common.c ../../text-io.c ../../text-iterator.c ../../text-util.c ../../text-motions.c ../../text-objects.c ../../text-regex.c ../../array.c @echo Compiling $@ binary - @${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_LIBC} ${CFLAGS_EXTRA} ${filter %.c, $^} ${SRC} ${LDFLAGS} -o $@ + @${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_EXTRA} ${filter %.c, $^} ${SRC} ${LDFLAGS} -o $@ buffer-test: config.h buffer-test.c ../../buffer.c @echo Compiling $@ binary - @${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_LIBC} ${CFLAGS_EXTRA} buffer-test.c ${SRC} ${LDFLAGS} -o $@ + @${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_EXTRA} buffer-test.c ${SRC} ${LDFLAGS} -o $@ map-test: config.h map-test.c ../../map.c @echo Compiling $@ binary - @${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_LIBC} ${CFLAGS_EXTRA} ${filter %.c, $^} ${SRC} ${LDFLAGS} -o $@ + @${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_EXTRA} ${filter %.c, $^} ${SRC} ${LDFLAGS} -o $@ array-test: config.h array-test.c ../../array.c @echo Compiling $@ binary - @${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_LIBC} ${CFLAGS_EXTRA} ${filter %.c, $^} ${SRC} ${LDFLAGS} -o $@ + @${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_EXTRA} ${filter %.c, $^} ${SRC} ${LDFLAGS} -o $@ debug: clean $(MAKE) CFLAGS_EXTRA='${CFLAGS_EXTRA} ${CFLAGS_DEBUG}' diff --git a/test/fuzz/Makefile b/test/fuzz/Makefile index 9aade50..3b79ef1 100644 --- a/test/fuzz/Makefile +++ b/test/fuzz/Makefile @@ -10,15 +10,15 @@ test: $(ALL) text-fuzzer: text-fuzzer.c fuzzer.h $(TEXT_SRC) @echo Compiling $@ binary - @${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_LIBC} ${CFLAGS_EXTRA} ${filter %.c, $^} ${LDFLAGS} -o $@ + @${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_EXTRA} ${filter %.c, $^} ${LDFLAGS} -o $@ text-libfuzzer: text-fuzzer.c fuzzer.h $(TEXT_SRC) @echo Compiling $@ binary - @${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_LIBC} ${CFLAGS_EXTRA} -DLIBFUZZER ${filter %.c, $^} -fsanitize=fuzzer,address,undefined ${LDFLAGS} -o $@ + @${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_EXTRA} -DLIBFUZZER ${filter %.c, $^} -fsanitize=fuzzer,address,undefined ${LDFLAGS} -o $@ buffer-fuzzer: buffer-fuzzer.c fuzzer.h ../../buffer.c @echo Compiling $@ binary - @${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_LIBC} ${CFLAGS_EXTRA} ${filter %.c, $^} ${LDFLAGS} -o $@ + @${CC} ${CFLAGS} ${CFLAGS_STD} ${CFLAGS_EXTRA} ${filter %.c, $^} ${LDFLAGS} -o $@ debug: clean $(MAKE) CFLAGS_EXTRA='${CFLAGS_EXTRA} ${CFLAGS_DEBUG}' diff --git a/text-iterator.c b/text-iterator.c index e080ca5..b61c609 100644 --- a/text-iterator.c +++ b/text-iterator.c @@ -1,6 +1,3 @@ -#ifndef _GNU_SOURCE -#define _GNU_SOURCE /* memrchr(3) is non-standard */ -#endif #include #include #include @@ -72,9 +69,19 @@ bool text_iterator_byte_prev(Iterator *it, char *b) { return true; } +static void *scan_memory_reverse(const void *memory, int byte, ptrdiff_t n) +{ + void *result = 0; + if (n > 0) { + const signed char *s = memory; + while (n) if (s[--n] == byte) { result = (void *)(s + n); break; } + } + return result; +} + bool text_iterator_byte_find_prev(Iterator *it, char b) { while (it->text) { - const char *match = memrchr(it->start, b, it->text - it->start); + const char *match = scan_memory_reverse(it->start, b, it->text - it->start); if (match) { it->pos -= it->text - match; it->text = match; diff --git a/util.h b/util.h index 08a44a8..d84846a 100644 --- a/util.h +++ b/util.h @@ -23,17 +23,6 @@ static inline bool addu(size_t a, size_t b, size_t *c) { } #endif -#if !HAVE_MEMRCHR -/* MIT licensed implementation from musl libc */ -static void *memrchr(const void *m, int c, size_t n) -{ - const unsigned char *s = m; - c = (unsigned char)c; - while (n--) if (s[n]==c) return (void *)(s+n); - return 0; -} -#endif - /* Needed for building on GNU Hurd */ #ifndef PIPE_BUF -- cgit v1.2.3