Main Content

MISRA C++:2023 Rule 21.10.1

The features of <cstdarg> shall not be used

Since R2024b

Description

Rule Definition

The features of <cstdarg> shall not be used.

Rationale

The rule forbids use of va_list, va_arg, va_start, va_end, and va_copy.

You can use these features in ways where the behavior is not defined in the C++ standard. For instance:

  • You invoke va_start in a function but do not invoke the corresponding va_end before the function block ends.

  • You invoke va_arg in different functions on the same variable of type va_list.

  • va_arg has the syntax type va_arg (va_list ap, type).

    You invoke va_arg with a type that is incompatible with the actual type of the argument retrieved from ap.

Polyspace Implementation

The checker reports a violation if you use any of these functions from <sstdarg>: va_list, va_arg, va_start, va_end, and va_copy.

Troubleshooting

If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include<cstdarg>
void f2(int n, ...) {
    int i;
    double val;
    va_list vl;                         /* Non-compliant */

    va_start(vl, n);                    /* Non-compliant */

    for(i = 0; i < n; i++)
    {
        val = va_arg(vl, double);         /* Non-compliant */
    }

    va_end(vl);                         /* Non-compliant */
}

In this example, the rule is violated because va_start, va_list, va_arg and va_end are used.

Check Information

Group: Language Support Library
Category: Required

Version History

Introduced in R2024b