Commit 4a2ae92993be24ba727faa733e99d7980d389ec0

Authored by Gustavo A. R. Silva
Committed by Linus Torvalds
1 parent 667da6a268

ipc/sem.c: replace kvmalloc/memset with kvzalloc and use struct_size

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 <gustavo@embeddedor.com>
Reviewed-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Manfred Spraul <manfred@colorfullife.com>
Cc: Kees Cook <keescook@chromium.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Showing 1 changed file with 1 additions and 5 deletions Side-by-side Diff

... ... @@ -488,17 +488,13 @@
488 488 static struct sem_array *sem_alloc(size_t nsems)
489 489 {
490 490 struct sem_array *sma;
491   - size_t size;
492 491  
493 492 if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
494 493 return NULL;
495 494  
496   - size = sizeof(*sma) + nsems * sizeof(sma->sems[0]);
497   - sma = kvmalloc(size, GFP_KERNEL);
  495 + sma = kvzalloc(struct_size(sma, sems, nsems), GFP_KERNEL);
498 496 if (unlikely(!sma))
499 497 return NULL;
500   -
501   - memset(sma, 0, size);
502 498  
503 499 return sma;
504 500 }