]> asedeno.scripts.mit.edu Git - linux.git/commit
mtd: cfi: cmdset_0001: Use struct_size() in kmalloc()
authorGustavo A. R. Silva <gustavo@embeddedor.com>
Tue, 8 Jan 2019 15:52:44 +0000 (09:52 -0600)
committerBoris Brezillon <bbrezillon@kernel.org>
Tue, 15 Jan 2019 19:49:31 +0000 (20:49 +0100)
commit04b4c06caf2b810f0ce4822f6611ed35326ca11a
tree1c253be8681376f142cdc4c0b9a954f699338346
parent9cb76a6aa1a9524866ec1558f08d044506b707fa
mtd: cfi: cmdset_0001: Use struct_size() in kmalloc()

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 foo {
    int stuff;
    void *entry[];
};

instance = kmalloc(sizeof(struct foo) + sizeof(void *) * count, GFP_KERNEL);

Instead of leaving these open-coded and prone to type mistakes, we can
now use the new struct_size() helper:

instance = kmalloc(struct_size(instance, entry, count), GFP_KERNEL);

This code was detected with the help of Coccinelle.

Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Signed-off-by: Boris Brezillon <bbrezillon@kernel.org>
drivers/mtd/chips/cfi_cmdset_0001.c