Skip to contentSkip to conversation

staging: android: ion: tegra: Use devm_kcalloc instead of devm_kzalloc

PATCH In review Amitoj Kaur Chawla 2 participants 3 messages Feb 25, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: android: ion: tegra: Use devm_kcalloc instead of devm_kzalloc

Replace devm_kzalloc with devm_kcalloc to ensure there are no integer overflows from the multiplication of a number * sizeof.

The following Coccinelle semantic patch was used to make this change:
//<smpl>
@@
expression dev,E1,E3;
type T;
@@
- devm_kzalloc(dev,E1*sizeof(T),E3)
+ devm_kcalloc(dev,E1,sizeof(T),E3)
//</smpl>

---
 drivers/staging/android/ion/tegra/tegra_ion.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/staging/android/ion/tegra/tegra_ion.c +2 -3
index 4d3c516..8828524 100644
@@ -33,9 +33,8 @@ static int tegra_ion_probe(struct platform_device *pdev)
 
 	num_heaps = pdata->nr;
 
-	heaps = devm_kzalloc(&pdev->dev,
-			     sizeof(struct ion_heap *) * pdata->nr,
-			     GFP_KERNEL);
+	heaps = devm_kcalloc(&pdev->dev, pdata->nr,
+			     sizeof(struct ion_heap *), GFP_KERNEL);
 
 	idev = ion_device_create(NULL);
 	if (IS_ERR_OR_NULL(idev))
Signature · 1 line
1.9.1
#1 lore raw
Quoted 24 lines · Amitoj Kaur Chawla

Replace devm_kzalloc with devm_kcalloc to ensure there are no integer overflows from the multiplication of a number * sizeof.

The following Coccinelle semantic patch was used to make this change:
//<smpl>
@@
expression dev,E1,E3;
type T;
@@
- devm_kzalloc(dev,E1*sizeof(T),E3)
+ devm_kcalloc(dev,E1,sizeof(T),E3)
//</smpl>

---
 drivers/staging/android/ion/tegra/tegra_ion.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/staging/android/ion/tegra/tegra_ion.c +0 -0
index 4d3c516..8828524 100644
@@ -33,9 +33,8 @@ static int tegra_ion_probe(struct platform_device *pdev)
 

drivers/staging/android/ion/tegra/tegra_ion.c static int tegra_ion_probe(struct platform_device *pdev) · line 40

Quoted 10 lines
drivers/staging/android/ion/tegra/tegra_ion.c +2 -3
 	num_heaps = pdata->nr;
 
-	heaps = devm_kzalloc(&pdev->dev,
-			     sizeof(struct ion_heap *) * pdata->nr,
-			     GFP_KERNEL);
+	heaps = devm_kcalloc(&pdev->dev, pdata->nr,
+			     sizeof(struct ion_heap *), GFP_KERNEL);
 
 	idev = ion_device_create(NULL);
 	if (IS_ERR_OR_NULL(idev))

Here's another IS_ERR_OR_NULL. You could look into whether it is really necessary.

julia

#2 lore raw
Quoted 7 lines · Julia Lawall
Quoted 2 lines
      idev = ion_device_create(NULL);
      if (IS_ERR_OR_NULL(idev))

Here's another IS_ERR_OR_NULL. You could look into whether it is really necessary.

julia

ion_device_create() function returns an ERR_PTR in two cases, one on failure of kzalloc() and the other if it fails to register the device i.e. failure of misc_register(). Other than that it is returning a ion_device pointer which can be NULL.

I think we can make the change since the return type of ion_device_create() is a structure which should return NULL on failure ideally.

What do you think? Should I go for making the change?

Amitoj