[PATCH setxkbmap] Consistent reporting OOM error.

Van de Bugger van.de.bugger at gmail.com
Tue Feb 15 14:48:08 PST 2011


1.  `OOM' macro introduced to check memory allocation error and report it
    consistently. In case of error the macro prints message "Out of memory"
    (not sure anybody will really see it, remember -- it's OOM!) and aborts
    (not exits).
2.  All existing checks for OOM condition fixed to use this new macro.
3.  One check added: result of `strdup' was not checked before.
---
 setxkbmap.c |   32 +++++++++-----------------------
 1 files changed, 9 insertions(+), 23 deletions(-)

diff --git a/setxkbmap.c b/setxkbmap.c
index a028f8b..87c976b 100644
--- a/setxkbmap.c
+++ b/setxkbmap.c
@@ -164,6 +164,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)        { if ((ptr) == NULL) { ERR("Out of memory.\n"); abort(); }; }
+
 /***====================================================================***/
 
 char * rprintf( char const * format, ... );
@@ -212,11 +214,7 @@ rprintf(char const * format, ...)
         do 
         {
             buffer = realloc(buffer, required + 1);
-            if (buffer == NULL)
-            {
-                ERR("Out of memory.\n");
-                abort();
-            }
+            OOM(buffer);
             allocated = required + 1;
             va_start(args, format);
             required = vsnprintf(buffer, allocated, format, args);
@@ -266,13 +264,9 @@ addToList(list_t * list, char *newVal)
         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;
 }
@@ -702,8 +696,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, ',');
@@ -739,21 +733,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