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

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
- CERT EXP47-C
Do not call va_arg with an argument of the incorrect type
Code examples
The following code example fails the check and will give a warning:
#include <stdarg.h>
#include <stddef.h>
void func(size_t num_vargs, ...) {
va_list ap;
va_start(ap, num_vargs);
if (num_vargs > 0) {
unsigned char c = va_arg(ap, unsigned char);
// ...
}
va_end(ap);
}
void f(void) {
unsigned char c = 0x12;
func(1, c);
}
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, ...) {
va_list ap;
va_start(ap, num_vargs);
if (num_vargs > 0) {
unsigned char c = (unsigned char) va_arg(ap, int);
// ...
}
va_end(ap);
}
void f(void) {
unsigned char c = 0x12;
func(1, c);
}