Skip to content

Commit bbccc11

Browse files
GustavoARSilvapcmoore
authored andcommitted
audit: Use struct_size() helper in alloc_chunk
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 audit_chunk { ... struct node { struct list_head list; struct audit_tree *owner; unsigned index; /* index; upper bit indicates 'will prune' */ } owners[]; }; Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes. So, replace the following form: offsetof(struct audit_chunk, owners) + count * sizeof(struct node); with: struct_size(chunk, owners, count) This code was detected with the help of Coccinelle. Signed-off-by: Gustavo A. R. Silva <[email protected]> Signed-off-by: Paul Moore <[email protected]>
1 parent b3a9e3b commit bbccc11

File tree

1 file changed

+1
-3
lines changed

1 file changed

+1
-3
lines changed

kernel/audit_tree.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,9 @@ static struct fsnotify_mark *alloc_mark(void)
188188
static struct audit_chunk *alloc_chunk(int count)
189189
{
190190
struct audit_chunk *chunk;
191-
size_t size;
192191
int i;
193192

194-
size = offsetof(struct audit_chunk, owners) + count * sizeof(struct node);
195-
chunk = kzalloc(size, GFP_KERNEL);
193+
chunk = kzalloc(struct_size(chunk, owners, count), GFP_KERNEL);
196194
if (!chunk)
197195
return NULL;
198196

0 commit comments

Comments
 (0)