linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/6] arch/powerpc/platforms/pseries: Use kasprintf
@ 2010-03-10 21:15 Julia Lawall
  2010-03-11 15:16 ` Nathan Fontenot
  0 siblings, 1 reply; 2+ messages in thread
From: Julia Lawall @ 2010-03-10 21:15 UTC (permalink / raw)
  To: Benjamin Herrenschmidt, Paul Mackerras, linuxppc-dev,
	linux-kernel, kernel-janitors

From: Julia Lawall <julia@diku.dk>

kasprintf combines kmalloc and sprintf, and takes care of the size
calculation itself.

The semantic patch that makes this change is as follows:
(http://coccinelle.lip6.fr/)

// <smpl>
@@
expression a,flag;
expression list args;
statement S;
@@

  a =
-  \(kmalloc\|kzalloc\)(...,flag)
+  kasprintf(flag,args)
  <... when != a
  if (a == NULL || ...) S
  ...>
- sprintf(a,args);
// </smpl>

Signed-off-by: Julia Lawall <julia@diku.dk>

---
 arch/powerpc/platforms/pseries/dlpar.c |    7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff -u -p a/arch/powerpc/platforms/pseries/dlpar.c b/arch/powerpc/platforms/pseries/dlpar.c
--- a/arch/powerpc/platforms/pseries/dlpar.c
+++ b/arch/powerpc/platforms/pseries/dlpar.c
@@ -78,13 +78,12 @@ static struct device_node *dlpar_parse_c
 	 * prepend this to the full_name.
 	 */
 	name = (char *)ccwa + ccwa->name_offset;
-	dn->full_name = kmalloc(strlen(name) + 2, GFP_KERNEL);
+	dn->full_name = kasprintf(GFP_KERNEL, "/%s", name);
 	if (!dn->full_name) {
 		kfree(dn);
 		return NULL;
 	}
 
-	sprintf(dn->full_name, "/%s", name);
 	return dn;
 }
 
@@ -409,15 +408,13 @@ static ssize_t dlpar_cpu_probe(const cha
 	 * directory of the device tree.  CPUs actually live in the
 	 * cpus directory so we need to fixup the full_name.
 	 */
-	cpu_name = kzalloc(strlen(dn->full_name) + strlen("/cpus") + 1,
-			   GFP_KERNEL);
+	cpu_name = kasprintf(GFP_KERNEL, "/cpus%s", dn->full_name);
 	if (!cpu_name) {
 		dlpar_free_cc_nodes(dn);
 		rc = -ENOMEM;
 		goto out;
 	}
 
-	sprintf(cpu_name, "/cpus%s", dn->full_name);
 	kfree(dn->full_name);
 	dn->full_name = cpu_name;
 

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH 1/6] arch/powerpc/platforms/pseries: Use kasprintf
  2010-03-10 21:15 [PATCH 1/6] arch/powerpc/platforms/pseries: Use kasprintf Julia Lawall
@ 2010-03-11 15:16 ` Nathan Fontenot
  0 siblings, 0 replies; 2+ messages in thread
From: Nathan Fontenot @ 2010-03-11 15:16 UTC (permalink / raw)
  To: Julia Lawall; +Cc: kernel-janitors, Paul Mackerras, linux-kernel, linuxppc-dev

Julia Lawall wrote:
> From: Julia Lawall <julia@diku.dk>
> 
> kasprintf combines kmalloc and sprintf, and takes care of the size
> calculation itself.
> 
> The semantic patch that makes this change is as follows:
> (http://coccinelle.lip6.fr/)
> 
> // <smpl>
> @@
> expression a,flag;
> expression list args;
> statement S;
> @@
> 
>   a =
> -  \(kmalloc\|kzalloc\)(...,flag)
> +  kasprintf(flag,args)
>   <... when != a
>   if (a == NULL || ...) S
>   ...>
> - sprintf(a,args);
> // </smpl>
> 

Looks good, thanks.

> Signed-off-by: Julia Lawall <julia@diku.dk>
Acked-by: Nathan Fontenot <nfont@austin.ibm.com>

> 
> ---
>  arch/powerpc/platforms/pseries/dlpar.c |    7 ++-----
>  1 file changed, 2 insertions(+), 5 deletions(-)
> 
> diff -u -p a/arch/powerpc/platforms/pseries/dlpar.c b/arch/powerpc/platforms/pseries/dlpar.c
> --- a/arch/powerpc/platforms/pseries/dlpar.c
> +++ b/arch/powerpc/platforms/pseries/dlpar.c
> @@ -78,13 +78,12 @@ static struct device_node *dlpar_parse_c
>  	 * prepend this to the full_name.
>  	 */
>  	name = (char *)ccwa + ccwa->name_offset;
> -	dn->full_name = kmalloc(strlen(name) + 2, GFP_KERNEL);
> +	dn->full_name = kasprintf(GFP_KERNEL, "/%s", name);
>  	if (!dn->full_name) {
>  		kfree(dn);
>  		return NULL;
>  	}
>  
> -	sprintf(dn->full_name, "/%s", name);
>  	return dn;
>  }
>  
> @@ -409,15 +408,13 @@ static ssize_t dlpar_cpu_probe(const cha
>  	 * directory of the device tree.  CPUs actually live in the
>  	 * cpus directory so we need to fixup the full_name.
>  	 */
> -	cpu_name = kzalloc(strlen(dn->full_name) + strlen("/cpus") + 1,
> -			   GFP_KERNEL);
> +	cpu_name = kasprintf(GFP_KERNEL, "/cpus%s", dn->full_name);
>  	if (!cpu_name) {
>  		dlpar_free_cc_nodes(dn);
>  		rc = -ENOMEM;
>  		goto out;
>  	}
>  
> -	sprintf(cpu_name, "/cpus%s", dn->full_name);
>  	kfree(dn->full_name);
>  	dn->full_name = cpu_name;
>  
> _______________________________________________
> Linuxppc-dev mailing list
> Linuxppc-dev@lists.ozlabs.org
> https://lists.ozlabs.org/listinfo/linuxppc-dev

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-03-11 15:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-03-10 21:15 [PATCH 1/6] arch/powerpc/platforms/pseries: Use kasprintf Julia Lawall
2010-03-11 15:16 ` Nathan Fontenot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).