Skip to contentSkip to conversation

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

PATCH New Gustavo A. R. Silva 1 participant 1 message Jan 31, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 ipc/sem.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
ipc/sem.c +1 -5
index a188d1b064ea..7da4504bcc7c 100644
@@ -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;
 }
 
Signature · 1 line
2.20.1