diff options
author | Gustavo A. R. Silva <gustavoars@kernel.org> | 2020-05-24 22:52:38 +0200 |
---|---|---|
committer | Paul Moore <paul@paul-moore.com> | 2020-06-17 22:43:11 +0200 |
commit | bbccc11bc8848926065915e6193fd4c6e33c85ef (patch) | |
tree | 87c2225344fc30b92dd8035e1ad10947359f7545 /kernel/audit_tree.c | |
parent | Linux 5.8-rc1 (diff) | |
download | linux-bbccc11bc8848926065915e6193fd4c6e33c85ef.tar.xz linux-bbccc11bc8848926065915e6193fd4c6e33c85ef.zip |
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 <gustavoars@kernel.org>
Signed-off-by: Paul Moore <paul@paul-moore.com>
Diffstat (limited to 'kernel/audit_tree.c')
-rw-r--r-- | kernel/audit_tree.c | 4 |
1 files changed, 1 insertions, 3 deletions
diff --git a/kernel/audit_tree.c b/kernel/audit_tree.c index e49c912f862d..1b7a2f041793 100644 --- a/kernel/audit_tree.c +++ b/kernel/audit_tree.c @@ -188,11 +188,9 @@ static struct fsnotify_mark *alloc_mark(void) static struct audit_chunk *alloc_chunk(int count) { struct audit_chunk *chunk; - size_t size; int i; - size = offsetof(struct audit_chunk, owners) + count * sizeof(struct node); - chunk = kzalloc(size, GFP_KERNEL); + chunk = kzalloc(struct_size(chunk, owners, count), GFP_KERNEL); if (!chunk) return NULL; |