From eda9528293fbc32857a5856a30ebd6585b281215 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Sat, 18 Nov 2023 16:41:44 -0500 Subject: [PATCH] libcdecl: Fully remove snprintf requirement from the library. There is only one remaining unconditional snprintf usage in the library, which implements the cdecl__strlcpy function. Replacing this with an alternate implementation means we no longer need snprintf at all except when NLS is enabled (in that case, we assume snprintf is available -- possibly provided by GNU libintl). So we can drop quite a lot of gnulib code potentially going into the library. The usual gnulib behaviour is to substitute complete re- implementations for almost any possible problem, even ones that are totally irrelevant to the usage at hand. Bumping dxcommon removes snprintf from the applications too so we can drop all of this nonsense. On systems that used the snprintf fallback, this reduces the overall library size by about 30%. --- bootstrap | 2 +- common | 2 +- m4/gnulib-cache.m4 | 2 -- src/output.c | 12 +++++++++--- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/bootstrap b/bootstrap index 5ec2dc4..ef46efa 100755 --- a/bootstrap +++ b/bootstrap @@ -33,7 +33,7 @@ if test -x $GNULIB/gnulib-tool; then exec 3>lib/symfiles.tmp 4&3 ) || die "gnulib-tool failed" shared_modules=`LC_ALL=C sort -u <&4` diff --git a/common b/common index 4081782..11dfda4 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 4081782832b7dc8b7426cedcec9a4495229dda6c +Subproject commit 11dfda46d8834955846372602441bbcec6a9047f diff --git a/m4/gnulib-cache.m4 b/m4/gnulib-cache.m4 index c970305..a246eab 100644 --- a/m4/gnulib-cache.m4 +++ b/m4/gnulib-cache.m4 @@ -49,7 +49,6 @@ # lock \ # mbswidth \ # readline \ -# snprintf \ # tls # Specification in the form of a few gnulib-tool.m4 macro invocations: @@ -63,7 +62,6 @@ gl_MODULES([ lock mbswidth readline - snprintf tls ]) gl_AVOID([gperf std-gnu11]) diff --git a/src/output.c b/src/output.c index fb80f99..c0438e6 100644 --- a/src/output.c +++ b/src/output.c @@ -38,9 +38,15 @@ size_t cdecl__advance(struct output_state *dst, size_t amount) size_t cdecl__strlcpy(char *dst, const char *src, size_t dstlen) { - if (dst) - snprintf(dst, dstlen, "%s", src); - return strlen(src); + size_t srclen = strlen(src); + + if (dstlen > 0) { + memcpy(dst, src, MIN(dstlen, srclen+1)); + if (dstlen <= srclen) + dst[dstlen-1] = 0; + } + + return srclen; } size_t cdecl__emit(struct output_state *dst, const char *src) -- 2.43.2