diff options
author | Van de Bugger <van.de.bugger@gmail.com> | 2011-02-26 02:32:29 +0300 |
---|---|---|
committer | Peter Hutterer <peter.hutterer@who-t.net> | 2011-02-28 08:55:55 +1000 |
commit | e4387240c3c78014561fc2821cba5a944449599f (patch) | |
tree | 22b39dda6e2d42b0bf0981e6c258b018a4d60801 /setxkbmap.c | |
parent | a9f39af2f4fa2add26d332914e500c6cfa57ef5d (diff) |
Consistent handling of memory allocation errors.
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@gmail.com>
Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
Diffstat (limited to 'setxkbmap.c')
-rw-r--r-- | 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++) |