Incorrect use of va_start
va_start
is called in a non-variadic function or called with a
second argument that is not the rightmost parameter of a variadic function
Description
This defect occurs when you use the
va_start
macro in a way that violates its specifications.
In a variadic function or function with variable number of arguments:
void multipleArgumentFunction(int someArg, int rightmostFixedArg, ...) { va_list myList; va_start(myList, rightmostFixedArg); ... va_end(myList); }
va_start
macro initializes a variable argument list so that additional
arguments to the variadic function after the fixed parameters can be captured in the list. In
the preceding example, the va_start
macro initializes
myList
so that it can capture arguments after
rightmostFixedArg
.You can violate the specifications of va_start
in multiple ways. For instance:
You call
va_start
in a non-variadic function.The second argument of
va_start
is not the rightmost fixed parameter of the variadic function.
Risk
Violating the specifications of the va_start
macro can result in
compilation errors. If the compiler fails to detect the violation, the violation can result
in undefined behavior.
Fix
Make sure that:
The
va_start
macro is used in a variadic functionThe second argument of the
va_start
macro is the rightmost fixed parameter of the variadic function.
To avoid undefined and implementation-defined behavior, minimize the use of variadic
functions. Use the checkers for MISRA C:2012 Rule 17.1
or MISRA C++:2008 Rule
8-4-1
to detect use of variadic functions.
Examples
Result Information
Group: Programming |
Language: C | C++ |
Default: On for handwritten code, off for generated code |
Command-Line Syntax:
VA_START_MISUSE |
Impact: Medium |
Version History
Introduced in R2019a
See Also
Incorrect data type
passed to va_arg
| Too many va_arg calls for
current argument list
| Incorrect type data
passed to va_start
| Find defects (-checkers)
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)