From 5cecf53144220570809915ad7b475cc2daead4ea Mon Sep 17 00:00:00 2001 From: Hector Martin Date: Thu, 2 Nov 2023 18:03:55 +0900 Subject: [PATCH] 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 --- src/afk.c | 3 +-- src/asc.c | 2 +- src/dart.c | 4 +--- src/dcp/dpav_ep.c | 2 +- src/dcp/parser.c | 2 +- src/dcp_iboot.c | 2 +- src/hv_virtio.c | 2 +- src/i2c.c | 2 +- src/iova.c | 12 +++++------- src/kboot.c | 14 +++++++------- src/ringbuffer.c | 4 ++-- src/rtkit.c | 5 ++--- src/sart.c | 3 +-- src/tps6598x.c | 2 +- src/usb_dwc3.c | 4 ++-- 15 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/afk.c b/src/afk.c index 53c28dc00..d8e239f48 100644 --- a/src/afk.c +++ b/src/afk.c @@ -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; diff --git a/src/asc.c b/src/asc.c index 2212a28d1..fb4b95f9c 100644 --- a/src/asc.c +++ b/src/asc.c @@ -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; diff --git a/src/dart.c b/src/dart.c index 907cc15bd..7136aad10 100644 --- a/src/dart.c +++ b/src/dart.c @@ -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; diff --git a/src/dcp/dpav_ep.c b/src/dcp/dpav_ep.c index 7001e6abc..292941e81 100644 --- a/src/dcp/dpav_ep.c +++ b/src/dcp/dpav_ep.c @@ -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; diff --git a/src/dcp/parser.c b/src/dcp/parser.c index d721fda2e..68bfb8990 100644 --- a/src/dcp/parser.c +++ b/src/dcp/parser.c @@ -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'; diff --git a/src/dcp_iboot.c b/src/dcp_iboot.c index 7f88805fa..f5ec4e122 100644 --- a/src/dcp_iboot.c +++ b/src/dcp_iboot.c @@ -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; diff --git a/src/hv_virtio.c b/src/hv_virtio.c index abe458200..4ddc4be14 100644 --- a/src/hv_virtio.c +++ b/src/hv_virtio.c @@ -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; diff --git a/src/i2c.c b/src/i2c.c index 942ef1e57..df728c0b8 100644 --- a/src/i2c.c +++ b/src/i2c.c @@ -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; diff --git a/src/iova.c b/src/iova.c index c3146cd9d..29a254982 100644 --- a/src/iova.c +++ b/src/iova.c @@ -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"); diff --git a/src/kboot.c b/src/kboot.c index ed61d10df..6ceafd7b6 100644 --- a/src/kboot.c +++ b/src/kboot.c @@ -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); } diff --git a/src/ringbuffer.c b/src/ringbuffer.c index 36b89d9b7..f7ecb164b 100644 --- a/src/ringbuffer.c +++ b/src/ringbuffer.c @@ -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; diff --git a/src/rtkit.c b/src/rtkit.c index b31ec60b3..bf401c67d 100644 --- a/src/rtkit.c +++ b/src/rtkit.c @@ -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); diff --git a/src/sart.c b/src/sart.c index e0345cdef..efa579a5f 100644 --- a/src/sart.c +++ b/src/sart.c @@ -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) { diff --git a/src/tps6598x.c b/src/tps6598x.c index fdb5e112f..7bc0fe2c7 100644 --- a/src/tps6598x.c +++ b/src/tps6598x.c @@ -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; diff --git a/src/usb_dwc3.c b/src/usb_dwc3.c index ddf2e4cec..324e622fe 100644 --- a/src/usb_dwc3.c +++ b/src/usb_dwc3.c @@ -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;