everything: Use calloc instead of malloc almost everywhere

This fixes at least one and likely more than one bug where we forgot to
zero-init.

Signed-off-by: Hector Martin <marcan@marcan.st>
This commit is contained in:
Hector Martin 2023-11-02 18:03:55 +09:00
parent 321a80c928
commit 5cecf53144
15 changed files with 28 additions and 35 deletions

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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';

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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;
@ -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;
@ -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;
@ -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");

View file

@ -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");
@ -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);
@ -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;
@ -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");
@ -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;
}
@ -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);
}

View file

@ -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;

View file

@ -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);

View file

@ -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) {

View file

@ -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;

View file

@ -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;
@ -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;