diff --git a/core/parse-xml.cpp b/core/parse-xml.cpp index dd3c3aa75..0383a92a6 100644 --- a/core/parse-xml.cpp +++ b/core/parse-xml.cpp @@ -2256,42 +2256,20 @@ extern "C" int parse_dlf_buffer(unsigned char *buffer, size_t size, struct divel /* Recording the starting battery status to extra data */ if (battery_start.volt1) { - size_t stringsize = snprintf(NULL, 0, "%dmV (%d%%)", battery_start.volt1, battery_start.percent1) + 1; - char *ptr = (char *)malloc(stringsize); + std::string str = format_string_std("%dmV (%d%%)", battery_start.volt1, battery_start.percent1); + add_extra_data(state.cur_dc, "Battery 1 (start)", str.c_str()); - if (ptr) { - snprintf(ptr, stringsize, "%dmV (%d%%)", battery_start.volt1, battery_start.percent1); - add_extra_data(state.cur_dc, "Battery 1 (start)", ptr); - free(ptr); - } - - stringsize = snprintf(NULL, 0, "%dmV (%d%%)", battery_start.volt2, battery_start.percent2) + 1; - ptr = (char *)malloc(stringsize); - if (ptr) { - snprintf(ptr, stringsize, "%dmV (%d%%)", battery_start.volt2, battery_start.percent2); - add_extra_data(state.cur_dc, "Battery 2 (start)", ptr); - free(ptr); - } + str = format_string_std("%dmV (%d%%)", battery_start.volt2, battery_start.percent2); + add_extra_data(state.cur_dc, "Battery 2 (start)", str.c_str()); } /* Recording the ending battery status to extra data */ if (battery_end.volt1) { - size_t stringsize = snprintf(NULL, 0, "%dmV (%d%%)", battery_end.volt1, battery_end.percent1) + 1; - char *ptr = (char *)malloc(stringsize); + std::string str = format_string_std("%dmV (%d%%)", battery_end.volt1, battery_end.percent1); + add_extra_data(state.cur_dc, "Battery 1 (end)", str.c_str()); - if (ptr) { - snprintf(ptr, stringsize, "%dmV (%d%%)", battery_end.volt1, battery_end.percent1); - add_extra_data(state.cur_dc, "Battery 1 (end)", ptr); - free(ptr); - } - - stringsize = snprintf(NULL, 0, "%dmV (%d%%)", battery_end.volt2, battery_end.percent2) + 1; - ptr = (char *)malloc(stringsize); - if (ptr) { - snprintf(ptr, stringsize, "%dmV (%d%%)", battery_end.volt2, battery_end.percent2); - add_extra_data(state.cur_dc, "Battery 2 (end)", ptr); - free(ptr); - } + str = format_string_std("%dmV (%d%%)", battery_end.volt2, battery_end.percent2); + add_extra_data(state.cur_dc, "Battery 2 (end)", str.c_str()); } divecomputer_end(&state); diff --git a/core/subsurface-string.h b/core/subsurface-string.h index e2127d02c..582f7622b 100644 --- a/core/subsurface-string.h +++ b/core/subsurface-string.h @@ -60,5 +60,21 @@ extern double strtod_flags(const char *str, const char **ptr, unsigned int flags #ifdef __cplusplus } + +#include +template +std::string format_string_std(const char *fmt, Args&&... args) +{ + size_t stringsize = snprintf(NULL, 0, fmt, std::forward(args)...); + if (stringsize == 0) + return std::string(); + std::string res; + res.resize(stringsize); // Pointless clearing, oh my. + // This overwrites the terminal null-byte of std::string. + // That's probably "undefined behavior". Oh my. + snprintf(res.data(), stringsize + 1, fmt, std::forward(args)...); + return res; +} + #endif #endif // SUBSURFACE_STRING_H