From 7e8561e030fcfaba5cd620d7e9c9ab8356163132 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 22 Sep 2020 17:28:34 +0200 Subject: dynbuf: make *addf() not require extra mallocs ... by introducing a printf() function that appends directly into a dynbuf: Curl_dyn_vprintf(). This avoids the mandatory extra malloc so if the buffer is already big enough it can just printf directly into it. Since this less-malloc version requires tthe use of a library internal printf function, we only provide this version when building libcurl and not for the dynbuf code that is used when building the curl tool. Closes #5998 --- lib/dynbuf.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'lib/dynbuf.c') diff --git a/lib/dynbuf.c b/lib/dynbuf.c index 5e15040bb..2dace3428 100644 --- a/lib/dynbuf.c +++ b/lib/dynbuf.c @@ -181,8 +181,17 @@ CURLcode Curl_dyn_add(struct dynbuf *s, const char *str) */ CURLcode Curl_dyn_addf(struct dynbuf *s, const char *fmt, ...) { - char *str; va_list ap; +#ifdef BUILDING_LIBCURL + int rc; + va_start(ap, fmt); + rc = Curl_dyn_vprintf(s, fmt, ap); + va_end(ap); + + if(!rc) + return CURLE_OK; +#else + char *str; va_start(ap, fmt); str = vaprintf(fmt, ap); /* this allocs a new string to append */ va_end(ap); @@ -194,9 +203,12 @@ CURLcode Curl_dyn_addf(struct dynbuf *s, const char *fmt, ...) } /* If we failed, we cleanup the whole buffer and return error */ Curl_dyn_free(s); +#endif return CURLE_OUT_OF_MEMORY; } + + /* * Returns a pointer to the buffer. */ -- cgit v1.2.1