Skip to contentSkip to conversation

staging: usbip: use kcalloc instead of kzalloc for array allocations

PATCH In review Djordje Zekovic 2 participants 3 messages May 23, 2014 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: usbip: use kcalloc instead of kzalloc for array allocations

The advantage of kcalloc is, that will prevent integer overflows which could result from the multiplication of number of elements and size and it is also a bit nicer to read.

---
 drivers/staging/usbip/stub_tx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/usbip/stub_tx.c +1 -1
index 1622563..4994090 100644
@@ -179,7 +179,7 @@ static int stub_send_ret_submit(struct stub_device *sdev)
 		else
 			iovnum = 2;
 
-		iov = kzalloc(iovnum * sizeof(struct kvec), GFP_KERNEL);
+		iov = kcalloc(iovnum * sizeof(struct kvec), GFP_KERNEL);
 
 		if (!iov) {
 			usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_MALLOC);
Signature · 1 line
1.8.5.2
#1 lore raw
Quoted 14 lines · Djordje Zekovic

The advantage of kcalloc is, that will prevent integer overflows which could result from the multiplication of number of elements and size and it is also a bit nicer to read.

---
 drivers/staging/usbip/stub_tx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/usbip/stub_tx.c +0 -0
index 1622563..4994090 100644

drivers/staging/usbip/stub_tx.c static int stub_send_ret_submit(struct stub_device *sdev) · line 182

Quoted 5 lines
drivers/staging/usbip/stub_tx.c +1 -1
@@ -179,7 +179,7 @@ static int stub_send_ret_submit(struct stub_device *sdev)
                else
                        iovnum = 2;
 
-               iov = kzalloc(iovnum * sizeof(struct kvec), GFP_KERNEL);
+               iov = kcalloc(iovnum * sizeof(struct kvec), GFP_KERNEL);

Does this even build?

static inline void *kcalloc(size_t n, size_t size, gfp_t flags)

Signature · 2 lines
Thanks,
//richard
#2 lore raw

PATCH v2 1/1 staging: usbip: use kcalloc instead of kzalloc for array allocations

The advantage of kcalloc is, that will prevent integer overflows which could result from the multiplication of number of elements and size and it is also a bit nicer to read.

---
v2: Fixed issue regarding kcalloc func signature, noted by
Richard Weinberger <[email protected]>

 drivers/staging/usbip/stub_tx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/usbip/stub_tx.c +1 -1
index 1622563..f09abb2 100644
@@ -179,7 +179,7 @@ static int stub_send_ret_submit(struct stub_device *sdev)
 		else
 			iovnum = 2;
 
-		iov = kzalloc(iovnum * sizeof(struct kvec), GFP_KERNEL);
+		iov = kcalloc(iovnum, sizeof(struct kvec), GFP_KERNEL);
 
 		if (!iov) {
 			usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_MALLOC);
Signature · 1 line
1.8.5.2