summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2011-01-07 19:29:03 -0800
committerAlan Coopersmith <alan.coopersmith@oracle.com>2011-01-07 19:29:09 -0800
commit78b8e6b772685a2ed567ac2d30f96116f050dad5 (patch)
tree3879eb292db06f4817aacf2e433bea6425f01a6a
parentdc63c253e33b1012c08cd274e6e37bf0fa87c624 (diff)
Resync fallback asprintf with updated version put into xrdb
Fixes issues found during xrdb review on xorg-devel Also make sure <stdarg.h> is #included for varargs macros Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--src/process.c22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/process.c b/src/process.c
index 4aceafa..6cde622 100644
--- a/src/process.c
+++ b/src/process.c
@@ -68,9 +68,11 @@ Author: Ralph Mor, X Consortium
}
#ifndef HAVE_ASPRINTF
-/* sprintf variant found in newer libc's that allocates string to print to */
-static int
-asprintf(char ** ret, const char *format, ...) _X_ATTRIBUTE_PRINTF(2,3)
+# include <stdarg.h>
+
+/* sprintf variant found in newer libc's which allocates string to print to */
+static int _X_ATTRIBUTE_PRINTF(2,3)
+asprintf(char ** ret, const char *format, ...)
{
char buf[256];
int len;
@@ -80,19 +82,25 @@ asprintf(char ** ret, const char *format, ...) _X_ATTRIBUTE_PRINTF(2,3)
len = vsnprintf(buf, sizeof(buf), format, ap);
va_end(ap);
- len += 1; /* snprintf doesn't count trailing '\0' */
- if (len <= sizeof(buf))
+ if (len < 0)
+ return -1;
+
+ if (len < sizeof(buf))
{
*ret = strdup(buf);
}
else
{
- *ret = malloc(len);
+ *ret = malloc(len + 1); /* snprintf doesn't count trailing '\0' */
if (*ret != NULL)
{
va_start(ap, format);
- len = vsnprintf(*ret, len, format, ap);
+ len = vsnprintf(*ret, len + 1, format, ap);
va_end(ap);
+ if (len < 0) {
+ free(*ret);
+ *ret = NULL;
+ }
}
}