CERT-EXP47-C_b
In this section:
Synopsis
Do not call va_arg with an argument of the incorrect type
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
Ensure that an invocation of the va_arg() macro does not attempt to access an argument that was not passed to the variadic function. Further, the type passed to the va_arg() macro must match the type passed to the variadic function after default argument promotions have been applied.
Coding standards
This check does not correspond to any coding standard rules.
Code examples
The following code example fails the check and will give a warning:
#include <stdarg.h>
void func(const char *cp, ...) {
va_list ap;
va_start(ap, cp);
int val = va_arg(ap, int);
// ...
va_end(ap);
}
void f(void) {
func("The only argument");
}
The following code example passes the check and will not give a warning about this issue:
#include <stdarg.h>
#include <stddef.h>
void func(size_t num_vargs, const char *cp, ...) {
va_list ap;
va_start(ap, cp);
if (num_vargs > 0) {
int val = va_arg(ap, int);
// ...
}
va_end(ap);
}
void f(void) {
func(0, "The only argument");
}