mraa: Prefer calloc over malloc
Switch to using calloc on all calls to malloc where the memory isn't initialized. For things like the mraa_board_t, not allocating all to zero causes issues such as with the sub_platform member, where if that's not zero mraa_get_platform_type will try to dereference a random memory location for the sub_platform->platform_name, which can result in segmentation faults and other issues. Note that in some places where immediately after the malloc call is a copy operation, there is no need for calloc, as all the memory gets overwritten anyways, but in cases where there may or may not be memory written to (such as in mraa_file_contains, with reading from a file), even though in most cases the memory is overwritten, it could be the case that the read operation does nothing, but the memory still has non-zero values, by virtue of the fact it wasn't overwritten. Signed-off-by: Ian Johnson <ijohnson@wolfram.com> Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
committed by
Brendan Le Foll
parent
7e302242db
commit
ab14b9de43
@@ -57,7 +57,7 @@ mraa_board_t* mraa_96boards()
|
||||
char ch;
|
||||
char name[MRAA_PIN_NAME_SIZE];
|
||||
|
||||
mraa_board_t* b = (mraa_board_t*) malloc(sizeof(mraa_board_t));
|
||||
mraa_board_t* b = (mraa_board_t*) calloc(1, sizeof(mraa_board_t));
|
||||
if (b == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user