Skip to content

Commit

Permalink
everything: Use calloc instead of malloc almost everywhere
Browse files Browse the repository at this point in the history
This fixes at least one and likely more than one bug where we forgot to
zero-init.

Signed-off-by: Hector Martin <[email protected]>
  • Loading branch information
marcan committed Nov 2, 2023
1 parent 321a80c commit 5cecf53
Show file tree
Hide file tree
Showing 15 changed files with 28 additions and 35 deletions.
3 changes: 1 addition & 2 deletions src/afk.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,11 +622,10 @@ static void afk_epic_notify_handler(afk_epic_ep_t *epic)
afk_epic_ep_t *afk_epic_start_ep(afk_epic_t *afk, int endpoint, const afk_epic_service_ops_t *ops,
bool notify)
{
afk_epic_ep_t *epic = malloc(sizeof(afk_epic_ep_t));
afk_epic_ep_t *epic = calloc(1, sizeof(afk_epic_ep_t));
if (!epic)
return NULL;

memset(epic, 0, sizeof(*epic));
epic->ep = endpoint;
epic->afk = afk;
epic->ops = ops;
Expand Down
2 changes: 1 addition & 1 deletion src/asc.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ asc_dev_t *asc_init(const char *path)
return NULL;
}

asc_dev_t *asc = malloc(sizeof(*asc));
asc_dev_t *asc = calloc(1, sizeof(*asc));
if (!asc)
return NULL;

Expand Down
4 changes: 1 addition & 3 deletions src/dart.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,10 @@ const struct dart_params dart_t8110 = {

dart_dev_t *dart_init(uintptr_t base, u8 device, bool keep_pts, enum dart_type_t type)
{
dart_dev_t *dart = malloc(sizeof(*dart));
dart_dev_t *dart = calloc(1, sizeof(*dart));
if (!dart)
return NULL;

memset(dart, 0, sizeof(*dart));

dart->regs = base;
dart->device = device;
dart->type = type;
Expand Down
2 changes: 1 addition & 1 deletion src/dcp/dpav_ep.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static const afk_epic_service_ops_t dcp_dpav_ops[] = {

dcp_dpav_if_t *dcp_dpav_init(dcp_dev_t *dcp)
{
dcp_dpav_if_t *dpav = malloc(sizeof(dcp_dpav_if_t));
dcp_dpav_if_t *dpav = calloc(1, sizeof(dcp_dpav_if_t));
if (!dpav)
return NULL;

Expand Down
2 changes: 1 addition & 1 deletion src/dcp/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ static char *parse_string(struct dcp_parse_ctx *handle)
if (!in)
return NULL;

out = malloc(tag->size + 1);
out = calloc(tag->size + 1, 1);

memcpy(out, in, tag->size);
out[tag->size] = '\0';
Expand Down
2 changes: 1 addition & 1 deletion src/dcp_iboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ static const afk_epic_service_ops_t iboot_service_ops[] = {

dcp_iboot_if_t *dcp_ib_init(dcp_dev_t *dcp)
{
dcp_iboot_if_t *iboot = malloc(sizeof(dcp_iboot_if_t));
dcp_iboot_if_t *iboot = calloc(1, sizeof(dcp_iboot_if_t));
if (!iboot)
return NULL;

Expand Down
2 changes: 1 addition & 1 deletion src/hv_virtio.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ void hv_map_virtio(u64 base, struct virtio_conf *conf)
struct virtio_dev *dev;
int i;

dev = malloc(sizeof(*dev) + sizeof(struct virtio_q) * conf->num_qus);
dev = calloc(1, sizeof(*dev) + sizeof(struct virtio_q) * conf->num_qus);
dev->num_qus = conf->num_qus;
dev->base = base;
dev->irq = conf->irq;
Expand Down
2 changes: 1 addition & 1 deletion src/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ i2c_dev_t *i2c_init(const char *adt_node)
return NULL;
}

i2c_dev_t *dev = malloc(sizeof(*dev));
i2c_dev_t *dev = calloc(1, sizeof(*dev));
if (!dev)
return NULL;

Expand Down
12 changes: 5 additions & 7 deletions src/iova.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ iova_domain_t *iovad_init(u64 base, u64 limit)
return NULL;
}

iova_domain_t *iovad = malloc(sizeof(*iovad));
iova_domain_t *iovad = calloc(1, sizeof(*iovad));
if (!iovad)
return NULL;

memset(iovad, 0, sizeof(*iovad));

struct iova_block *blk = malloc(sizeof(*blk));
struct iova_block *blk = calloc(1, sizeof(*blk));
if (!blk) {
free(iovad);
return NULL;
Expand Down Expand Up @@ -114,7 +112,7 @@ bool iova_reserve(iova_domain_t *iovad, u64 iova, size_t sz)
return true;
} else {
/* the to-be-reserved range is in the middle and we'll have to split this block */
struct iova_block *blk_new = malloc(sizeof(*blk_new));
struct iova_block *blk_new = calloc(1, sizeof(*blk_new));
if (!blk_new) {
printf("iova_reserve: out of memory.\n");
return false;
Expand Down Expand Up @@ -179,7 +177,7 @@ void iova_free(iova_domain_t *iovad, u64 iova, size_t sz)

/* create a new free list if it's empty */
if (!blk) {
blk = malloc(sizeof(*blk));
blk = calloc(1, sizeof(*blk));
if (!blk)
panic("out of memory in iovad_free");
blk->iova = iova;
Expand Down Expand Up @@ -209,7 +207,7 @@ void iova_free(iova_domain_t *iovad, u64 iova, size_t sz)
return;
} else if ((iova + sz) < blk->iova) {
/* create a new block */
struct iova_block *blk_new = malloc(sizeof(*blk_new));
struct iova_block *blk_new = calloc(1, sizeof(*blk_new));
if (!blk_new)
panic("iova_free: out of memory\n");

Expand Down
14 changes: 7 additions & 7 deletions src/kboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ static int dt_set_cpus(void)
if (cpus < 0)
bail("FDT: /cpus node not found in devtree\n");

uint32_t *pruned_phandles = malloc(MAX_CPUS * sizeof(uint32_t));
uint32_t *pruned_phandles = calloc(MAX_CPUS, sizeof(uint32_t));
size_t pruned = 0;
if (!pruned_phandles)
bail("FDT: out of memory\n");
Expand Down Expand Up @@ -431,7 +431,7 @@ static int dt_set_cpus(void)
if (!phs)
bail_cleanup("FDT: Failed to find cpus property under AIC affinity\n");

fdt32_t *new_phs = malloc(len);
fdt32_t *new_phs = calloc(len, 1);
size_t index = 0;
size_t count = len / sizeof(fdt32_t);

Expand Down Expand Up @@ -827,7 +827,7 @@ static void dt_set_uboot_dm_preloc(int node)
if (!pds)
return;

fdt32_t *phandles = malloc(pds_size);
fdt32_t *phandles = calloc(pds_size, 1);
if (!phandles) {
printf("FDT: out of memory\n");
return;
Expand Down Expand Up @@ -1921,8 +1921,8 @@ static int dt_disable_missing_devs(const char *adt_prefix, const char *dt_prefix
int dt_prefix_len = strlen(dt_prefix);

int acnt = 0, phcnt = 0;
u64 *addrs = malloc(max_devs * sizeof(u64));
u32 *phandles = malloc(max_devs * sizeof(u32) * 4); // Allow up to 4 extra nodes per device
u64 *addrs = calloc(max_devs, sizeof(u64));
u32 *phandles = calloc(max_devs * 4, sizeof(u32)); // Allow up to 4 extra nodes per device
if (!addrs || !phandles)
bail_cleanup("FDT: out of memory\n");

Expand Down Expand Up @@ -2145,7 +2145,7 @@ int kboot_set_chosen(const char *name, const char *value)

for (i = 0; i < MAX_CHOSEN_PARAMS; i++) {
if (!chosen_params[i][0]) {
chosen_params[i][0] = malloc(strlen(name) + 1);
chosen_params[i][0] = calloc(strlen(name) + 1, 1);
strcpy(chosen_params[i][0], name);
break;
}
Expand All @@ -2161,7 +2161,7 @@ int kboot_set_chosen(const char *name, const char *value)
return -1;

if (value) {
chosen_params[i][1] = malloc(strlen(value) + 1);
chosen_params[i][1] = calloc(strlen(value) + 1, 1);
strcpy(chosen_params[i][1], value);
}

Expand Down
4 changes: 2 additions & 2 deletions src/ringbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

ringbuffer_t *ringbuffer_alloc(size_t len)
{
ringbuffer_t *bfr = malloc(sizeof(*bfr));
ringbuffer_t *bfr = calloc(1, sizeof(*bfr));
if (!bfr)
return NULL;

bfr->buffer = malloc(len);
bfr->buffer = calloc(len, 1);
if (!bfr->buffer) {
free(bfr);
return NULL;
Expand Down
5 changes: 2 additions & 3 deletions src/rtkit.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,12 @@ rtkit_dev_t *rtkit_init(const char *name, asc_dev_t *asc, dart_dev_t *dart,
return NULL;
}

rtkit_dev_t *rtk = malloc(sizeof(*rtk));
rtkit_dev_t *rtk = calloc(1, sizeof(*rtk));
if (!rtk)
return NULL;
memset(rtk, 0, sizeof(*rtk));

size_t name_len = strlen(name);
rtk->name = malloc(name_len + 1);
rtk->name = calloc(name_len + 1, 1);
if (!rtk->name)
goto out_free_rtk;
strcpy(rtk->name, name);
Expand Down
3 changes: 1 addition & 2 deletions src/sart.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,10 @@ sart_dev_t *sart_init(const char *adt_path)
return NULL;
}

sart_dev_t *sart = malloc(sizeof(*sart));
sart_dev_t *sart = calloc(1, sizeof(*sart));
if (!sart)
return NULL;

memset(sart, 0, sizeof(*sart));
sart->base = base;

switch (*sart_version) {
Expand Down
2 changes: 1 addition & 1 deletion src/tps6598x.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ tps6598x_dev_t *tps6598x_init(const char *adt_node, i2c_dev_t *i2c)
return NULL;
}

tps6598x_dev_t *dev = malloc(sizeof(*dev));
tps6598x_dev_t *dev = calloc(1, sizeof(*dev));
if (!dev)
return NULL;

Expand Down
4 changes: 2 additions & 2 deletions src/usb_dwc3.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ static void usb_build_serial(void)
size_t len = strlen(serial);
size_t size = sizeof(struct usb_string_descriptor) + 2 * len;

struct usb_string_descriptor *desc = malloc(size);
struct usb_string_descriptor *desc = calloc(1, size);
memset(desc, 0, size);
desc->bLength = size;
desc->bDescriptorType = USB_STRING_DESCRIPTOR;
Expand Down Expand Up @@ -1100,7 +1100,7 @@ dwc3_dev_t *usb_dwc3_init(uintptr_t regs, dart_dev_t *dart)
return NULL;
}

dwc3_dev_t *dev = malloc(sizeof(*dev));
dwc3_dev_t *dev = calloc(1, sizeof(*dev));
if (!dev)
return NULL;

Expand Down

0 comments on commit 5cecf53

Please sign in to comment.