Angel Pons has uploaded this change for review.

View Change

treewide: Use calloc() where applicable

Replace uses of malloc() with calloc() where the newly-allocated buffer
is then zero-initialised or its size argument is the product of element
size by the number of elements.

Change-Id: Id6ef0d6d02c7b63baabc9a0087275cab22a48736
Signed-off-by: Angel Pons <th3fanbus@gmail.com>
---
M jlink_spi.c
M libflashrom.c
M mstarddc_spi.c
M sfdp.c
M spi95.c
5 files changed, 10 insertions(+), 13 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/69/55269/1
diff --git a/jlink_spi.c b/jlink_spi.c
index 0b862cf..b2e5151 100644
--- a/jlink_spi.c
+++ b/jlink_spi.c
@@ -115,7 +115,7 @@
if (length > JTAG_MAX_TRANSFER_SIZE)
return SPI_INVALID_LENGTH;

- buffer = malloc(length);
+ buffer = calloc(1, length);

if (!buffer) {
msg_perr("Memory allocation failed.\n");
@@ -125,8 +125,6 @@
/* Reverse all bytes because the device transfers data LSB first. */
reverse_bytes(buffer, writearr, writecnt);

- memset(buffer + writecnt, 0x00, readcnt);
-
if (!assert_cs(jlink_data)) {
free(buffer);
return SPI_PROGRAMMER_ERROR;
diff --git a/libflashrom.c b/libflashrom.c
index ef0b417..62b0856 100644
--- a/libflashrom.c
+++ b/libflashrom.c
@@ -120,7 +120,7 @@
const char **flashrom_supported_programmers(void)
{
enum programmer p = 0;
- const char **supported_programmers = malloc((PROGRAMMER_INVALID + 1) * sizeof(char*));
+ const char **supported_programmers = calloc(PROGRAMMER_INVALID + 1, sizeof(char *));

if (supported_programmers != NULL) {
for (; p < PROGRAMMER_INVALID; ++p) {
@@ -141,7 +141,7 @@
{
unsigned int i = 0;
struct flashrom_flashchip_info *supported_flashchips =
- malloc(flashchips_size * sizeof(*supported_flashchips));
+ calloc(flashchips_size, sizeof(*supported_flashchips));

if (supported_flashchips != NULL) {
for (; i < flashchips_size; ++i) {
@@ -182,7 +182,7 @@
++boards_known_size;

struct flashrom_board_info *supported_boards =
- malloc(boards_known_size * sizeof(*supported_boards));
+ calloc(boards_known_size, sizeof(*supported_boards));

if (supported_boards != NULL) {
for (; i < boards_known_size; ++i) {
@@ -219,7 +219,7 @@
++chipset_enables_size;

struct flashrom_chipset_info *supported_chipsets =
- malloc(chipset_enables_size * sizeof(*supported_chipsets));
+ calloc(chipset_enables_size, sizeof(*supported_chipsets));

if (supported_chipsets != NULL) {
for (; i < chipset_enables_size; ++i) {
@@ -339,10 +339,9 @@

chip_to_probe = chip_name; /* chip_to_probe is global in flashrom.c */

- *flashctx = malloc(sizeof(**flashctx));
+ *flashctx = calloc(1, sizeof(**flashctx));
if (!*flashctx)
return 1;
- memset(*flashctx, 0, sizeof(**flashctx));

for (i = 0; i < registered_master_count; ++i) {
int flash_idx = -1;
diff --git a/mstarddc_spi.c b/mstarddc_spi.c
index 0f638df..3eb5743 100644
--- a/mstarddc_spi.c
+++ b/mstarddc_spi.c
@@ -80,7 +80,7 @@
{
struct mstarddc_spi_data *mstarddc_data = flash->mst->spi.data;
int ret = 0;
- uint8_t *cmd = malloc((writecnt + 1) * sizeof(uint8_t));
+ uint8_t *cmd = calloc(writecnt + 1, sizeof(uint8_t));
if (cmd == NULL) {
msg_perr("Error allocating memory: errno %d.\n", errno);
ret = -1;
diff --git a/sfdp.c b/sfdp.c
index 530588f..a998b55 100644
--- a/sfdp.c
+++ b/sfdp.c
@@ -299,8 +299,8 @@
nph + 1, nph);

/* Fetch all parameter headers, even if we don't use them all (yet). */
- hbuf = malloc((nph + 1) * 8);
- hdrs = malloc((nph + 1) * sizeof(*hdrs));
+ hbuf = calloc(nph + 1, 8);
+ hdrs = calloc(nph + 1, sizeof(*hdrs));
if (hbuf == NULL || hdrs == NULL ) {
msg_gerr("Out of memory!\n");
goto cleanup_hdrs;
diff --git a/spi95.c b/spi95.c
index 976f99a..3a70a9f 100644
--- a/spi95.c
+++ b/spi95.c
@@ -60,7 +60,7 @@
uint8_t *erased_contents = NULL;
int result = 0;

- erased_contents = (uint8_t *)malloc(blocklen * sizeof(uint8_t));
+ erased_contents = calloc(blocklen, sizeof(uint8_t));
if (!erased_contents) {
msg_cerr("Out of memory!\n");
return 1;

To view, visit change 55269. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: Id6ef0d6d02c7b63baabc9a0087275cab22a48736
Gerrit-Change-Number: 55269
Gerrit-PatchSet: 1
Gerrit-Owner: Angel Pons <th3fanbus@gmail.com>
Gerrit-MessageType: newchange