Skip to content

Commit f402cf0

Browse files
GustavoARSilvatorvalds
authored andcommitted
ocfs2: Use zero-sized array and struct_size() in kzalloc()
Update the code to use a zero-sized array instead of a pointer in structure ocfs2_slot_info and use struct_size() in kzalloc(). Notice that 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); This code was detected with the help of Coccinelle. Link: http://lkml.kernel.org/r/20190108191903.GA22056@embeddedor Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com> Reviewed-by: Andrew Morton <akpm@linux-foundation.org> Cc: Mark Fasheh <mfasheh@versity.com> Cc: Joel Becker <jlbec@evilplan.org> Cc: Junxiao Bi <junxiao.bi@oracle.com> Cc: Joseph Qi <joseph.qi@huawei.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 5500ab4 commit f402cf0

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

fs/ocfs2/slot_map.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ struct ocfs2_slot_info {
5555
unsigned int si_blocks;
5656
struct buffer_head **si_bh;
5757
unsigned int si_num_slots;
58-
struct ocfs2_slot *si_slots;
58+
struct ocfs2_slot si_slots[];
5959
};
6060

6161

@@ -420,9 +420,7 @@ int ocfs2_init_slot_info(struct ocfs2_super *osb)
420420
struct inode *inode = NULL;
421421
struct ocfs2_slot_info *si;
422422

423-
si = kzalloc(sizeof(struct ocfs2_slot_info) +
424-
(sizeof(struct ocfs2_slot) * osb->max_slots),
425-
GFP_KERNEL);
423+
si = kzalloc(struct_size(si, si_slots, osb->max_slots), GFP_KERNEL);
426424
if (!si) {
427425
status = -ENOMEM;
428426
mlog_errno(status);
@@ -431,8 +429,6 @@ int ocfs2_init_slot_info(struct ocfs2_super *osb)
431429

432430
si->si_extended = ocfs2_uses_extended_slot_map(osb);
433431
si->si_num_slots = osb->max_slots;
434-
si->si_slots = (struct ocfs2_slot *)((char *)si +
435-
sizeof(struct ocfs2_slot_info));
436432

437433
inode = ocfs2_get_system_file_inode(osb, SLOT_MAP_SYSTEM_INODE,
438434
OCFS2_INVALID_SLOT);

0 commit comments

Comments
 (0)