Skip to end of metadata
Go to start of metadata

Functions can be defined to accept more formal arguments at the call site than are specified by the parameter declaration clause. Such functions are called variadic functions because they can accept a variable number of arguments from a caller. C++ provides two mechanisms by which a variadic function can be defined: function parameter packs and use of a C-style ellipsis as the final parameter declaration.

Variadic functions are flexible because they accept a varying number of arguments of differing types. However, they can also be hazardous. A variadic function using a C-style ellipsis (hereafter called a C-style variadic function) has no mechanisms to check the type safety of arguments being passed to the function or to check that the number of arguments being passed matches the semantics of the function definition. Consequently, a runtime call to a C-style variadic function that passes inappropriate arguments yields undefined behavior. Such undefined behavior could be exploited to run arbitrary code.

Do not define C-style variadic functions. (The declaration of a C-style variadic function that is never defined is permitted, as it is not harmful and can be useful in unevaluated contexts.)

Issues with C-style variadic functions can be avoided by using variadic functions defined with function parameter packs for situations in which a variable number of arguments should be passed to a function. Additionally, function currying can be used as a replacement to variadic functions. For example, in contrast to C's printf() family of functions, C++ output is implemented with the overloaded single-argument std::cout::operator<<() operators.

Noncompliant Code Example

This noncompliant code example uses a C-style variadic function to add a series of integers together. The function reads arguments until the value 0 is found. Calling this function without passing the value 0 as an argument (after the first two arguments) results in undefined behavior. Furthermore, passing any type other than an int also results in undefined behavior.

Compliant Solution (Recursive Pack Expansion)

In this compliant solution, a variadic function using a function parameter pack is used to implement the add() function, allowing identical behavior for call sites. Unlike the C-style variadic function used in the noncompliant code example, this compliant solution does not result in undefined behavior if the list of parameters is not terminated with 0. Additionally, if any of the values passed to the function are not integers, the code is ill-formed rather than producing undefined behavior.

This compliant solution makes use of std::enable_if to ensure that any nonintegral argument value results in an ill-formed program.

Compliant Solution (Braced Initializer List Expansion)

An alternative compliant solution that does not require recursive expansion of the function parameter pack instead expands the function parameter pack into a list of values as part of a braced initializer list. Since narrowing conversions are not allowed in a braced initializer list, the type safety is preserved despite the std::enable_if not involving any of the variadic arguments.


DCL50-CPP-EX1: It is permissible to define a C-style variadic function if that function also has external C language linkage. For instance, the function may be a definition used in a C library API that is implemented in C++.

DCL50-CPP-EX2: As stated in the normative text, C-style variadic functions that are declared but never defined are permitted. For example, when a function call expression appears in an unevaluated context, such as the argument in a sizeof expression, overload resolution is performed to determine the result type of the call but does not require a function definition. Some template metaprogramming techniques that employ SFINAE use variadic function declarations to implement compile-time type queries, as in the following example.

In this example, the value of value is determined on the basis of which overload of test() is selected. The declaration of Inner *I allows use of the variable I within the decltype specifier, which results in a pointer of some (possibly void) type, with a default value of nullptr. However, if there is no declaration of Inner::foo(), the decltype specifier will be ill-formed, and that variant of test() will not be a candidate function for overload resolution due to SFINAE. The result is that the C-style variadic function variant of test() will be the only function in the candidate set. Both test() functions are declared but never defined because their definitions are not required for use within an unevaluated expression context.

Risk Assessment

Incorrectly using a variadic function can result in abnormal program termination, unintended information disclosure, or execution of arbitrary code.




Remediation Cost









Automated Detection





Clang3.9cert-dcl50-cppChecked by clang-tidy.
LDRA tool suite9.5.8


41 S

Fully Implemented

Parasoft C/C++test9.5MISRA2004-16_1 
PRQA QA-C++3.2



SonarQube C/C++ Plugin3.11FunctionEllipsis 

Related Vulnerabilities

Search for other vulnerabilities resulting from the violation of this rule on the CERT website.


[ISO/IEC 14882-2014]Subclause 5.2.2, "Function Call"
Subclause 14.5.3, "Variadic Templates" 



  1. Although I agree that the misuses pointed out in this rule can lead to serious bugs I don't think it's realistic to expect existing projects to abandon vararg functions and convert to the recommended solutions, especially those that make use of internationalization APIs like gettext.  For printf-like functions, GCC and compatible compilers provide the format and format_arg attributes that together with the -Wformat family of options help detect and prevent the problems discussed here.  I would suggest adding an exception allowing these types of functions (i.e., those that are checked by the implementation).

    1. The -Wformat style of flags are not generalized to help with writing your own varargs function unless it uses the same format strings as printf() (and friends). What's more, that is not a portable solution because not all compilers support that kind of checking of format strings. So at best, this would be a very limited, nonportable exception. I'm not certain such an exception would really be useful – if your implementation has format string checks, and your varargs function happens to use the same format specifiers as printf(), I think it's better to simply document the function definition as not complying with this rule and provide rationale for why that's okay.

      1. My suggestion is to add an exception for functions that the compiler knows to check (like those decorated with GCC attribute format), not to others.  Many portable C++ projects define vararg functions for error reporting (all those I work with, such as GCC and GDB).  They make use of attribute format to help detect bugs when GCC or a compatible compiler is used to compile them.  It's misleading for the coding standard to suggest they're subject to the type safety problems mentioned here when they are detected and prevented by the compiler they most commonly use.  Few projects go to the effort of documenting their non-compliance with any given coding standard (I don't know of any).  In my view, the greatest value of this coding standard is in educating engineers about what is unsafe and in offering viable alternatives that make it safer.  Declaring that all vararg functions are necessarily unsafe is inaccurate and diminishes the standard's practical value.  When the recommended alternatives are also not viable replacements for an essential feature like internationalization a rule against using the feature becomes pointless.

        1. I'm not strongly opposed to this, but I am certainly uncomfortable with it.

          (1) These attributes are compiler extensions that are not supported by all major compiler vendors (MSVC comes to mind immediately). We have no other exceptions in the C++ rules for specific compiler extensions. Adding exceptions for vendor-specific extensions isn't awful in and of itself (though I do worry about it being a slippery slope), but our rules focus on what you can do with ISO C++ and only stray into implementation details when it is really beneficial to do so. 

          (2) These attributes are really hard to write properly because the GNU-style attribute requires positional indexing rather than placing the attribute directly on the format string argument itself. I see people use these attributes wrong frequently, and they are very fragile when it comes to modifications to the function signature. I am not keen on suggesting to use this arcane, fragile construct to support type unsafe C-style variadic functions rather than write a type-safe variadic function instead.

          (3) What makes these attributes special, compared to, say _Printf_format_string_ and friends that Microsoft Visual Studio supports? Or do you think we should have exceptions for those attributes as well? What if another vendor (say, Embarcadero or Digital Mars) has yet another way to solve this? This is part of what I meant above by "slippery slope".

          With those concerns in mind, do you still think it's worth having an exception? If so, would you mind drafting such an exception? That way we have something more concrete to work with for discussion, as it's possible I may be worrying about things you're not actually recommending.

          1. I agree that code should be safe and conforming first.  I will usually be among the first to recommend against relying on non-portable extensions.  But in cases where the only available alternatives are not viable for many projects or in common environments and where there are language extensions (like GCC attributes or MSVC source code annotations) that overcome the safety concerns I think the coding standard should mention them and make exceptions for using them.  I don't think we need to have an exhaustive list of these extensions to introduce an exception.  Mentioning as examples those we know are in widespread use (like GCC attribute format) should be good enough.  More can be added as we learn about them.