[PATCH libX11] Fix XQueryColors() doesn't return if ncolors is 65536.

Matt Turner mattst88 at gmail.com
Fri Dec 3 14:11:46 PST 2010


From: Kusanagi Kouichi <slash at ma.neweb.ne.jp>

Fixes bugs:
	http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=278984
	https://bugs.freedesktop.org/show_bug.cgi?id=9160
---
The patch looks like it only makes the whitespace mess worse, so if this gets
committed, we should fix the whitespace.

 src/QuColors.c |   70 ++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 46 insertions(+), 24 deletions(-)

diff --git a/src/QuColors.c b/src/QuColors.c
index e51375f..91331cc 100644
--- a/src/QuColors.c
+++ b/src/QuColors.c
@@ -36,40 +36,62 @@ XQueryColors(
     XColor *defs, 		/* RETURN */
     int ncolors)
 {
-    register int i;
-    xrgb *color;
-    xQueryColorsReply rep;
-    long nbytes;
-    register xQueryColorsReq *req;
+    long max;
+    XColor *next;
+    unsigned int rest;
 
     LockDisplay(dpy);
-    GetReq(QueryColors, req);
 
-    req->cmap = cmap;
-    req->length += ncolors; /* each pixel is a CARD32 */
+    max = dpy->max_request_size - (sizeof (xQueryColorsReq) >> 2);
+    if (max > 65535)
+	max = 65535;
 
-    for (i = 0; i < ncolors; i++)
-      Data32 (dpy, (long *)&defs[i].pixel, 4L);
-       /* XXX this isn't very efficient */
+    next = defs;
+    rest = ncolors;
+    while (rest) {
+	register unsigned int i;
+	xrgb *color;
+	xQueryColorsReply rep;
+	long nbytes;
+	register xQueryColorsReq *req;
+	unsigned int npixels;
 
-    if (_XReply(dpy, (xReply *) &rep, 0, xFalse) != 0) {
-	if ((color = (xrgb *)
-	    Xmalloc((unsigned) (nbytes = (long) ncolors * SIZEOF(xrgb))))) {
+	GetReq(QueryColors, req);
 
-	    _XRead(dpy, (char *) color, nbytes);
+	req->cmap = cmap;
+	if (rest > max)
+	    npixels = max;
+	else
+	    npixels = rest;
+	req->length += npixels;
+	rest -= npixels;
 
-	    for (i = 0; i < ncolors; i++) {
-		register XColor *def = &defs[i];
-		register xrgb *rgb = &color[i];
-		def->red = rgb->red;
-		def->green = rgb->green;
-		def->blue = rgb->blue;
-		def->flags = DoRed | DoGreen | DoBlue;
+	for (i = 0; i < npixels; i++)
+	  Data32 (dpy, (long *)&next[i].pixel, 4L);
+	   /* XXX this isn't very efficient */
+
+	if (_XReply(dpy, (xReply *) &rep, 0, xFalse) != 0) {
+	    if ((color = (xrgb *)
+		Xmalloc((unsigned) (nbytes = (long) npixels * SIZEOF(xrgb))))) {
+
+		_XRead(dpy, (char *) color, nbytes);
+
+		for (i = 0; i < npixels; i++) {
+		    register XColor *def = &next[i];
+		    register xrgb *rgb = &color[i];
+		    def->red = rgb->red;
+		    def->green = rgb->green;
+		    def->blue = rgb->blue;
+		    def->flags = DoRed | DoGreen | DoBlue;
+		}
+		Xfree((char *)color);
 	    }
-	    Xfree((char *)color);
+	    else _XEatData(dpy, (unsigned long) nbytes);
+
+	    next += npixels;
 	}
-	else _XEatData(dpy, (unsigned long) nbytes);
     }
+
     UnlockDisplay(dpy);
     SyncHandle();
     return 1;
-- 
1.7.2.2



More information about the xorg-devel mailing list