• Gustavo A. R. Silva's avatar
    clk: npcm7xx: fix memory allocation · 450b6b9b
    Gustavo A. R. Silva authored
    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 = kzalloc(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 = kzalloc(struct_size(instance, entry, count), GFP_KERNEL);
    
    Notice that, currently, there is a bug during the allocation:
    
    sizeof(npcm7xx_clk_data) should be sizeof(*npcm7xx_clk_data)
    
    Fix this bug by using struct_size() in kzalloc()
    
    This issue was detected with the help of Coccinelle.
    
    Cc: stable@vger.kernel.org
    Signed-off-by: default avatarGustavo A. R. Silva <gustavo@embeddedor.com>
    Reviewed-by: default avatarKees Cook <keescook@chromium.org>
    Reviewed-by: default avatarAvi Fishman <avifishman70@gmail.com>
    Signed-off-by: default avatarStephen Boyd <sboyd@kernel.org>
    450b6b9b
clk-npcm7xx.c 21.6 KB