Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

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 133 of Appendix J of C99.

Argument Processing

Wiki MarkupIn the following code example, the variadic function {{average()}} calculates the average value of the positive integer arguments passed to the function \[ [Seacord 2005c|AA. Bibliography#Seacord 05c]\]. The function processes arguments until it encounters an argument with the value of {{va_eol}} ({{\-1}}).

Code Block
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);
}

...

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

DCL10-C

high

probable

high

P6

L2

Related Vulnerabilities

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

...

Tool

Version

Checker

Description

Section

LDRA tool suite

Include Page
c:LDRA_Vc:
LDRA_V
Section

41 S

Section

Partially Implemented

...

MITRE CWE: CWE-628, "Function Call with Incorrectly Specified Arguments"

Bibliography

Wiki Markup\[[Seacord 2005c|AA. Bibliography#Seacord 05c]\]

...

      02. Declarations and Initialization (DCL)