[comp.windows.x] default colormap of immutable visual bug

mouse@LARRY.MCRCIM.MCGILL.EDU (09/15/90)

			  X Window System Bug Report
			    xbugs@expo.lcs.mit.edu


VERSION:
    R4

CLIENT MACHINE and OPERATING SYSTEM:
    N/A

DISPLAY TYPE:
    Any whose root window's default visual is a StaticGray,
    StaticColor, or TrueColor visual.

WINDOW MANAGER:
    N/A

AREA:
    Server

SYNOPSIS:
    The sample server bounces attempts to free colors allocated out of
    the default map, when that map is for an immutable visual type, and
    the screen is using cfb rather than mfb (mfb works fine).

DESCRIPTION:

    When the root window's default visual is an immutable type, ie,
    StaticGray, StaticColor, or TrueColor, XFreeColors cannot be used
    to free cells allocated with XAllocColor from the default colormap.
    The server generates a BadAccess error for such attempts.

    This does not occur with one-bit StaticGray screens using mfb; it
    happens only when the display uses cfb.

    The problem is that the FreeColors code bounces any attempt to free
    colors in a colormap which was entirely allocated at creation (ie,
    which has the AllAllocated bit set).  However, when cfb creates its
    default colormap, if the visual is immutable, AllocAll is used
    rather than AllocNone.  The reason mfb doesn't have the same
    problem is that it uses AllocNone.

REPEAT BY:

    Run this on any server such as described above.  Then run it again
    on a normal color server or an mfb-based server.

	#include <stdio.h>
	#include <X11/X.h>
	#include <X11/Xlib.h>
	
	Display *disp;
	Screen *defscreen;
	Colormap defcmap;
	XColor color;
	
	main()
	{
	 disp = XOpenDisplay((char *)0);
	 if (disp == 0)
	  { fprintf(stderr,"Can't open display\n");
	    exit(1);
	  }
	 XSynchronize(disp,True);
	 defscreen = XDefaultScreenOfDisplay(disp);
	 defcmap = XDefaultColormapOfScreen(defscreen);
	 color.red = 0;
	 color.green = 0;
	 color.blue = 0;
	 if (XAllocColor(disp,defcmap,&color) == 0)
	  { fprintf(stderr,"XAllocColor failed\n");
	    exit(1);
	  }
	 XFreeColors(disp,defcmap,&color.pixel,1,0L);
	 XCloseDisplay(disp);
	 printf("Completed successfully\n");
	 exit(0);
	}

SAMPLE FIX:

    My inclination would be to have cfb use AllocNone when creating its
    colormaps:

	*** mit/ddx/cfb/cfbcmap.c.old	Fri Sep 14 16:42:55 1990
	--- mit/ddx/cfb/cfbcmap.c	Fri Sep 14 16:43:14 1990
	***************
	*** 212,221 ****
	  	 pVisual++)
	  	;
	  
	!     if (CreateColormap(pScreen->defColormap, pScreen, pVisual, &cmap,
	! 			(pVisual->class & DynamicClass) ? AllocNone : AllocAll,
	! 		       0)
	! 	!= Success)
	  	return FALSE;
	      if ((AllocColor(cmap, &ones, &ones, &ones, &(pScreen->whitePixel), 0) !=
	         	   Success) ||
	--- 212,218 ----
	  	 pVisual++)
	  	;
	  
	!     if (CreateColormap(pScreen->defColormap, pScreen, pVisual, &cmap, AllocNone, 0) != Success)
	  	return FALSE;
	      if ((AllocColor(cmap, &ones, &ones, &ones, &(pScreen->whitePixel), 0) !=
	         	   Success) ||
	

    but I am not sure I understand all the ramifications of this.  It
    does not cause immediate and obvious problems, but that's about all
    I can say for it.  (It clearly works for mfb, but then, mfb
    preallocates all the pixels explicitly with AllocColor().)

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu