2014-06-10 16:11:22

by Himangi Saraogi

[permalink] [raw]
Subject: [PATCH] i2o/memory.c : Use kstrdup

Use kstrdup when the goal of an allocation is copy a string into the
allocated region.

The Coccinelle semantic patch that makes this change is as follows:

// <smpl>
@@
expression from,to;
expression flag,E1,E2;
statement S;
@@

- to = kmalloc(strlen(from) + 1,flag);
+ to = kstrdup(from, flag);
... when != \(from = E1 \| to = E1 \)
if (to==NULL || ...) S
... when != \(from = E2 \| to = E2 \)
- strcpy(to, from);
// </smpl>

Signed-off-by: Himangi Saraogi <[email protected]>
---
To send to: [email protected]
drivers/message/i2o/memory.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/message/i2o/memory.c b/drivers/message/i2o/memory.c
index 292b41e..1741a59 100644
--- a/drivers/message/i2o/memory.c
+++ b/drivers/message/i2o/memory.c
@@ -270,10 +270,9 @@ EXPORT_SYMBOL_GPL(i2o_dma_realloc);
int i2o_pool_alloc(struct i2o_pool *pool, const char *name,
size_t size, int min_nr)
{
- pool->name = kmalloc(strlen(name) + 1, GFP_KERNEL);
+ pool->name = kstrdup(name, GFP_KERNEL);
if (!pool->name)
goto exit;
- strcpy(pool->name, name);

pool->slab =
kmem_cache_create(pool->name, size, 0, SLAB_HWCACHE_ALIGN, NULL);
--
1.9.1


2014-06-10 16:45:47

by Alan

[permalink] [raw]
Subject: Re: [PATCH] i2o/memory.c : Use kstrdup

On Tue, 2014-06-10 at 21:41 +0530, Himangi Saraogi wrote:
> Use kstrdup when the goal of an allocation is copy a string into the
> allocated region.
>
> The Coccinelle semantic patch that makes this change is as follows:
>
> // <smpl>
> @@
> expression from,to;
> expression flag,E1,E2;
> statement S;
> @@
>
> - to = kmalloc(strlen(from) + 1,flag);
> + to = kstrdup(from, flag);
> ... when != \(from = E1 \| to = E1 \)
> if (to==NULL || ...) S
> ... when != \(from = E2 \| to = E2 \)
> - strcpy(to, from);
> // </smpl>
>
> Signed-off-by: Himangi Saraogi <[email protected]>
> ---
> To send to: [email protected]
> drivers/message/i2o/memory.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)


I no longer have any I2O hardware so I'm unable to test this. Looks
right but does anyone actually still use these drivers ????

Alan