MISRAC2012-Rule-1.3_k
Synopsis
(Required) There shall be no occurrence of undefined or critical unspecified behavior.
Enabled by default
Yes
Severity/Certainty
High/Low

Full description
A variable is read before it is assigned a value. This check is identical to MISRAC2004-9.1_b, MISRAC++2008-8-5-1_b, MISRAC2012-Rule-9.1_f, SPC-uninit-var-some, MISRAC++2023-11.6.2.
Coding standards
- CWE 457
Use of Uninitialized Variable
- MISRA C:2004 9.1
(Required) All automatic variables shall have been assigned a value before being used.
- MISRA C:2012 Rule-9.1
(Mandatory) The value of an object with automatic storage duration shall not be read before it has been set
- MISRA C++ 2008 8-5-1
(Required) All variables shall have a defined value before they are used.
- MISRA C++ 2023 11.6.2
(Mandatory) The value of an object must not be read before it has been set
Code examples
The following code example fails the check and will give a warning:
#include <stdlib.h>
int main(void) {
int x, y;
if (rand()) {
x = 0;
}
y = x; //x may not be initialized
return 0;
}
The following code example passes the check and will not give a warning about this issue:
#include <stdlib.h>
int main(void) {
int x;
if (rand()) {
x = 0;
}
/* x never read */
return 0;
}