History

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.

Gustavo A. R. Silva committed 7 years ago

Commit 43e11fa1 parent 79eb5971 file · +3 −3Browse files

Unified · Split

fs/select.c+3−3
@@ -961,7 +961,7 @@ static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds, · 960 unmodified lines
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, · 21 unmodified lines
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;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t