]> asedeno.scripts.mit.edu Git - linux.git/commit
libceph: use struct_size() for kmalloc() in crush_decode()
authorGustavo A. R. Silva <gustavo@embeddedor.com>
Tue, 15 Jan 2019 19:41:53 +0000 (13:41 -0600)
committerIlya Dryomov <idryomov@gmail.com>
Tue, 5 Mar 2019 17:55:17 +0000 (18:55 +0100)
commit6b41d4d9cb8f48363e50990e00c64c3fde355d11
treeef81b3996979c01fe39744dcac88e8ad99863d6e
parente3ec8d6898f71636a067dae683174ef9bf81bc96
libceph: use struct_size() for kmalloc() in crush_decode()

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;
    struct boo entry[];
};

instance = kmalloc(sizeof(struct foo) + count * sizeof(struct boo), 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>
Reviewed-by: Ilya Dryomov <idryomov@gmail.com>
Signed-off-by: Ilya Dryomov <idryomov@gmail.com>
net/ceph/osdmap.c