dmaengine: stm32-dma: Use struct_size() helper
authorGustavo A. R. Silva <gustavo@embeddedor.com>
Fri, 30 Aug 2019 16:14:23 +0000 (11:14 -0500)
committerVinod Koul <vkoul@kernel.org>
Wed, 4 Sep 2019 04:55:08 +0000 (10:25 +0530)
commit402096cb5b7d9ae9c69b851f5ec6289d249a9ed5
treec4c60479dcf136185036408d5d011f220e1f7190
parent37256335bd065e200c7e42cfd27b26b688606466
dmaengine: stm32-dma: Use struct_size() helper

One of the more common cases of allocation size calculations is finding
the size of a structure that has a zero-sized array at the end, along
with memory for some number of elements for that array. For example:

struct stm32_dma_desc {
...
        struct stm32_dma_sg_req sg_req[];
};

Make use of the struct_size() helper instead of an open-coded version
in order to avoid any potential type mistakes.

So, replace the following function:

static struct stm32_dma_desc *stm32_dma_alloc_desc(u32 num_sgs)
{
       return kzalloc(sizeof(struct stm32_dma_desc) +
                      sizeof(struct stm32_dma_sg_req) * num_sgs, GFP_NOWAIT);
}

with:

kzalloc(struct_size(desc, sg_req, num_sgs), GFP_NOWAIT)

This code was detected with the help of Coccinelle.

Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Link: https://lore.kernel.org/r/20190830161423.GA3483@embeddedor
Signed-off-by: Vinod Koul <vkoul@kernel.org>
drivers/dma/stm32-dma.c