summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2022-11-13 10:48:30 -0800
committerAlan Coopersmith <alan.coopersmith@oracle.com>2022-11-13 14:18:15 -0800
commitedcda53f9e66afadd871c23793e021e9725b4d6b (patch)
treef9989ad7fd91b55862bd8f1582abb1d9287fe445
parent496bdd684935b9b7156222fec7009e8aa4a8dbad (diff)
Fallback asprintf: don't truncate output that has a \0 in string
Unlikely to be hit in xrdb, but may affect other uses if this code gets copied to other programs. Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--xrdb.c25
1 files changed, 12 insertions, 13 deletions
diff --git a/xrdb.c b/xrdb.c
index a183199..6455b93 100644
--- a/xrdb.c
+++ b/xrdb.c
@@ -168,25 +168,24 @@ asprintf(char **ret, const char *format, ...)
if (len < 0)
return -1;
+ *ret = malloc(len + 1); /* snprintf doesn't count trailing '\0' */
+ if (*ret == NULL)
+ return -1;
+
if (len < sizeof(buf)) {
- *ret = strdup(buf);
+ memcpy(*ret, buf, len + 1);
}
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;
- }
+ va_start(ap, format);
+ len = vsnprintf(*ret, len + 1, format, ap);
+ va_end(ap);
+ if (len < 0) {
+ free(*ret);
+ *ret = NULL;
+ return -1;
}
}
- if (*ret == NULL)
- return -1;
-
return len;
}
#endif /* HAVE_ASPRINTF */