Mercurial > hg > nnchat
changeset 361:b3556ff686fc
Fix th_strdup_vprintf().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 23 Jun 2011 11:10:30 +0300 |
parents | b465a17ffa47 |
children | 7f9057e29af2 |
files | th_string.c |
diffstat | 1 files changed, 9 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/th_string.c Thu Jun 23 10:37:11 2011 +0300 +++ b/th_string.c Thu Jun 23 11:10:30 2011 +0300 @@ -58,21 +58,27 @@ */ char * th_strdup_vprintf(const char *fmt, va_list args) { - int size = 100; + int size = 64; char *buf, *nbuf = NULL; if ((buf = th_malloc(size)) == NULL) return NULL; + fprintf(stderr, "th_strdup_vprintf(\"%s\", ...):\n", fmt); while (1) { - int n = vsnprintf(buf, size, fmt, args); + int n; + va_list ap; + va_copy(ap, args); + n = vsnprintf(buf, size, fmt, ap); + va_end(ap); + if (n > -1 && n < size) return buf; if (n > -1) size = n + 1; else size *= 2; - + if ((nbuf = th_realloc(nbuf, size)) == NULL) { th_free(buf); return NULL;