SPC-uninit-struct-field-heap
In this section:
Synopsis
A field of a dynamically allocated struct is read before it is initialized.
Enabled by default
Yes
Severity/Certainty
High/Medium

Full description
A field of a dynamically allocated struct is read before it is initialized. An uninitialized field might cause unexpected and unpredictable results. Uninitialized variables are easy to overlook, because they seldom cause problems.
Coding standards
- CERT EXP33-C
Do not reference uninitialized memory
- CWE 457
Use of Uninitialized Variable
Code examples
The following code example fails the check and will give a warning:
#include <stdlib.h>
struct st {
int x;
int y;
};
void example(void) {
int a;
struct st *str = malloc(sizeof(struct st));
a = str->x;
}
The following code example passes the check and will not give a warning about this issue:
#include <stdlib.h>
struct st {
int x;
int y;
};
void example(void) {
int a;
struct st *str = malloc(sizeof(struct st));
str->x = 0;
a = str->x;
}