From bfb6044479e8e974e848f47969f75b84865fea13 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Tue, 27 May 2014 20:55:33 -0700 Subject: Replace several malloc+sprintf pairs with asprintf() calls Includes fallback uAsprintf() for systems without asprintf yet Signed-off-by: Alan Coopersmith --- configure.ac | 4 ++++ psgeom.c | 12 ++++++------ utils.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ utils.h | 6 ++++++ xkbprint.c | 7 +++++-- 5 files changed, 68 insertions(+), 8 deletions(-) diff --git a/configure.ac b/configure.ac index 66db16a..c1ad70f 100644 --- a/configure.ac +++ b/configure.ac @@ -27,6 +27,8 @@ AC_INIT([xkbprint], [1.0.3], [https://bugs.freedesktop.org/enter_bug.cgi?product=xorg], [xkbprint]) AC_CONFIG_SRCDIR([Makefile.am]) AC_CONFIG_HEADERS([config.h]) +# Needed for asprintf on GNU libc +AC_USE_SYSTEM_EXTENSIONS # Initialize Automake AM_INIT_AUTOMAKE([foreign dist-bzip2]) @@ -38,6 +40,8 @@ m4_ifndef([XORG_MACROS_VERSION], XORG_MACROS_VERSION(1.8) XORG_DEFAULT_OPTIONS +AC_CHECK_FUNCS([asprintf]) + # Checks for pkg-config packages PKG_CHECK_MODULES(XKBPRINT, [xkbfile x11 xproto >= 7.0.17]) diff --git a/psgeom.c b/psgeom.c index 4ec1347..94c178a 100644 --- a/psgeom.c +++ b/psgeom.c @@ -26,6 +26,10 @@ ********************************************************/ /* $XFree86: xc/programs/xkbprint/psgeom.c,v 1.5 2001/07/25 15:05:25 dawes Exp $ */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #define XK_TECHNICAL #define XK_PUBLISHING #define XK_KATAKANA @@ -869,11 +873,9 @@ PSPageTrailer(FILE *out, PSState *state) if (sName == NULL) sName = "(unknown)"; - lbuf = malloc(10 + strlen(sName)); - if (!lbuf) { + if (asprintf(&lbuf, "Layout: %s", sName) == -1) { uFatalError("Can't allocate memory for string\n"); } - sprintf(lbuf, "Layout: %s", sName); fprintf(out, "kbx kbdscalewidth 0 (%s) centeroffset pop add\n", lbuf); fprintf(out, " kby kbdscaleheight add %d add\n", baseline); @@ -904,11 +906,9 @@ PSPageTrailer(FILE *out, PSState *state) if (sName == NULL) sName = "(unknown)"; - lbuf = malloc(12 + strlen(sName)); - if (!lbuf) { + if (asprintf(&lbuf, "Keycodes: %s", sName) == -1) { uFatalError("Can't allocate memory for string\n"); } - sprintf(lbuf, "Keycodes: %s", sName); fprintf(out, "kbx kbdscalewidth 0 (%s) centeroffset pop add\n", lbuf); fprintf(out, " kby kbdscaleheight add %d add\n", baseline); diff --git a/utils.c b/utils.c index 7223a62..0c798a6 100644 --- a/utils.c +++ b/utils.c @@ -27,6 +27,10 @@ \*/ /* $XFree86: xc/programs/xkbprint/utils.c,v 3.4 2001/01/17 23:46:11 dawes Exp $ */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include "utils.h" #include #include @@ -144,3 +148,46 @@ uInternalError(const char *s, ...) va_end(ap); return; } + +/***====================================================================***/ + +#ifndef HAVE_ASPRINTF +int +uAsprintf(char ** ret, const char *format, ...) +{ + char buf[256]; + int len; + va_list ap; + + va_start(ap, format); + len = vsnprintf(buf, sizeof(buf), format, ap); + va_end(ap); + + if (len < 0) + return -1; + + if (len < sizeof(buf)) + { + *ret = strdup(buf); + } + else + { + *ret = malloc(len + 1); /* snprintf doesn't count trailing '\0' */ + if (*ret != NULL) + { + va_start(ap, format); + len = vsnprintf(*ret, len + 1, format, ap); + va_end(ap); + if (len < 0) { + free(*ret); + *ret = NULL; + } + } + } + + if (*ret == NULL) + return -1; + + return len; +} +#endif /* HAVE_ASPRINTF */ diff --git a/utils.h b/utils.h index f37c8df..82c8c6b 100644 --- a/utils.h +++ b/utils.h @@ -95,6 +95,12 @@ extern void uInternalError(const char *s, ...) _X_ATTRIBUTE_PRINTF(1, 2); #define uStringCompare(s1,s2) (strcmp(s1,s2)) #define uStrCaseEqual(s1,s2) (strcasecmp(s1,s2)==0) +#ifndef HAVE_ASPRINTF +extern _X_HIDDEN int _X_ATTRIBUTE_PRINTF(2,3) + uAsprintf(char ** ret, const char *format, ...); +#define asprintf uAsprintf +#endif + /***====================================================================***/ _XFUNCPROTOEND diff --git a/xkbprint.c b/xkbprint.c index 41ebec3..fe2219d 100644 --- a/xkbprint.c +++ b/xkbprint.c @@ -26,6 +26,10 @@ ********************************************************/ /* $XFree86: xc/programs/xkbprint/xkbprint.c,v 3.10 2003/05/27 22:27:07 tsi Exp $ */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include #include #include @@ -421,8 +425,7 @@ parseArgs(int argc, char *argv[]) FILE *file = NULL; if (outputFile == NULL) { - outputFile = malloc(strlen(outputFont) + 5); - sprintf(outputFile, "%s.pfa", outputFont); + asprintf(&outputFile, "%s.pfa", outputFont); } else if (uStringEqual(outputFile, "-")) file = stdout; -- cgit v1.2.3