gout

A static git page generator
git clone https://git.bracken.jp/gout.git
Log | Files | Refs | README | LICENSE

commit 60a08e6740aa2d7f336fe17821063d411fff5631
parent 0c4663793cbeaf895e903e17fa10f5525cace9ae
Author: Chris Bracken <chris@bracken.jp>
Date:   Thu, 26 Feb 2026 09:13:28 +0900

build: improve usage of strlcpy, strlcat, reallocarray

Only include these functions where not already available. Where
included, instructs the linker to throw our version out if it's already
provided by the system.

Diffstat:
MBUILD.gn | 2--
Mthird_party/openbsd/BUILD.gn | 27+++++++++++++++++++--------
Mthird_party/openbsd/reallocarray.c | 6+-----
Mthird_party/openbsd/strlcat.c | 6+-----
Mthird_party/openbsd/strlcpy.c | 6+-----
5 files changed, 22 insertions(+), 25 deletions(-)

diff --git a/BUILD.gn b/BUILD.gn @@ -9,8 +9,6 @@ config("gout_config") { "//build:compiler_std", "//build:compiler_warnings", "//build:strict_prototypes", - "//build:no_exceptions", - "//build:no_rtti", "//build:posix_env", ] if (is_debug) { diff --git a/third_party/openbsd/BUILD.gn b/third_party/openbsd/BUILD.gn @@ -3,6 +3,9 @@ config("openbsd_config") { "//build:compiler_std", "//build:compiler_warnings", "//build:strict_prototypes", + "//build:no_exceptions", + "//build:no_rtti", + "//build:posix_env", ] if (is_debug) { public_configs += [ @@ -20,13 +23,21 @@ config("openbsd_config") { } source_set("openbsd") { - sources = [ - "reallocarray.c", - "reallocarray.h", - "strlcat.c", - "strlcat.h", - "strlcpy.c", - "strlcpy.h", - ] + sources = [] + + if (target_os == "linux" || target_os == "mac") { + sources += [ + "reallocarray.c", + "reallocarray.h", + ] + } + if (target_os == "linux") { + sources += [ + "strlcat.c", + "strlcat.h", + "strlcpy.c", + "strlcpy.h", + ] + } configs += [ ":openbsd_config" ] } diff --git a/third_party/openbsd/reallocarray.c b/third_party/openbsd/reallocarray.c @@ -21,15 +21,13 @@ #include "reallocarray.h" -#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && \ - !defined(__DragonFly__) - /* * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW */ #define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) +__attribute__((weak)) void* reallocarray(void* optr, size_t nmemb, size_t size) { if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && nmemb > 0 && SIZE_MAX / nmemb < size) { @@ -38,5 +36,3 @@ void* reallocarray(void* optr, size_t nmemb, size_t size) { } return realloc(optr, size * nmemb); } - -#endif diff --git a/third_party/openbsd/strlcat.c b/third_party/openbsd/strlcat.c @@ -21,9 +21,6 @@ #include "strlcat.h" -#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && \ - !defined(__DragonFly__) && !defined(__APPLE__) - /* * Appends src to string dst of size dsize (unlike strncat, dsize is the * full size of dst, not space left). At most dsize-1 characters @@ -31,6 +28,7 @@ * Returns strlen(src) + MIN(dsize, strlen(initial dst)). * If retval >= dsize, truncation occurred. */ +__attribute__((weak)) size_t strlcat(char* dst, const char* src, size_t dsize) { const char* odst = dst; const char* osrc = src; @@ -58,5 +56,3 @@ size_t strlcat(char* dst, const char* src, size_t dsize) { return (dlen + (src - osrc)); /* count does not include NUL */ } - -#endif diff --git a/third_party/openbsd/strlcpy.c b/third_party/openbsd/strlcpy.c @@ -20,14 +20,12 @@ #include "strlcpy.h" -#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && \ - !defined(__DragonFly__) && !defined(__APPLE__) - /* * Copy string src to buffer dst of size dsize. At most dsize-1 * chars will be copied. Always NUL terminates (unless dsize == 0). * Returns strlen(src); if retval >= dsize, truncation occurred. */ +__attribute__((weak)) size_t strlcpy(char* dst, const char* src, size_t dsize) { const char* osrc = src; size_t nleft = dsize; @@ -52,5 +50,3 @@ size_t strlcpy(char* dst, const char* src, size_t dsize) { return (src - osrc - 1); /* count does not include NUL */ } - -#endif