Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 5 Dec 2012 15:48:42 +0100
From:      Mateusz Guzik <mjguzik@gmail.com>
To:        Eitan Adler <eadler@FreeBSD.org>
Cc:        svn-src-head@freebsd.org, svn-src-all@freebsd.org, src-committers@freebsd.org
Subject:   Re: svn commit: r243898 - head/usr.sbin/pw
Message-ID:  <20121205144842.GA29435@dft-labs.eu>
In-Reply-To: <201212051356.qB5Duu0c068432@svn.freebsd.org>
References:  <201212051356.qB5Duu0c068432@svn.freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, Dec 05, 2012 at 01:56:56PM +0000, Eitan Adler wrote:
> Author: eadler
> Date: Wed Dec  5 13:56:56 2012
> New Revision: 243898
> URL: http://svnweb.freebsd.org/changeset/base/243898
> 
> Log:
>   Simplify string duplication: use strdup instead of malloc + strcpy
>   
>   Submitted by:	db
>   Approved by:	cperciva
>   MFC after:	2 weeks
> 
> Modified:
>   head/usr.sbin/pw/grupd.c
> 
> Modified: head/usr.sbin/pw/grupd.c
> ==============================================================================
> --- head/usr.sbin/pw/grupd.c	Wed Dec  5 13:56:52 2012	(r243897)
> +++ head/usr.sbin/pw/grupd.c	Wed Dec  5 13:56:56 2012	(r243898)
> @@ -50,12 +50,11 @@ setgrdir(const char * dir)
>  {
>  	if (dir == NULL)
>  		return -1;
> -	else {
> -		char * d = malloc(strlen(dir)+1);
> -		if (d == NULL)
> -			return -1;
> -		grpath = strcpy(d, dir);
> -	}
> +	else
> +		grpath = strdup(dir);
> +	if (grpath == NULL)
> +		return -1;
> +
>  	return 0;
>  }
>  

(yes, I know pw is a lot of work)

This can be further deuglified with slight change:

--- a/usr.sbin/pw/grupd.c
+++ b/usr.sbin/pw/grupd.c
@@ -50,8 +50,8 @@ setgrdir(const char * dir)
 {
        if (dir == NULL)
                return -1;
-       else
-               grpath = strdup(dir);
+
+       grpath = strdup(dir);
        if (grpath == NULL)
                return -1;

Also the only consumer does not check for errors, but after cursory look
I'm not sure if it is ok to just exit.

-- 
Mateusz Guzik <mjguzik gmail.com>



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20121205144842.GA29435>