[PATCH] replace malloc+strlen+strcpy with strdup

Alan Coopersmith alan.coopersmith at oracle.com
Sat Jun 19 23:07:07 PDT 2010


mattst88 at gmail.com wrote:
> From: Matt Turner <mattst88 at gmail.com>
> 
> Signed-off-by: Matt Turner <mattst88 at gmail.com>
> ---
>  dix/extension.c              |    3 +--
>  hw/xfree86/common/xf86Init.c |    6 ++----
>  hw/xfree86/parser/scan.c     |    6 ++----
>  3 files changed, 5 insertions(+), 10 deletions(-)
> 
> diff --git a/dix/extension.c b/dix/extension.c
> index c8e921a..1e340ce 100644
> --- a/dix/extension.c
> +++ b/dix/extension.c
> @@ -164,10 +164,9 @@ Bool AddExtensionAlias(char *alias, ExtensionEntry *ext)
>      if (!aliases)
>  	return FALSE;
>      ext->aliases = aliases;
> -    name = malloc(strlen(alias) + 1);
> +    name = strdup(alias);
>      if (!name)
>  	return FALSE;
> -    strcpy(name,  alias);
>      ext->aliases[ext->num_aliases] = name;
>      ext->num_aliases++;
>      return TRUE;
> diff --git a/hw/xfree86/common/xf86Init.c b/hw/xfree86/common/xf86Init.c
> index ca532ee..4195a09 100644
> --- a/hw/xfree86/common/xf86Init.c
> +++ b/hw/xfree86/common/xf86Init.c
> @@ -1067,10 +1067,9 @@ ddxProcessArgument(int argc, char **argv, int i)
>      {
>        char *mp;
>        CHECK_FOR_REQUIRED_ARGUMENT();
> -      mp = malloc(strlen(argv[i + 1]) + 1);
> +      mp = strdup(argv[i + 1]);
>        if (!mp)
>  	FatalError("Can't allocate memory for ModulePath\n");
> -      strcpy(mp, argv[i + 1]);
>        xf86ModulePath = mp;
>        xf86ModPathFrom = X_CMDLINE;
>        return 2;
> @@ -1079,10 +1078,9 @@ ddxProcessArgument(int argc, char **argv, int i)
>      {
>        char *lf;
>        CHECK_FOR_REQUIRED_ARGUMENT();
> -      lf = malloc(strlen(argv[i + 1]) + 1);
> +      lf = strdup(argv[i + 1]);
>        if (!lf)
>  	FatalError("Can't allocate memory for LogFile\n");
> -      strcpy(lf, argv[i + 1]);
>        xf86LogFile = lf;
>        xf86LogFileFrom = X_CMDLINE;
>        return 2;
> diff --git a/hw/xfree86/parser/scan.c b/hw/xfree86/parser/scan.c
> index 5312143..ada3910 100644
> --- a/hw/xfree86/parser/scan.c
> +++ b/hw/xfree86/parser/scan.c
> @@ -451,8 +451,7 @@ again:
>  			}
>  			while ((c != '\"') && (c != '\n') && (c != '\r') && (c != '\0'));
>  			configRBuf[i] = '\0';
> -			val.str = malloc (strlen (configRBuf) + 1);
> -			strcpy (val.str, configRBuf);	/* private copy ! */
> +			val.str = strdup(configRBuf);	/* private copy ! */
>  			return STRING;
>  		}
>  
> @@ -1088,8 +1087,7 @@ void
>  xf86setSection (char *section)
>  {
>  	free(configSection);
> -	configSection = malloc(strlen (section) + 1);
> -	strcpy (configSection, section);
> +	configSection = strdup(section);
>  }
>  
>  /* 

Reviewed-by: Alan Coopersmith <alan.coopersmith at oracle.com>

-- 
	-Alan Coopersmith-        alan.coopersmith at oracle.com
	 Oracle Solaris Platform Engineering: X Window System



More information about the xorg-devel mailing list