kern: make some use of mallocarray(9).

Focus on code where we are doing multiplications within malloc(9). None of
these ire likely to overflow, however the change is still useful as some
static checkers can benefit from the allocation attributes we use for
mallocarray.

This initial sweep only covers malloc(9) calls with M_NOWAIT. No good
reason but I started doing the changes before r327796 and at that time it
was convenient to make sure the sorrounding code could handle NULL values.

X-Differential revision: https://reviews.freebsd.org/D13837
This commit is contained in:
Pedro F. Giffuni 2018-01-15 21:18:04 +00:00
parent 0699955838
commit a18a2290cd
7 changed files with 12 additions and 11 deletions

View File

@ -159,7 +159,7 @@ sysinit_add(struct sysinit **set, struct sysinit **set_end)
count += newsysinit_end - newsysinit;
else
count += sysinit_end - sysinit;
newset = malloc(count * sizeof(*sipp), M_TEMP, M_NOWAIT);
newset = mallocarray(count, sizeof(*sipp), M_TEMP, M_NOWAIT);
if (newset == NULL)
panic("cannot malloc for sysinit");
xipp = newset;

View File

@ -444,7 +444,7 @@ cf_get_method(device_t dev, struct cf_level *level)
* match of settings against each level.
*/
count = CF_MAX_LEVELS;
levels = malloc(count * sizeof(*levels), M_TEMP, M_NOWAIT);
levels = mallocarray(count, sizeof(*levels), M_TEMP, M_NOWAIT);
if (levels == NULL)
return (ENOMEM);
error = CPUFREQ_LEVELS(sc->dev, levels, &count);
@ -969,7 +969,7 @@ cpufreq_settings_sysctl(SYSCTL_HANDLER_ARGS)
/* Get settings from the device and generate the output string. */
set_count = MAX_SETTINGS;
sets = malloc(set_count * sizeof(*sets), M_TEMP, M_NOWAIT);
sets = mallocarray(set_count, sizeof(*sets), M_TEMP, M_NOWAIT);
if (sets == NULL) {
sbuf_delete(&sb);
return (ENOMEM);

View File

@ -45,7 +45,7 @@ z_alloc(void *nil, u_int items, u_int size)
{
void *ptr;
ptr = malloc(items * size, M_TEMP, M_NOWAIT);
ptr = mallocarray(items, size, M_TEMP, M_NOWAIT);
return ptr;
}

View File

@ -338,7 +338,8 @@ init_hwpmc(void *dummy __unused)
"range.\n", pmc_softevents);
pmc_softevents = PMC_EV_DYN_COUNT;
}
pmc_softs = malloc(pmc_softevents * sizeof(struct pmc_soft *), M_PMCHOOKS, M_NOWAIT|M_ZERO);
pmc_softs = mallocarray(pmc_softevents, sizeof(struct pmc_soft *),
M_PMCHOOKS, M_NOWAIT|M_ZERO);
KASSERT(pmc_softs != NULL, ("cannot allocate soft events table"));
}

View File

@ -1464,7 +1464,7 @@ devclass_get_devices(devclass_t dc, device_t **devlistp, int *devcountp)
device_t *list;
count = devclass_get_count(dc);
list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
list = mallocarray(count, sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
if (!list)
return (ENOMEM);
@ -1680,7 +1680,7 @@ devclass_alloc_unit(devclass_t dc, device_t dev, int *unitp)
oldlist = dc->devices;
newsize = roundup((unit + 1), MINALLOCSIZE / sizeof(device_t));
newlist = malloc(sizeof(device_t) * newsize, M_BUS, M_NOWAIT);
newlist = mallocarray(newsize, sizeof(device_t), M_BUS, M_NOWAIT);
if (!newlist)
return (ENOMEM);
if (oldlist != NULL)
@ -2300,7 +2300,7 @@ device_get_children(device_t dev, device_t **devlistp, int *devcountp)
return (0);
}
list = malloc(count * sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
list = mallocarray(count, sizeof(device_t), M_TEMP, M_NOWAIT|M_ZERO);
if (!list)
return (ENOMEM);

View File

@ -651,8 +651,8 @@ _taskqueue_start_threads(struct taskqueue **tqp, int count, int pri,
vsnprintf(ktname, sizeof(ktname), name, ap);
tq = *tqp;
tq->tq_threads = malloc(sizeof(struct thread *) * count, M_TASKQUEUE,
M_NOWAIT | M_ZERO);
tq->tq_threads = mallocarray(count, sizeof(struct thread *),
M_TASKQUEUE, M_NOWAIT | M_ZERO);
if (tq->tq_threads == NULL) {
printf("%s: no memory for %s threads\n", __func__, ktname);
return (ENOMEM);

View File

@ -692,7 +692,7 @@ vmem_rehash(vmem_t *vm, vmem_size_t newhashsize)
MPASS(newhashsize > 0);
newhashlist = malloc(sizeof(struct vmem_hashlist) * newhashsize,
newhashlist = mallocarray(newhashsize, sizeof(struct vmem_hashlist),
M_VMEM, M_NOWAIT);
if (newhashlist == NULL)
return ENOMEM;