]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
USB: core: urb: Use struct_size() in kmalloc()
authorGustavo A. R. Silva <gustavo@embeddedor.com>
Tue, 8 Jan 2019 15:22:25 +0000 (09:22 -0600)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 8 Jan 2019 15:46:46 +0000 (16:46 +0100)
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: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/core/urb.c

index f51750bcd1528ba5aa3c8996b9dd0bc320ce8beb..0eab79f82ce41bd107de6e2997543a0aabfaf3a6 100644 (file)
@@ -70,9 +70,8 @@ struct urb *usb_alloc_urb(int iso_packets, gfp_t mem_flags)
 {
        struct urb *urb;
 
-       urb = kmalloc(sizeof(struct urb) +
-               iso_packets * sizeof(struct usb_iso_packet_descriptor),
-               mem_flags);
+       urb = kmalloc(struct_size(urb, iso_frame_desc, iso_packets),
+                     mem_flags);
        if (!urb)
                return NULL;
        usb_init_urb(urb);