Skip to content

Commit 5cecf53

Browse files
committed
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>
1 parent 321a80c commit 5cecf53

File tree

15 files changed

+28
-35
lines changed

15 files changed

+28
-35
lines changed

src/afk.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,11 +622,10 @@ static void afk_epic_notify_handler(afk_epic_ep_t *epic)
622622
afk_epic_ep_t *afk_epic_start_ep(afk_epic_t *afk, int endpoint, const afk_epic_service_ops_t *ops,
623623
bool notify)
624624
{
625-
afk_epic_ep_t *epic = malloc(sizeof(afk_epic_ep_t));
625+
afk_epic_ep_t *epic = calloc(1, sizeof(afk_epic_ep_t));
626626
if (!epic)
627627
return NULL;
628628

629-
memset(epic, 0, sizeof(*epic));
630629
epic->ep = endpoint;
631630
epic->afk = afk;
632631
epic->ops = ops;

src/asc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ asc_dev_t *asc_init(const char *path)
4444
return NULL;
4545
}
4646

47-
asc_dev_t *asc = malloc(sizeof(*asc));
47+
asc_dev_t *asc = calloc(1, sizeof(*asc));
4848
if (!asc)
4949
return NULL;
5050

src/dart.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,10 @@ const struct dart_params dart_t8110 = {
195195

196196
dart_dev_t *dart_init(uintptr_t base, u8 device, bool keep_pts, enum dart_type_t type)
197197
{
198-
dart_dev_t *dart = malloc(sizeof(*dart));
198+
dart_dev_t *dart = calloc(1, sizeof(*dart));
199199
if (!dart)
200200
return NULL;
201201

202-
memset(dart, 0, sizeof(*dart));
203-
204202
dart->regs = base;
205203
dart->device = device;
206204
dart->type = type;

src/dcp/dpav_ep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static const afk_epic_service_ops_t dcp_dpav_ops[] = {
4444

4545
dcp_dpav_if_t *dcp_dpav_init(dcp_dev_t *dcp)
4646
{
47-
dcp_dpav_if_t *dpav = malloc(sizeof(dcp_dpav_if_t));
47+
dcp_dpav_if_t *dpav = calloc(1, sizeof(dcp_dpav_if_t));
4848
if (!dpav)
4949
return NULL;
5050

src/dcp/parser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static char *parse_string(struct dcp_parse_ctx *handle)
127127
if (!in)
128128
return NULL;
129129

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

132132
memcpy(out, in, tag->size);
133133
out[tag->size] = '\0';

src/dcp_iboot.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static const afk_epic_service_ops_t iboot_service_ops[] = {
127127

128128
dcp_iboot_if_t *dcp_ib_init(dcp_dev_t *dcp)
129129
{
130-
dcp_iboot_if_t *iboot = malloc(sizeof(dcp_iboot_if_t));
130+
dcp_iboot_if_t *iboot = calloc(1, sizeof(dcp_iboot_if_t));
131131
if (!iboot)
132132
return NULL;
133133

src/hv_virtio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ void hv_map_virtio(u64 base, struct virtio_conf *conf)
282282
struct virtio_dev *dev;
283283
int i;
284284

285-
dev = malloc(sizeof(*dev) + sizeof(struct virtio_q) * conf->num_qus);
285+
dev = calloc(1, sizeof(*dev) + sizeof(struct virtio_q) * conf->num_qus);
286286
dev->num_qus = conf->num_qus;
287287
dev->base = base;
288288
dev->irq = conf->irq;

src/i2c.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ i2c_dev_t *i2c_init(const char *adt_node)
4848
return NULL;
4949
}
5050

51-
i2c_dev_t *dev = malloc(sizeof(*dev));
51+
i2c_dev_t *dev = calloc(1, sizeof(*dev));
5252
if (!dev)
5353
return NULL;
5454

src/iova.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ iova_domain_t *iovad_init(u64 base, u64 limit)
2424
return NULL;
2525
}
2626

27-
iova_domain_t *iovad = malloc(sizeof(*iovad));
27+
iova_domain_t *iovad = calloc(1, sizeof(*iovad));
2828
if (!iovad)
2929
return NULL;
3030

31-
memset(iovad, 0, sizeof(*iovad));
32-
33-
struct iova_block *blk = malloc(sizeof(*blk));
31+
struct iova_block *blk = calloc(1, sizeof(*blk));
3432
if (!blk) {
3533
free(iovad);
3634
return NULL;
@@ -114,7 +112,7 @@ bool iova_reserve(iova_domain_t *iovad, u64 iova, size_t sz)
114112
return true;
115113
} else {
116114
/* the to-be-reserved range is in the middle and we'll have to split this block */
117-
struct iova_block *blk_new = malloc(sizeof(*blk_new));
115+
struct iova_block *blk_new = calloc(1, sizeof(*blk_new));
118116
if (!blk_new) {
119117
printf("iova_reserve: out of memory.\n");
120118
return false;
@@ -179,7 +177,7 @@ void iova_free(iova_domain_t *iovad, u64 iova, size_t sz)
179177

180178
/* create a new free list if it's empty */
181179
if (!blk) {
182-
blk = malloc(sizeof(*blk));
180+
blk = calloc(1, sizeof(*blk));
183181
if (!blk)
184182
panic("out of memory in iovad_free");
185183
blk->iova = iova;
@@ -209,7 +207,7 @@ void iova_free(iova_domain_t *iovad, u64 iova, size_t sz)
209207
return;
210208
} else if ((iova + sz) < blk->iova) {
211209
/* create a new block */
212-
struct iova_block *blk_new = malloc(sizeof(*blk_new));
210+
struct iova_block *blk_new = calloc(1, sizeof(*blk_new));
213211
if (!blk_new)
214212
panic("iova_free: out of memory\n");
215213

src/kboot.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ static int dt_set_cpus(void)
356356
if (cpus < 0)
357357
bail("FDT: /cpus node not found in devtree\n");
358358

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

434-
fdt32_t *new_phs = malloc(len);
434+
fdt32_t *new_phs = calloc(len, 1);
435435
size_t index = 0;
436436
size_t count = len / sizeof(fdt32_t);
437437

@@ -827,7 +827,7 @@ static void dt_set_uboot_dm_preloc(int node)
827827
if (!pds)
828828
return;
829829

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

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

@@ -2145,7 +2145,7 @@ int kboot_set_chosen(const char *name, const char *value)
21452145

21462146
for (i = 0; i < MAX_CHOSEN_PARAMS; i++) {
21472147
if (!chosen_params[i][0]) {
2148-
chosen_params[i][0] = malloc(strlen(name) + 1);
2148+
chosen_params[i][0] = calloc(strlen(name) + 1, 1);
21492149
strcpy(chosen_params[i][0], name);
21502150
break;
21512151
}
@@ -2161,7 +2161,7 @@ int kboot_set_chosen(const char *name, const char *value)
21612161
return -1;
21622162

21632163
if (value) {
2164-
chosen_params[i][1] = malloc(strlen(value) + 1);
2164+
chosen_params[i][1] = calloc(strlen(value) + 1, 1);
21652165
strcpy(chosen_params[i][1], value);
21662166
}
21672167

0 commit comments

Comments
 (0)