Marco Borgeaud 072e67b8d4
Fix asciidoc w.r.t. C++ (#3519)
"C++" spelled as-is can result in unexpected rendering, such as

    C++20 was released before C++17

renders as

    C20 was released before C17

Make consistent use of `[source,cpp]`.
2023-12-21 15:51:41 +01:00

81 lines
3.8 KiB
Plaintext

== Why is this an issue?
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 {cpp} standard version in use:
* Before {cpp}11, the standard only allows POD types to be used as variadic arguments.
* In {cpp}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 {cpp}11 parameter packs instead of variadic functions.
=== Noncompliant code example
[source,cpp]
----
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<float> duration;
my_log("%f", duration); // Noncompliant, the user probably meant to pass duration.count()
}
----
=== Compliant solution
[source,cpp]
----
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<float> 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).
[source,cpp]
----
// This variadic function is used as a catch-all net to terminate recursion
std::size_t elementsCount(...) { return 1u; }
template<typename T>
std::size_t elementsCount(const std::vector<T>& 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)
});
}
----
== Resources
* MISRA C:2004, 16.1 - Functions shall not be defined with a variable number of arguments.
* https://wiki.sei.cmu.edu/confluence/x/5ns-BQ[CERT, DCL50-CPP.] - Do not define a C-style variadic function
* {cpp} Core Guidelines - https://github.com/isocpp/CppCoreGuidelines/blob/e49158a/CppCoreGuidelines.md#-es34-dont-define-a-c-style-variadic-function[ES.34: Don't define a (C-style) variadic function]
=== Related rules
* S2275 and S3457 are rules that specialize in detecting type mismatches with format strings.