Private
Public Access
2
0

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:
Ian Johnson
2016-01-17 09:39:45 -06:00
committed by Brendan Le Foll
parent 7e302242db
commit ab14b9de43
16 changed files with 22 additions and 22 deletions

View File

@@ -356,7 +356,7 @@ mraa_beaglebone_pwm_init_replace(int pin)
}
if (mraa_file_exist(devpath)) {
mraa_pwm_context dev = (mraa_pwm_context) malloc(sizeof(struct _pwm));
mraa_pwm_context dev = (mraa_pwm_context) calloc(1, sizeof(struct _pwm));
if (dev == NULL)
return NULL;
dev->duty_fp = -1;
@@ -488,7 +488,7 @@ mraa_beaglebone()
else
ehrpwm2b_enabled = 0;
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;
// TODO: Detect Beaglebone Black Revisions, for now always TYPE B
@@ -512,7 +512,7 @@ mraa_beaglebone()
b->pwm_max_period = 2147483;
b->pwm_min_period = 1;
b->pins = (mraa_pininfo_t*) malloc(sizeof(mraa_pininfo_t) * b->phy_pin_count);
b->pins = (mraa_pininfo_t*) calloc(b->phy_pin_count,sizeof(mraa_pininfo_t));
if (b->pins == NULL) {
goto error;
}