[PATCH setxkbmap; 2nd try] Consistent handling of memory allocation errors.

Alan Coopersmith alan.coopersmith at oracle.com
Fri Feb 25 14:50:45 PST 2011


On 02/25/11 11:43 AM, 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.
> 
> Signed-off-by: Van de Bugger <van.de.bugger at gmail.com>
> ---
>  setxkbmap.c |   27 +++++++++------------------
>  1 files changed, 9 insertions(+), 18 deletions(-)
> 
> diff --git a/setxkbmap.c b/setxkbmap.c
> index f7dbade..7aa56f7 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)        { if ((ptr) == NULL) { ERR("Out of memory.\n"); abort(); }; }

There's an extra ; between the last two }'s there.   Also, you probably want to
stick with the original exit(-1), not force a core dump with abort();

The idiom normally used in the X code (and much C code in fact) for a
compound statement in a macro like this you want to make look like a
function call is:

#define OOM(ptr) \
    do { if ((ptr) == NULL) { ERR("Out of memory.\n"); abort(); } } while (0)

The do { ... } while(0) allows OOM(); to end with a ; without any compiler
warnings about empty statements or without causing any issues in breaking
other blocks, like if/else pairs, that you may wind up adjacent to.

For more complete explanations, see:
	http://kernelnewbies.org/FAQ/DoWhile0
	http://www.rtems.com/ml/rtems-users/2001/august/msg00086.html
	http://stackoverflow.com/questions/923822/whats-the-use-of-do-while0-when-we-define-a-macro

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



More information about the xorg-devel mailing list