Created
February 2, 2016 07:29
-
-
Save orbitcowboy/ac8b91b98cc8fbc1c478 to your computer and use it in GitHub Desktop.
Stackoverflow question http://stackoverflow.com/questions/35122760/how-to-detect-or-avoid-using-c-uninitialized-pointer?answertab=active#tab-top
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <inttypes.h> | |
#include <iostream> | |
#include <cstdio> | |
// def.h | |
struct INFO { | |
uint32_t val1; | |
uint32_t val2; | |
}; | |
struct INFO_LIST { | |
uint32_t id; | |
struct INFO * data; | |
}; | |
// util.cpp | |
void print_request(const struct INFO_LIST req) | |
{ | |
fprintf(stdout, "%u\t%u\t%u\n", req.id, req.data->val1, req.data->val2); | |
} | |
// A.cpp | |
int parse_ie() | |
{ | |
struct INFO_LIST req; | |
req.id = 10; | |
req.data = new INFO(); | |
req.data->val1 = 101; | |
req.data->val2 = 102; | |
print_request(req); | |
} | |
// B.cpp | |
int parse_chrome() | |
{ | |
struct INFO_LIST req; | |
req.id = 20; | |
print_request(req); // core dump here! | |
} | |
/* | |
$ cppcheck test.cpp | |
Checking test.cpp... | |
[test.cpp:37]: (error) Uninitialized struct member: req.data | |
$ cppcheck --version | |
Cppcheck 1.73 dev | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment