diff options
author | Alan Coopersmith <alan.coopersmith@oracle.com> | 2022-11-13 10:48:30 -0800 |
---|---|---|
committer | Alan Coopersmith <alan.coopersmith@oracle.com> | 2022-11-13 14:18:15 -0800 |
commit | edcda53f9e66afadd871c23793e021e9725b4d6b (patch) | |
tree | f9989ad7fd91b55862bd8f1582abb1d9287fe445 | |
parent | 496bdd684935b9b7156222fec7009e8aa4a8dbad (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.c | 25 |
1 files changed, 12 insertions, 13 deletions
@@ -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 */ |