- SonarQube Server v9.9.7 (build 96285)
I have a custom logging function which has been working fine for years. SonarQube is now telling me this code has a major bug.
template <class... Args>
void log(int priority, const char* format, const char* file, int line, Args... args) {
// Add time, file and line to start of format string
const auto system_time = std::chrono::system_clock::now();
const std::chrono::milliseconds ms =
std::chrono::duration_cast<std::chrono::milliseconds>(system_time.time_since_epoch()) % 10000;
// Try a buffer, see if it fits
char time_buffer[80];
char buffer[1024];
tm now_tm;
{
const std::time_t time = std::chrono::system_clock::to_time_t(system_time);
localtime_r(&time, &now_tm);
}
std::strftime(time_buffer, 80, "%Y-%m-%d %H:%M:%S", &now_tm);
int size = snprintf(buffer, 1024, "[%s.%04ld] [%s:%d] %s\n", time_buffer, ms.count(), file, line, format);
std::string full_format;
if (size < 1024) {
full_format = buffer;
}
else{
full_format.resize(size);
snprintf(&full_format[0], size + 1, "[%s.%04ld] [%s:%d] %s\n", time_buffer, ms.count(), file, line, format);
}
// Try a buffer, see if it fits
// Suppress "format string argument" error for the next line
// codeql: [query]@off
// lgtm [cpp/printf-nonliteral-format-string]
size = snprintf(buffer, 1024, full_format.c_str(), args...);
On this line I get the error: cannot pass object of non-trivial type ‘std::basic_string’ through variadic function; call will abort at runtime and the args...
parameter is underlined. I’ve read the “Why is this an issue?” section and the link to cpp:S5270 User-defined types should not be passed as variadic arguments but neither make sense in this context - I’m not passing a string directly.