CertC-DCL10

Maintain the contract between the writer and caller of variadic functions

Required inputs: IR

Variadic functions accept a variable number of arguments but are problematic. Variadic functions define an implicit contract between the function writer and the function user that allows the function to determine the number of arguments passed in any particular invocation. Failure to enforce this contract may result in undefined behavior . See undefined behavior 141 of Appendix J of the C Standard.

Argument Processing

In the following code example, the variadic function average() calculates the average value of the positive integer arguments passed to the function [ Seacord 2013 ]. The function processes arguments until it encounters an argument with the value of va_eol ( -1 ).

enum { va_eol = -1 };

unsigned int average(int first, ...) {
  unsigned int count = 0;
  unsigned int sum = 0;
  int i = first;
  va_list args;

  va_start(args, first);

  while (i != va_eol) {
    sum += i;
    count++;
    i = va_arg(args, int);
  }

  va_end(args);
  return(count ? (sum / count) : 0);
}

Note that va_start() must be called to initialize the argument list and that va_end() must be called when finished with a variable argument list.

Noncompliant Code Example

In this noncompliant code example, the average() function is called as follows:

int avg = average(1, 4, 6, 4, 1);

The omission of the va_eol terminating value means that the function will continue to process values from the stack until it encounters a va_eol by coincidence or an error occurs.

Compliant Solution

This compliant solution enforces the contract by adding va_eol as the final argument:

int avg = average(1, 4, 6, 4, 1, va_eol);
Noncompliant Code Example

Another common mistake is to use more conversion specifiers than supplied arguments, as shown in this noncompliant code example:

const char *error_msg = "Resource not available to user.";
/* ... */
printf("Error (%s): %s", error_msg);

This code results in nonexistent arguments being processed by the function, potentially leaking information about the process.

Compliant Solution

This compliant solution matches the number of format specifiers with the number of variable arguments:

const char *error_msg = "Resource not available to user.";
/* ... */
printf("Error: %s", error_msg);
Argument List Caveats

C functions that accept the variadic primitive va_list as an argument pose an additional risk. Calls to vfprintf() , vfscanf() , vprintf() , vscanf() , vsnprintf() , vsprintf() , and vsscanf() use the va_arg() macro, invalidating the parameterized va_list . Consequently, once a va_list is passed as an argument to any of these functions, it cannot be used again without a call to va_end() followed by a call to va_start() .

Risk Assessment

Incorrectly using a variadic function can result in abnormal program termination or unintended information disclosure.

Recommendation Severity Likelihood Detectable Repairable Priority Level
DCL10-C High Probable No No P6 L2
Related Guidelines
ISO/IEC TR 24772:2013 Subprogram Signature Mismatch [OTR]
MISRA C:2012 Rule 17.1 (required)
MITRE CWE CWE-628 , Function call with incorrectly specified arguments
Bibliography
[ Seacord 2013 ] Chapter 6, "Formatted Output"
Excerpt from SEI CERT C Coding Standard: Rules for Developing Safe, Reliable, and Secure Systems (2016 Edition) and SEI CERT C Coding Standard [https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/recommendations/declarations-and-initialization-dcl/dcl10-c], Copyright (C) 1995-2026 Carnegie Mellon University. See 9.4 3rd-Party Licenses in the documentation for full details.

Possible Messages

Key

Text

Severity

Disabled

arg_type_mismatch

{} expects argument of type ‘{}’, but argument {} has type ‘{}’

None

False

buffer_too_small

{} may write up to {} characters to buffer of size {}.

None

False

invalid_conversion

Invalid or non-standard conversion specification

None

False

matching_arg_expected

{} expects a matching ‘{}’ argument

None

False

missing_terminating_value

missing ‘{}’ terminating value.

None

False

precision_for_conversion

Precision must not be used with %{} conversion specifier

None

False

too_many_args

Too many arguments for format.

None

False

unknown_buffer_size

Potential buffer overflow: {} used with buffer of unknown size.

None

False

unlimited_read

Potential buffer overflow: {} has no limit on amount of characters read.

None

False

unsupported_assignment_suppression

%n does not support assignment suppression

None

False

unsupported_field_width

%n does not support field width

None

False

unsupported_flags

%n does not support flags

None

False

unsupported_flags_modifiers

Cannot use any flags or modifiers with ‘%%’

None

False

unsupported_hash

%{} does not support the ‘#’ flag

None

False

unsupported_i_flag

%{} does not support the ‘I’ flag

None

False

unsupported_length_modifier

%{} does not support the ‘{}’ length modifier

None

False

unsupported_tick

%{} does not support the “’” flag

None

False

unsupported_zero

%{} does not support the ‘0’ flag

None

False

Options

allow_extra_args

allow_extra_args : bool = False

Whether to allow additional arguments that are not used by the format string.
 

allow_gnu_extensions

allow_gnu_extensions : bool = False

Whether to allow the GNU extensions to format specifications.
 

allow_unknown_specs

allow_unknown_specs : bool = False

Whether to allow unknown format specifications. It may be necessary to set this option when using implementation-specific extensions. Arguments are not checked when the format string contains unknown format specifications.
 

functions

functions

Type: dict[bauhaus.analysis.config.QualifiedName, typing.Tuple[str, int, typing.Optional[int]]]

Default:

{
   '_printf_l': ('printf', 1, 3),
   'fprintf': ('printf', 1, 2),
   'fscanf': ('scanf', 1, 2),
   'printf': ('printf', 0, 1),
   'scanf': ('scanf', 0, 1),
   'snprintf': ('printf', 2, 3),
   'sprintf': ('printf', 1, 2),
   'sscanf': ('scanf', 1, 2),
   'vfprintf': ('printf', 1, None),
   'vfscanf': ('scanf', 1, None),
   'vprintf': ('printf', 0, None),
   'vscanf': ('scanf', 0, None),
   'vsnprintf': ('printf', 2, None),
   'vsprintf': ('printf', 1, None),
   'vsscanf': ('scanf', 1, None)
}
A dictionary mapping the names of the functions to check, to a triple (function_kind, fmt_param_index, arg_start_index) where function_kind is either printf or scanf, fmt_param_index is the index of the format-string parameter, and arg_start_index is the index of the first variadic argument.
 

use_static_semantic_analysis

use_static_semantic_analysis : bool = True

Whether the rule should use the results of the StaticSemanticAnalysis to check buffer sizes. This can produce different findings, as the size of buffer arguments are computed more accurately. This will not enforce StaticSemanticAnalysis to be enabled, but will produce less accurate results if it is not.