From 0138793ae6e49f70ab64d883810a5e05953a74ff Mon Sep 17 00:00:00 2001 From: Thenula Weerasinghe <167681802+programmer-1101@users.noreply.github.com> Date: Sat, 25 Apr 2026 15:20:17 +1000 Subject: [PATCH] Update VA_COPY macro to use va_copy function va_list isn't guarenteed to be a simple assignable type. In the case it's not #define VA_COPY(dest, src) (dest) = (src) produces undefined behaviour --- .cirrus.yml | 2 +- .github/workflows/ci.yml | 2 +- c89stringutils/c89stringutils_string_extras.c | 10 +++++++--- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.cirrus.yml b/.cirrus.yml index c3799ca..79c600f 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -1,5 +1,5 @@ freebsd_instance: - image_family: freebsd-13-0 + image_family: freebsd-14 task: install_script: pkg install -y cmake diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d33f9ec..84e398a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,7 +45,7 @@ jobs: EOF - name: Compile Test Amalgamation - run: gcc -Wall -Wextra -Werror -o test_amalg test_amalg.c + run: gcc -std=c89 -Wpedantic -Wall -Wextra -Werror -o test_amalg test_amalg.c - name: Run Test Amalgamation run: ./test_amalg diff --git a/c89stringutils/c89stringutils_string_extras.c b/c89stringutils/c89stringutils_string_extras.c index b24edda..1a5209e 100644 --- a/c89stringutils/c89stringutils_string_extras.c +++ b/c89stringutils/c89stringutils_string_extras.c @@ -103,8 +103,11 @@ char *strnstr(const char *buffer, const char *target, size_t bufferLength) { character of the first occurrence of little is returned. [this doc (c) FreeBSD <3 clause BSD license> from their manpage] */ - const size_t targetLength = strlen(target); + size_t targetLength; const char *start; + + /* Fix assignment of targetLength. C89 requires variables to be defined at beginning of a scope */ + targetLength = strlen(target); if (targetLength == 0) return (char *)buffer; for (start = buffer; *start && start + targetLength <= buffer + bufferLength; @@ -125,7 +128,8 @@ char *strnstr(const char *buffer, const char *target, size_t bufferLength) { /* `strcasestr` from MUSL */ char *strcasestr(const char *h, const char *n) { - const size_t l = strlen(n); + size_t l; + l = strlen(n); for (; *h; h++) if (!strncasecmp(h, n, l)) return (char *)h; @@ -192,7 +196,7 @@ size_t strerrorlen_s(errno_t errnum) { #ifdef HAVE___VA_COPY #define VA_COPY(dest, src) __va_copy(dest, src) #else -#define VA_COPY(dest, src) (dest) = (src) +#define VA_COPY(dest, src) memcpy(&(dest), &(src), sizeof(va_list)) #endif #endif #endif /* ! VA_COPY */