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

(cherry picked from commit b22b4ca935)
This commit is contained in:
jkriege2 2020-06-27 14:35:45 +02:00
parent 4f597909a2
commit 1e47a86268
2 changed files with 37 additions and 11 deletions

View File

@ -81,15 +81,6 @@ std::string jkqtp_tolower(const std::string& s){
return d; 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){ std::string jkqtp_bytestostr(double bytes){

View File

@ -89,7 +89,42 @@ JKQTCOMMON_LIB_EXPORT std::string jkqtp_toupper(const std::string& s);
/** \brief std::string wrapper around sprintf() /** \brief std::string wrapper around sprintf()
* \ingroup jkqtptools_string * \ingroup jkqtptools_string
*/ */
JKQTCOMMON_LIB_EXPORT std::string jkqtp_format(const std::string& templ, ...); template <class T1>
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 <class T1, class T2>
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 <class T1, class T2, class T3>
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 <class T1, class T2, class T3, class T4>
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, ... /** \brief convert a number of bytes to a string, formatting e.g. 1024 as 1kB, ...
* \ingroup jkqtptools_string * \ingroup jkqtptools_string