From b22b4ca9356e28e2d3e3486830df08260ad11a1f Mon Sep 17 00:00:00 2001 From: jkriege2 Date: Sat, 27 Jun 2020 14:35:45 +0200 Subject: [PATCH] Fixed Bug #43: jkqtp_format() had undefined behaviour, because va_start was called with a ref-parameter, which does not work. Now there are 4 overloaded template variants. See https://github.com/jkriege2/JKQtPlotter/issues/43 --- lib/jkqtcommon/jkqtpstringtools.cpp | 11 +-------- lib/jkqtcommon/jkqtpstringtools.h | 37 ++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/lib/jkqtcommon/jkqtpstringtools.cpp b/lib/jkqtcommon/jkqtpstringtools.cpp index f6f59a15f4..029f4ba71a 100644 --- a/lib/jkqtcommon/jkqtpstringtools.cpp +++ b/lib/jkqtcommon/jkqtpstringtools.cpp @@ -81,19 +81,10 @@ std::string jkqtp_tolower(const std::string& s){ return d; }; - std::string jkqtp_format(const std::string& templ, ...){ - va_list ap; - char buffer[4096]; - va_start (ap, templ); - vsnprintf(buffer, 4096, templ.c_str(), ap); - va_end (ap); - std::string ret(buffer); - return ret; -}; std::string jkqtp_bytestostr(double bytes){ - double data=bytes; + double data=bytes; std::string form="%.0lf"; std::string res=jkqtp_format(form,data); form="%.3lf"; diff --git a/lib/jkqtcommon/jkqtpstringtools.h b/lib/jkqtcommon/jkqtpstringtools.h index 3c8a5cec2c..0910a204f1 100644 --- a/lib/jkqtcommon/jkqtpstringtools.h +++ b/lib/jkqtcommon/jkqtpstringtools.h @@ -89,7 +89,42 @@ JKQTCOMMON_LIB_EXPORT std::string jkqtp_toupper(const std::string& s); /** \brief std::string wrapper around sprintf() * \ingroup jkqtptools_string */ -JKQTCOMMON_LIB_EXPORT std::string jkqtp_format(const std::string& templ, ...); +template +inline std::string jkqtp_format(const std::string& templ, T1 d1) { + char buffer[4096]; + snprintf(buffer, 4096, templ.c_str(), d1); + return std::string(buffer); +}; + +/** \brief std::string wrapper around sprintf() + * \ingroup jkqtptools_string + */ +template +inline std::string jkqtp_format(const std::string& templ, T1 d1, T2 d2) { + char buffer[4096]; + snprintf(buffer, 4096, templ.c_str(), d1, d2); + return std::string(buffer); +}; + +/** \brief std::string wrapper around sprintf() + * \ingroup jkqtptools_string + */ +template +inline std::string jkqtp_format(const std::string& templ, T1 d1, T2 d2, T3 d3) { + char buffer[4096]; + snprintf(buffer, 4096, templ.c_str(), d1, d2, d3); + return std::string(buffer); +}; + +/** \brief std::string wrapper around sprintf() + * \ingroup jkqtptools_string + */ +template +inline std::string jkqtp_format(const std::string& templ, T1 d1, T2 d2, T3 d3, T4 d4) { + char buffer[4096]; + snprintf(buffer, 4096, templ.c_str(), d1, d2, d3, d4); + return std::string(buffer); +}; /** \brief convert a number of bytes to a string, formatting e.g. 1024 as 1kB, ... * \ingroup jkqtptools_string