treewide: Use struct_size() for devm_kmalloc() and friends
authorKees Cook <keescook@chromium.org>
Tue, 8 May 2018 23:08:53 +0000 (16:08 -0700)
committerKees Cook <keescook@chromium.org>
Wed, 6 Jun 2018 18:15:43 +0000 (11:15 -0700)
Replaces open-coded struct size calculations with struct_size() for
devm_*, f2fs_*, and sock_* allocations. Automatically generated (and
manually adjusted) from the following Coccinelle script:

// Direct reference to struct field.
@@
identifier alloc =~ "devm_kmalloc|devm_kzalloc|sock_kmalloc|f2fs_kmalloc|f2fs_kzalloc";
expression HANDLE;
expression GFP;
identifier VAR, ELEMENT;
expression COUNT;
@@

- alloc(HANDLE, sizeof(*VAR) + COUNT * sizeof(*VAR->ELEMENT), GFP)
+ alloc(HANDLE, struct_size(VAR, ELEMENT, COUNT), GFP)

// mr = kzalloc(sizeof(*mr) + m * sizeof(mr->map[0]), GFP_KERNEL);
@@
identifier alloc =~ "devm_kmalloc|devm_kzalloc|sock_kmalloc|f2fs_kmalloc|f2fs_kzalloc";
expression HANDLE;
expression GFP;
identifier VAR, ELEMENT;
expression COUNT;
@@

- alloc(HANDLE, sizeof(*VAR) + COUNT * sizeof(VAR->ELEMENT[0]), GFP)
+ alloc(HANDLE, struct_size(VAR, ELEMENT, COUNT), GFP)

// Same pattern, but can't trivially locate the trailing element name,
// or variable name.
@@
identifier alloc =~ "devm_kmalloc|devm_kzalloc|sock_kmalloc|f2fs_kmalloc|f2fs_kzalloc";
expression HANDLE;
expression GFP;
expression SOMETHING, COUNT, ELEMENT;
@@

- alloc(HANDLE, sizeof(SOMETHING) + COUNT * sizeof(ELEMENT), GFP)
+ alloc(HANDLE, CHECKME_struct_size(&SOMETHING, ELEMENT, COUNT), GFP)

Signed-off-by: Kees Cook <keescook@chromium.org>
32 files changed:
crypto/af_alg.c
drivers/clk/bcm/clk-bcm2835-aux.c
drivers/clk/bcm/clk-bcm2835.c
drivers/clk/clk-s2mps11.c
drivers/clk/clk-scmi.c
drivers/clk/davinci/da8xx-cfgchip.c
drivers/clk/mvebu/armada-37xx-periph.c
drivers/clk/mvebu/armada-37xx-tbg.c
drivers/clk/qcom/clk-spmi-pmic-div.c
drivers/clk/samsung/clk-exynos-audss.c
drivers/clk/samsung/clk-exynos5433.c
drivers/clk/samsung/clk-s3c2410-dclk.c
drivers/clk/samsung/clk-s5pv210-audss.c
drivers/dma/bcm-sba-raid.c
drivers/dma/nbpfaxi.c
drivers/dma/sprd-dma.c
drivers/gpio/gpio-uniphier.c
drivers/hwspinlock/sirf_hwspinlock.c
drivers/input/keyboard/cap11xx.c
drivers/mfd/qcom-pm8xxx.c
drivers/misc/cb710/core.c
drivers/mtd/spi-nor/aspeed-smc.c
drivers/net/can/peak_canfd/peak_pciefd_main.c
drivers/pinctrl/samsung/pinctrl-s3c64xx.c
drivers/pinctrl/uniphier/pinctrl-uniphier-core.c
drivers/regulator/mc13783-regulator.c
drivers/regulator/mc13892-regulator.c
drivers/rtc/rtc-ac100.c
drivers/soc/actions/owl-sps.c
drivers/soc/rockchip/pm_domains.c
drivers/thermal/qcom/tsens.c
sound/soc/qcom/apq8016_sbc.c

index 7846c0c20cfec1998674e18e0a516a89caea509f..bcbf6774f4318558c80f9db9edfa062cdb5d8f15 100644 (file)
@@ -501,8 +501,8 @@ int af_alg_alloc_tsgl(struct sock *sk)
                sg = sgl->sg;
 
        if (!sg || sgl->cur >= MAX_SGL_ENTS) {
-               sgl = sock_kmalloc(sk, sizeof(*sgl) +
-                                      sizeof(sgl->sg[0]) * (MAX_SGL_ENTS + 1),
+               sgl = sock_kmalloc(sk,
+                                  struct_size(sgl, sg, (MAX_SGL_ENTS + 1)),
                                   GFP_KERNEL);
                if (!sgl)
                        return -ENOMEM;
index 77e276d61702a4e333829a7fd6412a39fa83b19b..f225ad29b1107df4b2777ba68aab9ca9aa5fb033 100644 (file)
@@ -40,8 +40,10 @@ static int bcm2835_aux_clk_probe(struct platform_device *pdev)
        if (IS_ERR(reg))
                return PTR_ERR(reg);
 
-       onecell = devm_kmalloc(dev, sizeof(*onecell) + sizeof(*onecell->hws) *
-                              BCM2835_AUX_CLOCK_COUNT, GFP_KERNEL);
+       onecell = devm_kmalloc(dev,
+                              struct_size(onecell, hws,
+                                          BCM2835_AUX_CLOCK_COUNT),
+                              GFP_KERNEL);
        if (!onecell)
                return -ENOMEM;
        onecell->num = BCM2835_AUX_CLOCK_COUNT;
index fa0d5c8611a0dc0440464ae8d71a44d5b37a7712..6d4e69edfb368c771e3696b250e25e1f0f205d7d 100644 (file)
@@ -2147,8 +2147,8 @@ static int bcm2835_clk_probe(struct platform_device *pdev)
        size_t i;
        int ret;
 
-       cprman = devm_kzalloc(dev, sizeof(*cprman) +
-                             sizeof(*cprman->onecell.hws) * asize,
+       cprman = devm_kzalloc(dev,
+                             struct_size(cprman, onecell.hws, asize),
                              GFP_KERNEL);
        if (!cprman)
                return -ENOMEM;
index fbaa84a33c46c4e5b0160c154c1b3fd9a30b1788..d44e0eea31ec6de81e471469eee884f37a559fca 100644 (file)
@@ -147,8 +147,8 @@ static int s2mps11_clk_probe(struct platform_device *pdev)
        if (!s2mps11_clks)
                return -ENOMEM;
 
-       clk_data = devm_kzalloc(&pdev->dev, sizeof(*clk_data) +
-                               sizeof(*clk_data->hws) * S2MPS11_CLKS_NUM,
+       clk_data = devm_kzalloc(&pdev->dev,
+                               struct_size(clk_data, hws, S2MPS11_CLKS_NUM),
                                GFP_KERNEL);
        if (!clk_data)
                return -ENOMEM;
index 488c21376b555daaa2487e31b6037de3ae539c46..bb2a6f2f5516756c6050b4977964c4746901f6e4 100644 (file)
@@ -137,8 +137,8 @@ static int scmi_clocks_probe(struct scmi_device *sdev)
                return -EINVAL;
        }
 
-       clk_data = devm_kzalloc(dev, sizeof(*clk_data) +
-                               sizeof(*clk_data->hws) * count, GFP_KERNEL);
+       clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, count),
+                               GFP_KERNEL);
        if (!clk_data)
                return -ENOMEM;
 
index c971111d2601ca898f8df90360b90c18db63a12d..aae62a5b8734e859e76a16f995b05f43bf5f9b4a 100644 (file)
@@ -650,8 +650,8 @@ static int of_da8xx_usb_phy_clk_init(struct device *dev, struct regmap *regmap)
        struct da8xx_usb0_clk48 *usb0;
        struct da8xx_usb1_clk48 *usb1;
 
-       clk_data = devm_kzalloc(dev, sizeof(*clk_data) + 2 *
-                               sizeof(*clk_data->hws), GFP_KERNEL);
+       clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, 2),
+                               GFP_KERNEL);
        if (!clk_data)
                return -ENOMEM;
 
index 87213ea7fc84f32690d0849c6006b86882636a27..6860bd5a37c5e50e9be9e26c40981dea33d57dfc 100644 (file)
@@ -667,9 +667,10 @@ static int armada_3700_periph_clock_probe(struct platform_device *pdev)
        if (!driver_data)
                return -ENOMEM;
 
-       driver_data->hw_data = devm_kzalloc(dev, sizeof(*driver_data->hw_data) +
-                           sizeof(*driver_data->hw_data->hws) * num_periph,
-                           GFP_KERNEL);
+       driver_data->hw_data = devm_kzalloc(dev,
+                                           struct_size(driver_data->hw_data,
+                                                       hws, num_periph),
+                                           GFP_KERNEL);
        if (!driver_data->hw_data)
                return -ENOMEM;
        driver_data->hw_data->num = num_periph;
index aa80db11f543c8b6e64ef7db04096d574958e1ed..7ff041f73b5530b1bff020d9ad21c32b3f808050 100644 (file)
@@ -91,8 +91,8 @@ static int armada_3700_tbg_clock_probe(struct platform_device *pdev)
        void __iomem *reg;
        int i, ret;
 
-       hw_tbg_data = devm_kzalloc(&pdev->dev, sizeof(*hw_tbg_data)
-                                  + sizeof(*hw_tbg_data->hws) * NUM_TBG,
+       hw_tbg_data = devm_kzalloc(&pdev->dev,
+                                  struct_size(hw_tbg_data, hws, NUM_TBG),
                                   GFP_KERNEL);
        if (!hw_tbg_data)
                return -ENOMEM;
index 8672ab84746f7ab4dd371c9bfcace7def981ea42..c90dfdd6c147ff9198559bfa06be3fff6cbf5651 100644 (file)
@@ -239,8 +239,7 @@ static int spmi_pmic_clkdiv_probe(struct platform_device *pdev)
        if (!nclks)
                return -EINVAL;
 
-       cc = devm_kzalloc(dev, sizeof(*cc) + sizeof(*cc->clks) * nclks,
-                         GFP_KERNEL);
+       cc = devm_kzalloc(dev, struct_size(cc, clks, nclks), GFP_KERNEL);
        if (!cc)
                return -ENOMEM;
        cc->nclks = nclks;
index b4b057c7301c0147728a38e8200792a41c9d5100..f659c5cbf1d5d6d70a965582166221e61911cfb1 100644 (file)
@@ -149,8 +149,8 @@ static int exynos_audss_clk_probe(struct platform_device *pdev)
        epll = ERR_PTR(-ENODEV);
 
        clk_data = devm_kzalloc(dev,
-                               sizeof(*clk_data) +
-                               sizeof(*clk_data->hws) * EXYNOS_AUDSS_MAX_CLKS,
+                               struct_size(clk_data, hws,
+                                           EXYNOS_AUDSS_MAX_CLKS),
                                GFP_KERNEL);
        if (!clk_data)
                return -ENOMEM;
index 5305ace514b2dd03c922cf12f9e53d01505c8595..162de44df099bff5be3a70e4fd5b010e1fc584f5 100644 (file)
@@ -5505,8 +5505,8 @@ static int __init exynos5433_cmu_probe(struct platform_device *pdev)
 
        info = of_device_get_match_data(dev);
 
-       data = devm_kzalloc(dev, sizeof(*data) +
-                           sizeof(*data->ctx.clk_data.hws) * info->nr_clk_ids,
+       data = devm_kzalloc(dev,
+                           struct_size(data, ctx.clk_data.hws, info->nr_clk_ids),
                            GFP_KERNEL);
        if (!data)
                return -ENOMEM;
index 077df3e539a71fef9a1ebfd98424525aeabb4885..66a904758761bfde5a2501b78b027e5803540617 100644 (file)
@@ -247,9 +247,10 @@ static int s3c24xx_dclk_probe(struct platform_device *pdev)
        struct clk_hw **clk_table;
        int ret, i;
 
-       s3c24xx_dclk = devm_kzalloc(&pdev->dev, sizeof(*s3c24xx_dclk) +
-                           sizeof(*s3c24xx_dclk->clk_data.hws) * DCLK_MAX_CLKS,
-                           GFP_KERNEL);
+       s3c24xx_dclk = devm_kzalloc(&pdev->dev,
+                                   struct_size(s3c24xx_dclk, clk_data.hws,
+                                               DCLK_MAX_CLKS),
+                                   GFP_KERNEL);
        if (!s3c24xx_dclk)
                return -ENOMEM;
 
index b9641414ddc6a59b1ea4e510658cd6acd7739af0..22b18e728b88a8f5ed7684045e5a854bb978ba93 100644 (file)
@@ -81,8 +81,7 @@ static int s5pv210_audss_clk_probe(struct platform_device *pdev)
        }
 
        clk_data = devm_kzalloc(&pdev->dev,
-                               sizeof(*clk_data) +
-                               sizeof(*clk_data->hws) * AUDSS_MAX_CLKS,
+                               struct_size(clk_data, hws, AUDSS_MAX_CLKS),
                                GFP_KERNEL);
 
        if (!clk_data)
index 3956a018bf5ac9bcef3a425ab537c59630d431e0..72878ac5c78d61ead59059932f0bf22efd93c93b 100644 (file)
@@ -1499,9 +1499,8 @@ static int sba_prealloc_channel_resources(struct sba_device *sba)
 
        for (i = 0; i < sba->max_req; i++) {
                req = devm_kzalloc(sba->dev,
-                               sizeof(*req) +
-                               sba->max_cmd_per_req * sizeof(req->cmds[0]),
-                               GFP_KERNEL);
+                                  struct_size(req, cmds, sba->max_cmd_per_req),
+                                  GFP_KERNEL);
                if (!req) {
                        ret = -ENOMEM;
                        goto fail_free_cmds_pool;
index 50559338239b6ec586e1366314bac315c7cf3269..2f9974ddfbb2fde6f20d49689502156936452eb5 100644 (file)
@@ -1305,8 +1305,8 @@ static int nbpf_probe(struct platform_device *pdev)
        cfg = of_device_get_match_data(dev);
        num_channels = cfg->num_channels;
 
-       nbpf = devm_kzalloc(dev, sizeof(*nbpf) + num_channels *
-                           sizeof(nbpf->chan[0]), GFP_KERNEL);
+       nbpf = devm_kzalloc(dev, struct_size(nbpf, chan, num_channels),
+                           GFP_KERNEL);
        if (!nbpf)
                return -ENOMEM;
 
index b106e8a60af642ad213e1a21a08b24d749d4673f..52ebccb483bed15c480134b5e176f4e0b22acca9 100644 (file)
@@ -805,8 +805,8 @@ static int sprd_dma_probe(struct platform_device *pdev)
                return ret;
        }
 
-       sdev = devm_kzalloc(&pdev->dev, sizeof(*sdev) +
-                           sizeof(*dma_chn) * chn_count,
+       sdev = devm_kzalloc(&pdev->dev,
+                           struct_size(sdev, channels, chn_count),
                            GFP_KERNEL);
        if (!sdev)
                return -ENOMEM;
index 761d8279abca20b4cad83a54c8ace87ee38de8e8..d3cf9502e7e7f46abeb366c735f2e47d6808e64d 100644 (file)
@@ -371,8 +371,7 @@ static int uniphier_gpio_probe(struct platform_device *pdev)
                return ret;
 
        nregs = uniphier_gpio_get_nbanks(ngpios) * 2 + 3;
-       priv = devm_kzalloc(dev,
-                           sizeof(*priv) + sizeof(priv->saved_vals[0]) * nregs,
+       priv = devm_kzalloc(dev, struct_size(priv, saved_vals, nregs),
                            GFP_KERNEL);
        if (!priv)
                return -ENOMEM;
index 16018544d4317a6aeb323640b09f431ec4b9f30d..cb38e487c6c42b7ed822f6f551e69856ccaf1788 100644 (file)
@@ -62,8 +62,10 @@ static int sirf_hwspinlock_probe(struct platform_device *pdev)
        if (!pdev->dev.of_node)
                return -ENODEV;
 
-       hwspin = devm_kzalloc(&pdev->dev, sizeof(*hwspin) +
-                       sizeof(*hwlock) * HW_SPINLOCK_NUMBER, GFP_KERNEL);
+       hwspin = devm_kzalloc(&pdev->dev,
+                             struct_size(hwspin, bank.lock,
+                                         HW_SPINLOCK_NUMBER),
+                             GFP_KERNEL);
        if (!hwspin)
                return -ENOMEM;
 
index 1a1eacae3ea14295f7b1284d83dd9b0f68ef7a29..312916f99597ad5dc6949296edc7b42cd459f9b8 100644 (file)
@@ -357,8 +357,7 @@ static int cap11xx_i2c_probe(struct i2c_client *i2c_client,
        }
 
        priv = devm_kzalloc(dev,
-                           sizeof(*priv) +
-                               cap->num_channels * sizeof(priv->keycodes[0]),
+                           struct_size(priv, keycodes, cap->num_channels),
                            GFP_KERNEL);
        if (!priv)
                return -ENOMEM;
index f08758f6b418f02fc1772770d321e9770b9d9435..e6e8d81c15fd4731260cd85a88a09bd9f8a93ece 100644 (file)
@@ -563,8 +563,8 @@ static int pm8xxx_probe(struct platform_device *pdev)
        pr_info("PMIC revision 2: %02X\n", val);
        rev |= val << BITS_PER_BYTE;
 
-       chip = devm_kzalloc(&pdev->dev, sizeof(*chip) +
-                           sizeof(chip->config[0]) * data->num_irqs,
+       chip = devm_kzalloc(&pdev->dev,
+                           struct_size(chip, config, data->num_irqs),
                            GFP_KERNEL);
        if (!chip)
                return -ENOMEM;
index fb397e7d1cce8a17ab2583c2bedc49438d72b03b..4d4acf763b655c72353ce4e9d2bbff9d962dd957 100644 (file)
@@ -232,8 +232,8 @@ static int cb710_probe(struct pci_dev *pdev,
        if (val & CB710_SLOT_SM)
                ++n;
 
-       chip = devm_kzalloc(&pdev->dev,
-               sizeof(*chip) + n * sizeof(*chip->slot), GFP_KERNEL);
+       chip = devm_kzalloc(&pdev->dev, struct_size(chip, slot, n),
+                           GFP_KERNEL);
        if (!chip)
                return -ENOMEM;
 
index 8d3cbe27efb645c34f9e390b665685449edee98b..95e54468cf7d755802cfdcda6b35fc0c746af8f7 100644 (file)
@@ -861,8 +861,9 @@ static int aspeed_smc_probe(struct platform_device *pdev)
                return -ENODEV;
        info = match->data;
 
-       controller = devm_kzalloc(&pdev->dev, sizeof(*controller) +
-               info->nce * sizeof(controller->chips[0]), GFP_KERNEL);
+       controller = devm_kzalloc(&pdev->dev,
+                                 struct_size(controller, chips, info->nce),
+                                 GFP_KERNEL);
        if (!controller)
                return -ENOMEM;
        controller->info = info;
index 3c51a884db87bc90e71d5df8d5b0a91eadf69cdb..b9e28578bc7bd7463485c316020af6e9cdaf9d02 100644 (file)
@@ -752,8 +752,7 @@ static int peak_pciefd_probe(struct pci_dev *pdev,
                can_count = 1;
 
        /* allocate board structure object */
-       pciefd = devm_kzalloc(&pdev->dev, sizeof(*pciefd) +
-                             can_count * sizeof(*pciefd->can),
+       pciefd = devm_kzalloc(&pdev->dev, struct_size(pciefd, can, can_count),
                              GFP_KERNEL);
        if (!pciefd) {
                err = -ENOMEM;
index 288e6567ceb1bf233a748d838dd03eae71270cb5..c399f0932af5e26eb3d9af531bc91975abd9d18c 100644 (file)
@@ -483,8 +483,8 @@ static int s3c64xx_eint_gpio_init(struct samsung_pinctrl_drv_data *d)
                ++nr_domains;
        }
 
-       data = devm_kzalloc(dev, sizeof(*data)
-                       + nr_domains * sizeof(*data->domains), GFP_KERNEL);
+       data = devm_kzalloc(dev, struct_size(data, domains, nr_domains),
+                           GFP_KERNEL);
        if (!data)
                return -ENOMEM;
        data->drvdata = d;
index ec0f77afeaa493f3ef0fb5c8ae63de3fc6e92a48..add8e870667b3d2c862c55a78b468366c07c25bc 100644 (file)
@@ -759,8 +759,7 @@ static int uniphier_pinctrl_add_reg_region(struct device *dev,
 
        nregs = DIV_ROUND_UP(count * width, 32);
 
-       region = devm_kzalloc(dev,
-                             sizeof(*region) + sizeof(region->vals[0]) * nregs,
+       region = devm_kzalloc(dev, struct_size(region, vals, nregs),
                              GFP_KERNEL);
        if (!region)
                return -ENOMEM;
index fe4c7d677f9c7b3b37143bc1e71ed07eafaf1fd2..0e0277bd91a8a961f13699af422293d90e4a172f 100644 (file)
@@ -409,9 +409,9 @@ static int mc13783_regulator_probe(struct platform_device *pdev)
        if (num_regulators <= 0)
                return -EINVAL;
 
-       priv = devm_kzalloc(&pdev->dev, sizeof(*priv) +
-                       num_regulators * sizeof(priv->regulators[0]),
-                       GFP_KERNEL);
+       priv = devm_kzalloc(&pdev->dev,
+                           struct_size(priv, regulators, num_regulators),
+                           GFP_KERNEL);
        if (!priv)
                return -ENOMEM;
 
index 0d17c92068162cc1c0810150716985e9812615c8..15dd7bc7b529a2c6c138b52ad52c61b592378c44 100644 (file)
@@ -547,9 +547,9 @@ static int mc13892_regulator_probe(struct platform_device *pdev)
        if (num_regulators <= 0)
                return -EINVAL;
 
-       priv = devm_kzalloc(&pdev->dev, sizeof(*priv) +
-               num_regulators * sizeof(priv->regulators[0]),
-               GFP_KERNEL);
+       priv = devm_kzalloc(&pdev->dev,
+                           struct_size(priv, regulators, num_regulators),
+                           GFP_KERNEL);
        if (!priv)
                return -ENOMEM;
 
index 3fe576fdd45e0782a1ecead2931ddf95d76d8a8d..784b676284bff57d9bb675e0f0f1b7af3113f354 100644 (file)
@@ -317,10 +317,10 @@ static int ac100_rtc_register_clks(struct ac100_rtc_dev *chip)
        const char *parents[2] = {AC100_RTC_32K_NAME};
        int i, ret;
 
-       chip->clk_data = devm_kzalloc(chip->dev, sizeof(*chip->clk_data) +
-                                                sizeof(*chip->clk_data->hws) *
-                                                AC100_CLKOUT_NUM,
-                                                GFP_KERNEL);
+       chip->clk_data = devm_kzalloc(chip->dev,
+                                     struct_size(chip->clk_data, hws,
+                                                 AC100_CLKOUT_NUM),
+                                     GFP_KERNEL);
        if (!chip->clk_data)
                return -ENOMEM;
 
index 8477f0f18e246375f40aa167e68cd60521de7496..032921d8d41f3cf67ccb66ddfa9c65dc4dff1e64 100644 (file)
@@ -117,8 +117,8 @@ static int owl_sps_probe(struct platform_device *pdev)
 
        sps_info = match->data;
 
-       sps = devm_kzalloc(&pdev->dev, sizeof(*sps) +
-                          sps_info->num_domains * sizeof(sps->domains[0]),
+       sps = devm_kzalloc(&pdev->dev,
+                          struct_size(sps, domains, sps_info->num_domains),
                           GFP_KERNEL);
        if (!sps)
                return -ENOMEM;
index 53efc386b1ada8cf8821403aa0eb7a333535cd30..111c44fc1c12a88bb67f46b44777465602dd1ebc 100644 (file)
@@ -626,8 +626,7 @@ static int rockchip_pm_domain_probe(struct platform_device *pdev)
        pmu_info = match->data;
 
        pmu = devm_kzalloc(dev,
-                          sizeof(*pmu) +
-                               pmu_info->num_domains * sizeof(pmu->domains[0]),
+                          struct_size(pmu, domains, pmu_info->num_domains),
                           GFP_KERNEL);
        if (!pmu)
                return -ENOMEM;
index 3f9fe6aa51ccc1eaf531c1dce51578b5d25c7271..c2c34425279df67b952c0cf269c85eeedb729fbd 100644 (file)
@@ -112,7 +112,6 @@ static int tsens_probe(struct platform_device *pdev)
        int ret, i;
        struct device *dev;
        struct device_node *np;
-       struct tsens_sensor *s;
        struct tsens_device *tmdev;
        const struct tsens_data *data;
        const struct of_device_id *id;
@@ -135,8 +134,9 @@ static int tsens_probe(struct platform_device *pdev)
                return -EINVAL;
        }
 
-       tmdev = devm_kzalloc(dev, sizeof(*tmdev) +
-                            data->num_sensors * sizeof(*s), GFP_KERNEL);
+       tmdev = devm_kzalloc(dev,
+                            struct_size(tmdev, sensor, data->num_sensors),
+                            GFP_KERNEL);
        if (!tmdev)
                return -ENOMEM;
 
index 704428735e3c978688505a786e5053b021a8d27b..1dd23bba1bedb1e0e6359b778956a324fb40fc40 100644 (file)
@@ -147,7 +147,8 @@ static struct apq8016_sbc_data *apq8016_sbc_parse_of(struct snd_soc_card *card)
        num_links = of_get_child_count(node);
 
        /* Allocate the private data and the DAI link array */
-       data = devm_kzalloc(dev, sizeof(*data) + sizeof(*link) * num_links,
+       data = devm_kzalloc(dev,
+                           struct_size(data, dai_link, num_links),
                            GFP_KERNEL);
        if (!data)
                return ERR_PTR(-ENOMEM);