From 74641f0bc6f251da3823151f435518b96df3bb9a Mon Sep 17 00:00:00 2001 From: "Pedro F. Giffuni" Date: Mon, 15 Jan 2018 21:08:22 +0000 Subject: [PATCH] x86: 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 --- sys/amd64/amd64/bpf_jit_machdep.c | 2 +- sys/i386/i386/bpf_jit_machdep.c | 2 +- sys/i386/i386/k6_mem.c | 2 +- sys/x86/cpufreq/est.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sys/amd64/amd64/bpf_jit_machdep.c b/sys/amd64/amd64/bpf_jit_machdep.c index 8d4ddaa24135..646c6f39b8cd 100644 --- a/sys/amd64/amd64/bpf_jit_machdep.c +++ b/sys/amd64/amd64/bpf_jit_machdep.c @@ -186,7 +186,7 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size) /* Allocate the reference table for the jumps. */ if (fjmp) { #ifdef _KERNEL - stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT, + stream.refs = mallocarray(nins + 1, sizeof(u_int), M_BPFJIT, M_NOWAIT | M_ZERO); #else stream.refs = calloc(nins + 1, sizeof(u_int)); diff --git a/sys/i386/i386/bpf_jit_machdep.c b/sys/i386/i386/bpf_jit_machdep.c index ff3fa3fb0750..d5d93bfd7395 100644 --- a/sys/i386/i386/bpf_jit_machdep.c +++ b/sys/i386/i386/bpf_jit_machdep.c @@ -185,7 +185,7 @@ bpf_jit_compile(struct bpf_insn *prog, u_int nins, size_t *size) /* Allocate the reference table for the jumps. */ if (fjmp) { #ifdef _KERNEL - stream.refs = malloc((nins + 1) * sizeof(u_int), M_BPFJIT, + stream.refs = mallocarray(nins + 1, sizeof(u_int), M_BPFJIT, M_NOWAIT | M_ZERO); #else stream.refs = calloc(nins + 1, sizeof(u_int)); diff --git a/sys/i386/i386/k6_mem.c b/sys/i386/i386/k6_mem.c index 2968518ee534..cccd7eca8e9e 100644 --- a/sys/i386/i386/k6_mem.c +++ b/sys/i386/i386/k6_mem.c @@ -107,7 +107,7 @@ k6_mrinit(struct mem_range_softc *sc) sc->mr_cap = 0; sc->mr_ndesc = 2; /* XXX (BFF) For now, we only have one msr for this */ - sc->mr_desc = malloc(sc->mr_ndesc * sizeof(struct mem_range_desc), + sc->mr_desc = mallocarray(sc->mr_ndesc, sizeof(struct mem_range_desc), M_MEMDESC, M_NOWAIT | M_ZERO); if (sc->mr_desc == NULL) panic("k6_mrinit: malloc returns NULL"); diff --git a/sys/x86/cpufreq/est.c b/sys/x86/cpufreq/est.c index d83cc98154b9..f13eb86d7ddb 100644 --- a/sys/x86/cpufreq/est.c +++ b/sys/x86/cpufreq/est.c @@ -1119,7 +1119,7 @@ est_acpi_info(device_t dev, freq_info **freqs) goto out; /* Parse settings into our local table format. */ - table = malloc((count + 1) * sizeof(freq_info), M_DEVBUF, M_NOWAIT); + table = mallocarray(count + 1, sizeof(freq_info), M_DEVBUF, M_NOWAIT); if (table == NULL) { error = ENOMEM; goto out;