]> git.draconx.ca Git - cdecl99.git/commitdiff
libcdecl: Avoid vsnprintf for error reporting.
authorNick Bowler <nbowler@draconx.ca>
Fri, 23 Jun 2023 05:11:15 +0000 (01:11 -0400)
committerNick Bowler <nbowler@draconx.ca>
Fri, 23 Jun 2023 05:17:49 +0000 (01:17 -0400)
Using vsnprintf is overkill here.  We only need to handle a single %s
conversion, which can be done by direct call to snprintf.

As this is the only caller of vsnprintf, dropping it means we can drop
the vsnprintf gnulib module.  Also, at least with gcc, using va_start
and such is fairly expensive.  The direct-snprintf version is quite
a bit more compact.

bootstrap
m4/gnulib-cache.m4
src/cdecl-internal.h
src/error.c
t/cdeclerr.c

index 8b6bf0b6a02d61e864bb0488b6a553ef7dd0f0e4..5ec2dc408e5a7e12b836435c7d6604b3d9d52e26 100755 (executable)
--- a/bootstrap
+++ b/bootstrap
@@ -33,7 +33,7 @@ if test -x $GNULIB/gnulib-tool; then
   exec 3>lib/symfiles.tmp 4<lib/symfiles.tmp
   rm -f lib/symfiles.tmp
 
-  shared_modules='lock tls snprintf vsnprintf'
+  shared_modules='lock tls snprintf'
   set x --extract-recursive-dependencies $shared_modules; shift
   (set -x; $GNULIB/gnulib-tool "$@" >&3 ) || die "gnulib-tool failed"
   shared_modules=`LC_ALL=C sort -u <&4`
index 6113698877cdfb828de20be04f3d244a4939068f..2b29495f30616c654937ee92354abbe5f8565991 100644 (file)
@@ -50,8 +50,7 @@
 #  mbswidth \
 #  readline \
 #  snprintf \
-#  tls \
-#  vsnprintf
+#  tls
 
 # Specification in the form of a few gnulib-tool.m4 macro invocations:
 gl_LOCAL_DIR([])
@@ -66,7 +65,6 @@ gl_MODULES([
   readline
   snprintf
   tls
-  vsnprintf
 ])
 gl_AVOID([gperf std-gnu11])
 gl_SOURCE_BASE([lib])
index ab6272590ed011573c5a2099a1ca4e3bbf516889..74c2654b76501016c2761d4138c7654fbdaa4994 100644 (file)
@@ -35,7 +35,7 @@ static inline void cdecl__init_i18n(void)
 }
 #endif
 
-void cdecl__err(unsigned code, const char *fmt, ...);
+void cdecl__err(unsigned code, const char *fmt, const char *arg);
 void cdecl__errmsg(unsigned msg);
 
 struct cdecl_declspec *cdecl__normalize_specs(struct cdecl_declspec *specs);
index 5466d393b05d03096e5b4dd36bb8fb6a8b045b6c..36e63666774d6a34cbf0ad99c0d14d06044adf85 100644 (file)
@@ -19,9 +19,7 @@
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
 #include <assert.h>
-#include <stdarg.h>
 
 #include "cdecl.h"
 #include "cdecl-internal.h"
@@ -124,23 +122,20 @@ void cdecl__errmsg(unsigned msg)
 }
 
 /*
- * Sets the library error to code, with a printf-style error string.
+ * Sets the library error to code; fmt is a printf-style string that may use
+ * up to one %s directive, to refer to arg.
  */
-void cdecl__err(unsigned code, const char *fmt, ...)
+void cdecl__err(unsigned code, const char *fmt, const char *arg)
 {
        struct err_state *state;
        int rc, try = 0;
-       va_list ap;
 
        state = get_err_state();
        if (!state)
                return;
 
 retry:
-       va_start(ap, fmt);
-       rc = vsnprintf(state->str, state->nstr, fmt, ap);
-       va_end(ap);
-
+       rc = snprintf(state->str, state->nstr, fmt, arg);
        if (rc > 0 && rc >= state->nstr) {
                assert(try++ == 0 && rc < SIZE_MAX / 4);
                state = alloc_err_state(state, (size_t)(rc+1u) * 3 / 2);
index 16ba1be809ea0b5abaf1071e72b0a40323430bf5..b8a9a741210cee508aef27bf494a9839779da643 100644 (file)
@@ -73,7 +73,7 @@ static void check_fixed_string(size_t len)
        memset(work2, 'X', len - 1);
 
        tap_diag("cdecl__err w/ %lu-byte string", (unsigned long)len);
-       cdecl__err(1234, work1);
+       cdecl__err(1234, work1, "");
        memset(work1, 0, len);
        err = cdecl_get_error();