Skip to content

Commit 43e11fa

Browse files
GustavoARSilvatorvalds
authored andcommitted
fs/select.c: 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; struct boo entry[]; }; size = sizeof(struct foo) + count * sizeof(struct boo); instance = kmalloc(size, 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); Also, notice that variable size is unnecessary, hence it is removed. This code was detected with the help of Coccinelle. Link: http://lkml.kernel.org/r/20190604164226.GA13823@embeddedor Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com> Reviewed-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 79eb597 commit 43e11fa

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

‎fs/select.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
961961
struct timespec64 *end_time)
962962
{
963963
struct poll_wqueues table;
964-
int err = -EFAULT, fdcount, len, size;
964+
int err = -EFAULT, fdcount, len;
965965
/* Allocate small arguments on the stack to save memory and be
966966
faster - use long to make sure the buffer is aligned properly
967967
on 64 bit archs to avoid unaligned access */
@@ -989,8 +989,8 @@ static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
989989
break;
990990

991991
len = min(todo, POLLFD_PER_PAGE);
992-
size = sizeof(struct poll_list) + sizeof(struct pollfd) * len;
993-
walk = walk->next = kmalloc(size, GFP_KERNEL);
992+
walk = walk->next = kmalloc(struct_size(walk, entries, len),
993+
GFP_KERNEL);
994994
if (!walk) {
995995
err = -ENOMEM;
996996
goto out_fds;

0 commit comments

Comments
 (0)