diff --git a/common/include/villas/utils.hpp b/common/include/villas/utils.hpp index ce6defa42..99f59a2cf 100644 --- a/common/include/villas/utils.hpp +++ b/common/include/villas/utils.hpp @@ -163,11 +163,11 @@ char *strcatf(char **dest, const char *fmt, ...) __attribute__((format(printf, 2, 3))); // Variadic version of strcatf() -char *vstrcatf(char **dest, const char *fmt, va_list va) +char *vstrcatf(char **dest, const char *fmt, va_list &va) __attribute__((format(printf, 2, 0))); char *strf(const char *fmt, ...); -char *vstrf(const char *fmt, va_list va); +char *vstrf(const char *fmt, va_list &va); // Allocate and copy memory. void *memdup(const void *src, size_t bytes); diff --git a/common/lib/utils.cpp b/common/lib/utils.cpp index 61666029a..e5422a882 100644 --- a/common/lib/utils.cpp +++ b/common/lib/utils.cpp @@ -194,10 +194,24 @@ double boxMuller(float m, float s) { double randf() { return (double)random() / RAND_MAX; } -char *vstrcatf(char **dest, const char *fmt, va_list ap) { +char *vstrcatf(char **dest, const char *fmt, va_list &ap) { char *tmp; int n = *dest ? strlen(*dest) : 0; - int i = vasprintf(&tmp, fmt, ap); + + va_list va_copy; + va_copy(va_copy, ap); + int i = vasprintf(&tmp, fmt, va_copy); + va_end(va_copy); + + // Advance va_list + const char *format = fmt; + if (strcmp(format, "%ju") == 0) { + va_arg(ap, unsigned int); + } else if (strcmp(format, "%lf") == 0) { + va_arg(ap, double); + } else if (strcmp(format, "%s") == 0) { + va_arg(ap, char *); + } *dest = (char *)(realloc(*dest, n + i + 1)); if (*dest != nullptr) @@ -228,7 +242,7 @@ char *strf(const char *fmt, ...) { return buf; } -char *vstrf(const char *fmt, va_list va) { +char *vstrf(const char *fmt, va_list &va) { char *buf = nullptr; vstrcatf(&buf, fmt, va);