rem: sprintf(RErrorString, "out of memory");
add: RErrorCode = RERR_NOMEMORY;
context: allocatePseudoColor(RContext *ctx){    XColor *colors;    XColor avcolors[256];    int avncolors;    int i, ncolors, r, g, b;    int retries;    int cpc = ctx->attribs->colors_per_channel;    ncolors = cpc * cpc * cpc;        if ( ncolors > (1<<ctx->depth) ) {      /* reduce colormap size */      cpc = ctx->attribs->colors_per_channel = 1<<((int)ctx->depth/3);      ncolors = cpc * cpc * cpc;    }    if (cpc < 2 || ncolors > (1<<ctx->depth)) {	sprintf(RErrorString, "invalid colormap size %i", cpc);	return NULL;    }    colors = malloc(sizeof(XColor)*ncolors);    if (!colors) {	sprintf(RErrorString, "out of memory");	return NULL;    }    i=0;    if ((ctx->attribs->flags & RC_GammaCorrection) && ctx->attribs->rgamma > 0	&& ctx->attribs->ggamma > 0 && ctx->attribs->bgamma > 0) {	double rg, gg, bg;	double tmp;	/* do gamma correction */	rg = 1.0/ctx->attribs->rgamma;	gg = 1.0/ctx->attribs->ggamma;	bg = 1.0/ctx->attribs->bgamma;	for (r=0; r<cpc; r++) {	    for (g=0; g<cpc; g++) {		for (b=0; b<cpc; b++) {		    colors[i].red=(r*0xffff) / (cpc-1);		    colors[i].green=(g*0xffff) / (cpc-1);		    colors[i].blue=(b*0xffff) / (cpc-1);		    colors[i].flags = DoRed|DoGreen|DoBlue;		    tmp = (double)colors[i].red / 65536.0;		    colors[i].red = (unsigned short)(65536.0*pow(tmp, rg));		    tmp = (double)colors[i].green / 65536.0;		    colors[i].green = (unsigned short)(65536.0*pow(tmp, gg));		    tmp = (double)colors[i].blue / 65536.0;		    colors[i].blue = (unsigned short)(65536.0*pow(tmp, bg));		    i++;		}	    }	}    } else {	for (r=0; r<cpc; r++) {	    for (g=0; g<cpc; g++) {		for (b=0; b<cpc; b++) {		    colors[i].red=(r*0xffff) / (cpc-1);		    colors[i].green=(g*0xffff) / (cpc-1);		    colors[i].blue=(b*0xffff) / (cpc-1);		    colors[i].flags = DoRed|DoGreen|DoBlue;		    i++;		}	    }	}    }    /* try to allocate the colors */    for (i=0; i<ncolors; i++) {	if (!XAllocColor(ctx->dpy, ctx->cmap, &(colors[i]))) {	    colors[i].flags = 0; /* failed */	} else {	    colors[i].flags = DoRed|DoGreen|DoBlue;	    	}    }    /* try to allocate close values for the colors that couldn't      * be allocated before */    avncolors = (1<<ctx->depth>256 ? 256 : 1<<ctx->depth);    for (i=0; i<avncolors; i++) avcolors[i].pixel = i;    XQueryColors(ctx->dpy, ctx->cmap, avcolors, avncolors);    for (i=0; i<ncolors; i++) {	if (colors[i].flags==0) {	    int j;	    unsigned long cdiff=0xffffffff, diff;	    unsigned long closest=0;	    	    retries = 2;	    	    while (retries--) {		/* find closest color */		for (j=0; j<avncolors; j++) {		    r = (colors[i].red - avcolors[i].red)>>8;		    g = (colors[i].green - avcolors[i].green)>>8;		    b = (colors[i].blue - avcolors[i].blue)>>8;		    diff = r*r + g*g + b*b;		    if (diff<cdiff) {			cdiff = diff;			closest = j;		    }		}		/* allocate closest color found */		colors[i].red = avcolors[closest].red;		colors[i].green = avcolors[closest].green;		colors[i].blue = avcolors[closest].blue;		if (XAllocColor(ctx->dpy, ctx->cmap, &colors[i])) {		    colors[i].flags = DoRed|DoGreen|DoBlue;		    break; /* succeeded, don't need to retry */		}#ifdef DEBUG		printf("close color allocation failed. Retrying...\n");#endif	    }	}    }    return colors;}