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
In the following code example, the variadic function {{ Wiki Markup 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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
|
|
...
MITRE CWE: CWE-628, "Function Call with Incorrectly Specified Arguments"
Bibliography
\[[Seacord 2005c|AA. Bibliography#Seacord 05c]\] Wiki Markup
...