[PATCH setxkbmap; 3rd try] Consistent handling of memory allocation errors.

Peter Hutterer peter.hutterer at who-t.net
Sun Feb 27 14:59:18 PST 2011


On Sat, Feb 26, 2011 at 02:32:29AM +0300, Van de Bugger wrote:
> Macro `OOM' ("Out of memory") introduced for checking and reporting
> memory allocation errors. The same macro is used in all the cases.
> 
> One check was missed in original source; fixed.
> 
> Changes after patch review:
> 
>     1. `OOM' macro uses `do ... while (0)'.
>     2. `exit(-1)', not `abort()'.
> 
> Signed-off-by: Van de Bugger <van.de.bugger at gmail.com>
> ---

btw, if you put the changelog after the ---, git will skip it when merging
and the commit message will be cleaner. You can also put other comments here
that you don't want in the commit. so your commit message would end up
being:

    one line subject

    multi-line 
    comment

    Signed-off-by: ....
    ---
    Changes to v2: 
    - blah
    - foobar

     setxkbmap.c |   27 +++++++++------------------
     1 files changed, 9 insertions(+), 18 deletions(-)

    diff --git a/setxkbmap.c b/setxkbmap.c
    index f7dbade..04c3fdf 100644
    [...]

either way - merged this patch, thank you!

Cheers,
  Peter

>  setxkbmap.c |   27 +++++++++------------------
>  1 files changed, 9 insertions(+), 18 deletions(-)
> 
> diff --git a/setxkbmap.c b/setxkbmap.c
> index f7dbade..04c3fdf 100644
> --- a/setxkbmap.c
> +++ b/setxkbmap.c
> @@ -170,6 +170,8 @@ static int deviceSpec = XkbUseCoreKbd;
>  #define ERR2(s,a,b)     fprintf(stderr,s,a,b)
>  #define ERR3(s,a,b,c)   fprintf(stderr,s,a,b,c)
>  
> +#define OOM(ptr)        do { if ((ptr) == NULL) { ERR("Out of memory.\n"); exit(-1); } } while (0)
> +
>  /***====================================================================***/
>  
>  Bool addToList(list_t *list, char *newVal);
> @@ -215,19 +217,16 @@ addToList(list_t *list, char *newVal)
>          list->num = 0;
>          list->sz = 4;
>          list->item = (char **) calloc(list->sz, sizeof(char *));
> +        OOM(list->item);
>      }
>      else if (list->num >= list->sz)
>      {
>          list->sz *= 2;
>          list->item = (char **) realloc(list->item, (list->sz) * sizeof(char *));
> -    }
> -    if (!list->item)
> -    {
> -        ERR("Internal Error! Allocation failure in add to list!\n");
> -        ERR("                Exiting.\n");
> -        exit(-1);
> +        OOM(list->item);
>      }
>      list->item[list->num] = strdup(newVal);
> +    OOM(list->item[list->num]);
>      list->num += 1;
>      return True;
>  }
> @@ -663,8 +662,8 @@ addStringToOptions(char *opt_str, list_t *opts)
>      char *tmp, *str, *next;
>      Bool ok = True;
>  
> -    if ((str = strdup(opt_str)) == NULL)
> -        return False;
> +    str = strdup(opt_str);
> +    OOM(str);
>      for (tmp = str; (tmp && *tmp != '\0') && ok; tmp = next)
>      {
>          next = strchr(str, ',');
> @@ -700,21 +699,13 @@ stringFromOptions(char *orig, list_t *newOpts)
>      if (orig)
>      {
>          orig = (char *) realloc(orig, len);
> -        if (!orig)
> -        {
> -            ERR("OOM in stringFromOptions\n");
> -            return NULL;
> -        }
> +        OOM(orig);
>          nOut = 1;
>      }
>      else
>      {
>          orig = (char *) calloc(len, 1);
> -        if (!orig)
> -        {
> -            ERR("OOM in stringFromOptions\n");
> -            return NULL;
> -        }
> +        OOM(orig);
>          nOut = 0;
>      }
>      for (i = 0; i < newOpts->num; i++)
> -- 
> 1.7.4
> 
> 


More information about the xorg-devel mailing list