From: Christopher Faulet Date: Tue, 24 Oct 2017 09:25:33 +0000 (+0200) Subject: MINOR: standard: Add memvprintf function X-Git-Tag: v1.8-rc1~174 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=93a518f02a8048fabd34c2f364e3ca9e42e4db06;p=thirdparty%2Fhaproxy.git MINOR: standard: Add memvprintf function Now memprintf relies on memvprintf. This new function does exactly what memprintf did before, but it must be called with a va_list instead of a variable number of arguments. So there is no change for every functions using memprintf. But it is now also possible to have same functionnality from any function with variadic arguments. --- diff --git a/include/common/standard.h b/include/common/standard.h index 18d90566d1..59809b4c77 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -1072,7 +1072,13 @@ int parse_asctime_date(const char *date, int len, struct tm *tm); * if (!fct2(err)) report(*err); * if (!fct3(err)) report(*err); * free(*err); + * + * memprintf relies on memvprintf. This last version can be called from any + * function with variadic arguments. */ +char *memvprintf(char **out, const char *format, va_list args) + __attribute__ ((format(printf, 2, 0))); + char *memprintf(char **out, const char *format, ...) __attribute__ ((format(printf, 2, 3))); diff --git a/src/standard.c b/src/standard.c index db312dd189..77641b4271 100644 --- a/src/standard.c +++ b/src/standard.c @@ -3309,8 +3309,11 @@ int parse_http_date(const char *date, int len, struct tm *tm) * if (!fct2(err)) report(*err); * if (!fct3(err)) report(*err); * free(*err); + * + * memprintf relies on memvprintf. This last version can be called from any + * function with variadic arguments. */ -char *memprintf(char **out, const char *format, ...) +char *memvprintf(char **out, const char *format, va_list orig_args) { va_list args; char *ret = NULL; @@ -3325,10 +3328,9 @@ char *memprintf(char **out, const char *format, ...) * target buffer is NULL. We do this in a loop just in case * intermediate evaluations get wrong. */ - va_start(args, format); + va_copy(args, orig_args); needed = vsnprintf(ret, allocated, format, args); va_end(args); - if (needed < allocated) { /* Note: on Solaris 8, the first iteration always * returns -1 if allocated is zero, so we force a @@ -3358,6 +3360,18 @@ char *memprintf(char **out, const char *format, ...) return ret; } +char *memprintf(char **out, const char *format, ...) +{ + va_list args; + char *ret = NULL; + + va_start(args, format); + ret = memvprintf(out, format, args); + va_end(args); + + return ret; +} + /* Used to add spaces before each line of , unless there is only one line. * The input argument is automatically freed and reassigned. The result will have to be * freed by the caller. It also supports being passed a NULL which results in the same