Your solution worked, but there is a warning raised for these two lines:
size_t buf_size = snprintf(nullptr, 0, format.c_str(), args...) + 1;
snprintf(buf.data(), buf_size, format.c_str(), args...);
The warning is:
Avoid passing object of class type "const KeyVelocity" as a variadic argument.
KeyVelocity is defined by this code:
struct KeyVelocity {
uint8_t value;
KeyVelocity() : value(0) {}
explicit KeyVelocity(uint8_t v) : value(v) {}
};
static_assert(sizeof(KeyVelocity) == 1, "KeyVelocity needs to be 1 in size!");
Further information on the warning is provided by the following:
User-defined types should not be passed as variadic arguments
Bug Major cpp:S5270 Tags: based-on-misra cert cppcoreguidelines suspicious
Variadic arguments allow a function to accept any number of arguments (in this rule, we are not talking about variadic templates, but about functions with ellipses). But these arguments have to respect some criteria to be handled properly.
The standard imposes some requirements on the class types that can be passed as variadic arguments, and those requirements vary according to the C++ standard version in use:
○ Before C++11, the standard only allows POD types to be used as variadic arguments.
○ In C++11, the rules are relaxed such that any class type with an eligible non-trivial copy constructor, an eligible non-trivial move constructor, or a non-trivial destructor can be used in variadic arguments.
The rule detects any violations of these requirements since they can trigger undefined behavior.
Additionally, since using an incorrect type to access the passed parameter within the variadic function can lead to undefined behavior, the rule goes a step further and reports all cases when class types are passed as variadic arguments. The rationale is that, most likely, the user forgot to call a method on the object being passed (std::string_view::data() for example) that would get a member of a built-in type.
When in need to pass class types to functions that take a variable number of arguments, consider using modern type-safe alternatives like C++11 parameter packs instead of variadic functions.
Noncompliant Code Example
void my_log(const char* format, ...);
void f() {
std::string someStr = "foo";
my_log("%s", someStr); // Noncompliant; the c++11 standard requires types passed as variadic arguments to have a trivial copy constructor. The user probably meant to pass someStr.c_str() here
std::string_view someStrView = "bar";
my_log("%s", someStrView); // Noncompliant; the user probably meant to pass someText.data()
std::chrono::duration duration;
my_log("%f", duration); // Noncompliant, the user probably meant to pass duration.count()
}
Compliant Solution
void my_log(const char* format, ...);
void f() {
std::string someStr = "foo";
my_log("%s", someStr.c_str()); // Compliant
std::string_view someStrView = "bar";
my_log("%s", someStrView.data()); // Compliant
std::chrono::duration duration;
my_log("%f", duration.count()); // Compliant
}
Exceptions
The rule doesn’t report an issue in the following cases:
○ When the called variadic function doesn’t have any non-variadic parameters. This is a common pattern when the function is used as a catch-all net for an overload set. This is also guaranteed to be safe since there is no portable to access the passed arguments.
○ When the called variadic function is known to accept a class type object as a variadic argument (e.g. the semctl system call).
// This variadic function is used as a catch-all net to terminate recursion
std::size_t elementsCount(...) { return 1u; }
template
std::size_t elementsCount(const std::vector& vec) {
// Sum the elements of all nested vectors recursively
return std::accumulate(vec.begin(), vec.end(), 0u, [] (const std::size_t count, const T& element) {
return count + elementsCount(element); // Compliant (the callee doesn't have non-variadic arguments)
});
}
Can you explain the warning? Is it valid? How do you suggest resolving it?