• Kees Cook's avatar
    treewide: Use struct_size() for vmalloc()-family · b4b06db1
    Kees Cook authored
    This only finds one hit in the entire tree, but here's the Coccinelle:
    
    // Directly refer to structure's field
    @@
    identifier alloc =~ "vmalloc|vzalloc";
    identifier VAR, ELEMENT;
    expression COUNT;
    @@
    
    - alloc(sizeof(*VAR) + COUNT * sizeof(*VAR->ELEMENT))
    + alloc(struct_size(VAR, ELEMENT, COUNT))
    
    // mr = kzalloc(sizeof(*mr) + m * sizeof(mr->map[0]), GFP_KERNEL);
    @@
    identifier alloc =~ "vmalloc|vzalloc";
    identifier VAR, ELEMENT;
    expression COUNT;
    @@
    
    - alloc(sizeof(*VAR) + COUNT * sizeof(VAR->ELEMENT[0]))
    + alloc(struct_size(VAR, ELEMENT, COUNT))
    
    // Same pattern, but can't trivially locate the trailing element name,
    // or variable name.
    @@
    identifier alloc =~ "vmalloc|vzalloc";
    expression SOMETHING, COUNT, ELEMENT;
    @@
    
    - alloc(sizeof(SOMETHING) + COUNT * sizeof(ELEMENT))
    + alloc(CHECKME_struct_size(&SOMETHING, ELEMENT, COUNT))
    Signed-off-by: 's avatarKees Cook <keescook@chromium.org>
    b4b06db1
ramht.c 4.01 KB