diff options
author | Werner Koch <wk@gnupg.org> | 2015-11-03 15:26:29 +0100 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2015-11-03 15:26:36 +0100 |
commit | 7279c3ce9283723bd7f670051cd8a2087f62cd7f (patch) | |
tree | e00a080f418b97f0d2965c305a2c3b21cdff8753 /src | |
parent | dda9c84bc67326a6d2bef1e5c45b5de8b67eeb40 (diff) | |
download | libassuan-7279c3ce9283723bd7f670051cd8a2087f62cd7f.tar.gz |
Use asprintf function from libgpg-error.
* configure.ac: Require libgpg-error 1.17.
(vasprintf): Remove ac_replace.
* src/assuan-defs.h [!HAVE_VASPRINTF]: Remove replace wrapper.
* src/assuan-logging.c (_assuan_log_control_channel):
(_assuan_log_control_channel): s/asprintf/gpgrt_asprintf/.
* src/debug.c (_assuan_debug): s/vasprintf/gpgrt_vasprintf/.
(_assuan_debug_begin): Ditto.
(_assuan_debug_add): Ditto. s/asprintf/gpgrt_asprintf/.
--
We plan to move more code into libgpg-error/gpgrt anyway thus this is
a good start which also fixes this bug:
GnuPG-bug-id: 1477
Signed-off-by: Werner Koch <wk@gnupg.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/assuan-defs.h | 6 | ||||
-rw-r--r-- | src/assuan-logging.c | 14 | ||||
-rw-r--r-- | src/debug.c | 8 | ||||
-rw-r--r-- | src/vasprintf.c | 211 |
4 files changed, 11 insertions, 228 deletions
diff --git a/src/assuan-defs.h b/src/assuan-defs.h index cf0015e..97b053d 100644 --- a/src/assuan-defs.h +++ b/src/assuan-defs.h @@ -394,12 +394,6 @@ int setenv (const char *name, const char *value, int replace); #ifndef HAVE_PUTC_UNLOCKED int putc_unlocked (int c, FILE *stream); #endif -#ifndef HAVE_VASPRINTF -int _assuan_vasprintf (char **result, const char *format, va_list args); -int _assuan_asprintf (char **buf, const char *fmt, ...); -#define vasprintf _assuan_vasprintf -#define asprintf _assuan_asprintf -#endif #define DIM(v) (sizeof(v)/sizeof((v)[0])) diff --git a/src/assuan-logging.c b/src/assuan-logging.c index d74e5d2..ca87488 100644 --- a/src/assuan-logging.c +++ b/src/assuan-logging.c @@ -28,7 +28,7 @@ #ifdef HAVE_W32_SYSTEM # ifdef HAVE_WINSOCK2_H # include <winsock2.h> -# endif +# endif # include <windows.h> #endif /*HAVE_W32_SYSTEM*/ #include <errno.h> @@ -147,7 +147,7 @@ _assuan_log_handler (assuan_context_t ctx, void *hook, unsigned int cat, fprintf (fp, "%s", msg); /* If the log stream is a file, the output would be buffered. This is bad for debugging, thus we flush the stream if FORMAT ends - with a LF. */ + with a LF. */ if (msg && *msg && msg[strlen (msg) - 1] == '\n') fflush (fp); gpg_err_set_errno (saved_errno); @@ -174,7 +174,7 @@ _assuan_log_control_channel (assuan_context_t ctx, int outbound, /* Check whether logging is enabled and do a quick check to see whether the callback supports our category. */ if (!ctx - || !ctx->log_cb + || !ctx->log_cb || ctx->flags.no_logging || !(*ctx->log_cb) (ctx, ctx->log_cb_data, ASSUAN_LOG_CONTROL, NULL)) return; @@ -204,8 +204,8 @@ _assuan_log_control_channel (assuan_context_t ctx, int outbound, if (string) { /* Print the diagnostic. */ - res = asprintf (&outbuf, "chan_" CHANNEL_FMT " %s [%s]\n", - ctx->inbound.fd, outbound? "->":"<-", string); + res = gpgrt_asprintf (&outbuf, "chan_" CHANNEL_FMT " %s [%s]\n", + ctx->inbound.fd, outbound? "->":"<-", string); } else if (buffer1) { @@ -231,7 +231,7 @@ _assuan_log_control_channel (assuan_context_t ctx, int outbound, { /* No control characters and not starting with our error message indicator. Log it verbatim. */ - res = asprintf (&outbuf, "chan_" CHANNEL_FMT " %s %.*s%.*s\n", + res = gpgrt_asprintf (&outbuf, "chan_" CHANNEL_FMT " %s %.*s%.*s\n", ctx->inbound.fd, outbound? "->":"<-", (int)length1, (const char*)buffer1, (int)length2, buffer2? (const char*)buffer2:""); @@ -246,7 +246,7 @@ _assuan_log_control_channel (assuan_context_t ctx, int outbound, char *hp; unsigned int nbytes; unsigned int maxbytes = full_logging? (2*LINELENGTH) : 16; - + nbytes = length1 + length2; if (nbytes > maxbytes) nbytes = maxbytes; diff --git a/src/debug.c b/src/debug.c index d6c3e80..9ef6237 100644 --- a/src/debug.c +++ b/src/debug.c @@ -60,7 +60,7 @@ _assuan_debug (assuan_context_t ctx, unsigned int cat, const char *format, ...) saved_errno = errno; va_start (arg_ptr, format); - res = vasprintf (&msg, format, arg_ptr); + res = gpgrt_vasprintf (&msg, format, arg_ptr); va_end (arg_ptr); if (res < 0) return; @@ -87,7 +87,7 @@ _assuan_debug_begin (assuan_context_t ctx, return; va_start (arg_ptr, format); - res = vasprintf ((char **) line, format, arg_ptr); + res = gpgrt_vasprintf ((char **) line, format, arg_ptr); va_end (arg_ptr); if (res < 0) *line = NULL; @@ -107,14 +107,14 @@ _assuan_debug_add (assuan_context_t ctx, void **line, const char *format, ...) return; va_start (arg_ptr, format); - res = vasprintf (&toadd, format, arg_ptr); + res = gpgrt_vasprintf (&toadd, format, arg_ptr); va_end (arg_ptr); if (res < 0) { free (*line); *line = NULL; } - res = asprintf (&result, "%s%s", *(char **) line, toadd); + res = gpgrt_asprintf (&result, "%s%s", *(char **) line, toadd); free (toadd); free (*line); if (res < 0) diff --git a/src/vasprintf.c b/src/vasprintf.c deleted file mode 100644 index bacb71c..0000000 --- a/src/vasprintf.c +++ /dev/null @@ -1,211 +0,0 @@ -/* Like vsprintf but provides a pointer to malloc'd storage, which must - be freed by the caller. - Copyright (C) 1994, 2002 Free Software Foundation, Inc. - -This file is part of the libiberty library. -Libiberty is free software; you can redistribute it and/or -modify it under the terms of the GNU Library General Public -License as published by the Free Software Foundation; either -version 2 of the License, or (at your option) any later version. - -Libiberty is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -Library General Public License for more details. - -You should have received a copy of the GNU Library General Public -License along with libiberty; see the file COPYING.LIB. If -not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, -Boston, MA 02111-1307, USA. */ - -#ifdef HAVE_CONFIG_H -#include <config.h> -#endif -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <stdarg.h> - - -#ifndef va_copy /* According to POSIX, va_copy is a macro. */ -#if defined (__GNUC__) && defined (__PPC__) \ - && (defined (_CALL_SYSV) || defined (_WIN32)) -#define va_copy(d, s) (*(d) = *(s)) -#elif defined (MUST_COPY_VA_BYVAL) -#define va_copy(d, s) ((d) = (s)) -#else -#define va_copy(d, s) memcpy ((d), (s), sizeof (va_list)) -#endif -#endif - - -#ifdef TEST -int global_total_width; -#endif - -static int int_vasprintf (char **, const char *, va_list *); - -static int -int_vasprintf (result, format, args) - char **result; - const char *format; - va_list *args; -{ -#if defined(HAVE_W32CE_SYSTEM) && defined(_MSC_VER) - /* No va_copy and the replacement above doesn't work. */ -#define MAX_STRLEN 256 - *result = malloc (MAX_STRLEN); - if (*result != NULL) - { - int res = _vsnprintf (*result, MAX_STRLEN, format, *args); - (*result)[MAX_STRLEN - 1] = '\0'; - return res; - } - else - return 0; -#else - const char *p = format; - /* Add one to make sure that it is never zero, which might cause malloc - to return NULL. */ - int total_width = strlen (format) + 1; - va_list ap; - - va_copy (ap, *args); - - while (*p != '\0') - { - if (*p++ == '%') - { - while (strchr ("-+ #0", *p)) - ++p; - if (*p == '*') - { - ++p; - total_width += abs (va_arg (ap, int)); - } - else - total_width += strtoul (p, (char **) &p, 10); - if (*p == '.') - { - ++p; - if (*p == '*') - { - ++p; - total_width += abs (va_arg (ap, int)); - } - else - total_width += strtoul (p, (char **) &p, 10); - } - while (strchr ("hlL", *p)) - ++p; - /* Should be big enough for any format specifier except %s and floats. */ - total_width += 30; - switch (*p) - { - case 'd': - case 'i': - case 'o': - case 'u': - case 'x': - case 'X': - case 'c': - (void) va_arg (ap, int); - break; - case 'f': - case 'e': - case 'E': - case 'g': - case 'G': - (void) va_arg (ap, double); - /* Since an ieee double can have an exponent of 307, we'll - make the buffer wide enough to cover the gross case. */ - total_width += 307; - break; - case 's': - { - char *tmp = va_arg (ap, char *); - if (tmp) - total_width += strlen (tmp); - else /* in case the vsprintf does prints a text */ - total_width += 25; /* e.g. "(null pointer reference)" */ - } - break; - case 'p': - case 'n': - (void) va_arg (ap, char *); - break; - } - p++; - } - } -#ifdef TEST - global_total_width = total_width; -#endif - *result = malloc (total_width); - if (*result != NULL) - return vsprintf (*result, format, *args); - else - return 0; -#endif -} - - -/* We use the _assuan prefix to keep our name space clean. */ -int -_assuan_vasprintf (char **result, const char *format, va_list args) -{ - return int_vasprintf (result, format, &args); -} - - -int -_assuan_asprintf (char **buf, const char *fmt, ...) -{ - int status; - va_list ap; - - va_start (ap, fmt); - status = _assuan_vasprintf (buf, fmt, ap); - va_end (ap); - return status; -} - - -#ifdef TEST - -#define asprintf _assuan_asprintf -#define vasprintf _assuan_vasprintf -static int any_failed; - -void -checkit (const char* format, ...) -{ - va_list args; - char *result; - - va_start (args, format); - vasprintf (&result, format, args); - if (strlen (result) < global_total_width) - printf ("PASS: "); - else - { - any_failed = 1; - printf ("FAIL: "); - } - printf ("%d %s\n", global_total_width, result); -} - -int -main (void) -{ - checkit ("%d", 0x12345678); - checkit ("%200d", 5); - checkit ("%.300d", 6); - checkit ("%100.150d", 7); - checkit ("%s", "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\ -777777777777777777333333333333366666666666622222222222777777777777733333"); - checkit ("%f%s%d%s", 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"); - checkit ("%2$f%4$s%3$d%1$s", "asdjffffffffffffffiiiiiiiiiiixxxxx", 1.0, 77, "foo"); - return any_failed; -} -#endif /* TEST */ |