From 34bb013b92a61233cc9feed7866a660e22693265 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Fri, 4 Mar 2011 23:39:41 +0000 Subject: [PATCH] Use the right asprintf since we don't support truly broken platforms right now. --- compat/asprintf.c | 42 +++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/compat/asprintf.c b/compat/asprintf.c index d7c6ec99..9bd576e8 100644 --- a/compat/asprintf.c +++ b/compat/asprintf.c @@ -1,4 +1,4 @@ -/* $Id: asprintf.c,v 1.6 2011-01-21 20:03:18 nicm Exp $ */ +/* $Id: asprintf.c,v 1.7 2011-03-04 23:39:41 nicm Exp $ */ /* * Copyright (c) 2006 Nicholas Marriott @@ -28,13 +28,13 @@ #include "tmux.h" int -asprintf(char **ret, const char *format, ...) +asprintf(char **ret, const char *fmt, ...) { va_list ap; int n; - va_start(ap, format); - n = vasprintf(ret, format, ap); + va_start(ap, fmt); + n = vasprintf(ret, fmt, ap); va_end(ap); return (n); @@ -43,28 +43,20 @@ asprintf(char **ret, const char *format, ...) int vasprintf(char **ret, const char *fmt, va_list ap) { - va_list aq; - size_t len; - char *buf; - int n; + int n; - len = 64; - buf = xmalloc(len); + if ((n = vsnprintf(NULL, 0, fmt, ap)) < 0) + goto error; - for (;;) { - va_copy(aq, ap); - n = vsnprintf(buf, len, fmt, aq); - va_end(aq); - - if (n != -1) { - *ret = buf; - return (n); - } - - if (len > SIZE_MAX / 2) { - xfree(buf); - return (-1); - } - len *= 2; + *ret = xmalloc(n + 1); + if ((n = vsnprintf(*ret, n + 1, fmt, ap)) < 0) { + xfree(*ret); + goto error; } + + return (n); + +error: + *ret = NULL; + return (-1); }