Skip to content

Commit

Permalink
ipc/sem.c: replace kvmalloc/memset with kvzalloc and use struct_size
Browse files Browse the repository at this point in the history
Use kvzalloc() instead of kvmalloc() and memset().

Also, make use of the struct_size() helper instead of the open-coded
version in order to avoid any potential type mistakes.

This code was detected with the help of Coccinelle.

Link: http://lkml.kernel.org/r/20190131214221.GA28930@embeddedor
Signed-off-by: Gustavo A. R. Silva <[email protected]>
Reviewed-by: Andrew Morton <[email protected]>
Cc: Davidlohr Bueso <[email protected]>
Cc: Manfred Spraul <[email protected]>
Cc: Kees Cook <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
GustavoARSilva authored and torvalds committed Mar 8, 2019
1 parent 667da6a commit 4a2ae92
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions ipc/sem.c
Original file line number Diff line number Diff line change
Expand Up @@ -488,18 +488,14 @@ static inline void sem_rmid(struct ipc_namespace *ns, struct sem_array *s)
static struct sem_array *sem_alloc(size_t nsems)
{
struct sem_array *sma;
size_t size;

if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
return NULL;

size = sizeof(*sma) + nsems * sizeof(sma->sems[0]);
sma = kvmalloc(size, GFP_KERNEL);
sma = kvzalloc(struct_size(sma, sems, nsems), GFP_KERNEL);
if (unlikely(!sma))
return NULL;

memset(sma, 0, size);

return sma;
}

Expand Down

0 comments on commit 4a2ae92

Please sign in to comment.