summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2014-05-27 20:55:33 -0700
committerAlan Coopersmith <alan.coopersmith@oracle.com>2014-05-27 20:55:37 -0700
commitbfb6044479e8e974e848f47969f75b84865fea13 (patch)
treed8631e32a64744389b9c6e2291ff901f771c0745
parent7848ba1d292f55553058c1f17596baa1094cdae5 (diff)
Replace several malloc+sprintf pairs with asprintf() calls
Includes fallback uAsprintf() for systems without asprintf yet Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--configure.ac4
-rw-r--r--psgeom.c12
-rw-r--r--utils.c47
-rw-r--r--utils.h6
-rw-r--r--xkbprint.c7
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 <ctype.h>
#include <stdlib.h>
@@ -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 <stdio.h>
#include <ctype.h>
#include <X11/Xlocale.h>
@@ -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;