[PATCH:mkfontscale] Replace malloc(strlen); strcpy() calls with strdup

walter harms wharms at bfs.de
Sun Jan 20 06:04:36 PST 2013


IMHO you can replace

if strdup == NULL goto fail

with a strdup that does simply an exit(1) on alloc error.
like:

char *xstrdup(char *buf)
{
	char *ret=strdup(buf);	
	if (ret == NULL)
	 exit(1);
	return ret;
}

perhaps there is already something like that.

re,
 wh



Am 20.01.2013 01:02, schrieb Alan Coopersmith:
> Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
> ---
>  configure.ac  |    1 +
>  hash.c        |   27 ++++++++++++---------------
>  ident.c       |    3 +--
>  mkfontscale.c |    5 +++--
>  4 files changed, 17 insertions(+), 19 deletions(-)
> 
> diff --git a/configure.ac b/configure.ac
> index 4340f99..4c7e599 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -27,6 +27,7 @@ AC_INIT([mkfontscale], [1.1.0],
>          [mkfontscale])
>  AC_CONFIG_SRCDIR([Makefile.am])
>  AC_CONFIG_HEADERS([config.h])
> +AC_USE_SYSTEM_EXTENSIONS
>  
>  # Initialize Automake
>  AM_INIT_AUTOMAKE([foreign dist-bzip2])
> diff --git a/hash.c b/hash.c
> index c2cf9ca..3adfb68 100644
> --- a/hash.c
> +++ b/hash.c
> @@ -20,6 +20,8 @@
>    THE SOFTWARE.
>  */
>  
> +#include "config.h"
> +
>  #include <stdlib.h>
>  #include <stdio.h>
>  #include <string.h>
> @@ -41,14 +43,11 @@ hash(const char *string)
>  }
>  
>  static void
> -strcpy_lwr(char *dst, const char *src)
> +str_tolower(char *s)
>  {
> -    while(1) {
> -        *dst = tolower(*src);
> -        if(*src == '\0')
> -            break;
> -        src++;
> -        dst++;
> +    while(*s != '\0') {
> +        *s = tolower(*s);
> +        s++;
>      }
>  }
>  
> @@ -97,12 +96,11 @@ putHash(HashTablePtr table, char *key, char *value, int prio)
>      for(bp = table[i]; bp; bp = bp->next) {
>          if(strcasecmp(bp->key, key) == 0) {
>              if(prio > bp->prio) {
> -                keycopy = malloc(strlen(key) + 1);
> +                keycopy = strdup(key);
>                  if(keycopy == NULL) goto fail;
> -                strcpy_lwr(keycopy, key);
> -                valuecopy = malloc(strlen(value) + 1);
> +                str_tolower(keycopy);
> +                valuecopy = strdup(value);
>                  if(valuecopy == NULL) goto fail;
> -                strcpy(valuecopy, value);
>                  free(bp->key);
>                  free(bp->value);
>                  bp->key = keycopy;
> @@ -111,14 +109,13 @@ putHash(HashTablePtr table, char *key, char *value, int prio)
>              return 1;
>          }
>      }
> -    keycopy = malloc(strlen(key) + 1);
> +    keycopy = strdup(key);
>      if(keycopy == NULL)
>          goto fail;
> -    strcpy_lwr(keycopy, key);
> -    valuecopy = malloc(strlen(value) + 1);
> +    str_tolower(keycopy);
> +    valuecopy = strdup(value);
>      if(valuecopy == NULL)
>          goto fail;
> -    strcpy(valuecopy, value);
>      bp = malloc(sizeof(HashBucketRec));
>      if(bp == NULL)
>          goto fail;
> diff --git a/ident.c b/ident.c
> index bf54483..4121257 100644
> --- a/ident.c
> +++ b/ident.c
> @@ -315,10 +315,9 @@ pcfIdentify(fontFile *f, char **name)
>      if(i >= nprops)
>          goto fail;
>  
> -    s = malloc(strlen(strings + props[i].value) + 1);
> +    s = strdup(strings + props[i].value);
>      if(s == NULL)
>          goto fail;
> -    strcpy(s, strings + props[i].value);
>      *name = s;
>      free(strings);
>      free(props);
> diff --git a/mkfontscale.c b/mkfontscale.c
> index 5cf5cb9..a67f283 100644
> --- a/mkfontscale.c
> +++ b/mkfontscale.c
> @@ -20,6 +20,8 @@
>    THE SOFTWARE.
>  */
>  
> +#include "config.h"
> +
>  #include <stdio.h>
>  #include <stdlib.h>
>  #include <string.h>
> @@ -896,10 +898,9 @@ doDirectory(const char *dirname_given, int numEncodings, ListPtr encodingsToDo)
>                  BDF_PropertyRec prop;
>                  rc = FT_Get_BDF_Property(face, "FONT", &prop);
>                  if(rc == 0 && prop.type == BDF_PROPERTY_TYPE_ATOM) {
> -                    xlfd_name = malloc(strlen(prop.u.atom) + 1);
> +                    xlfd_name = strdup(prop.u.atom);
>                      if(xlfd_name == NULL)
>                          goto done;
> -                    strcpy(xlfd_name, prop.u.atom);
>                  }
>              }
>          }


More information about the xorg-devel mailing list