open-src/lib/libX11/XEatDataWords.patch
changeset 1345 d5dacbb8de2b
equal deleted inserted replaced
1344:800e8c2d47f1 1345:d5dacbb8de2b
       
     1 From d738eb47a3e781477101d706f01dd61148a9086f Mon Sep 17 00:00:00 2001
       
     2 From: Alan Coopersmith <[email protected]>
       
     3 Date: Fri, 1 Mar 2013 20:54:24 -0800
       
     4 Subject: [PATCH:libX11 02/38] Add _XEatDataWords to discard a given number of
       
     5  32-bit words of reply data
       
     6 
       
     7 Matches the units of the length field in X protocol replies, and provides
       
     8 a single implementation of overflow checking to avoid having to replicate
       
     9 those checks in every caller.
       
    10 
       
    11 Signed-off-by: Alan Coopersmith <[email protected]>
       
    12 Reviewed-by: Matthieu Herrb <[email protected]>
       
    13 ---
       
    14  include/X11/Xlibint.h |    4 ++++
       
    15  src/xcb_io.c          |   17 +++++++++++++++++
       
    16  2 files changed, 21 insertions(+)
       
    17 
       
    18 diff --git a/include/X11/Xlibint.h b/include/X11/Xlibint.h
       
    19 index 06395b3..d63a534 100644
       
    20 --- a/include/X11/Xlibint.h
       
    21 +++ b/include/X11/Xlibint.h
       
    22 @@ -855,6 +855,10 @@ extern void _XEatData(
       
    23      Display*		/* dpy */,
       
    24      unsigned long	/* n */
       
    25  );
       
    26 +extern void _XEatDataWords(
       
    27 +    Display*		/* dpy */,
       
    28 +    unsigned long	/* n */
       
    29 +);
       
    30  extern char *_XAllocScratch(
       
    31      Display*		/* dpy */,
       
    32      unsigned long	/* nbytes */
       
    33 diff --git a/src/xcb_io.c b/src/xcb_io.c
       
    34 index 300ef57..727c6c7 100644
       
    35 --- a/src/xcb_io.c
       
    36 +++ b/src/xcb_io.c
       
    37 @@ -19,6 +19,7 @@
       
    38  #include <stdint.h>
       
    39  #include <stdlib.h>
       
    40  #include <string.h>
       
    41 +#include <limits.h>
       
    42  #ifdef HAVE_SYS_SELECT_H
       
    43  #include <sys/select.h>
       
    44  #endif
       
    45 @@ -757,3 +758,19 @@ void _XEatData(Display *dpy, unsigned long n)
       
    46  	dpy->xcb->reply_consumed += n;
       
    47  	_XFreeReplyData(dpy, False);
       
    48  }
       
    49 +
       
    50 +/*
       
    51 + * Read and discard "n" 32-bit words of data
       
    52 + * Matches the units of the length field in X protocol replies, and provides
       
    53 + * a single implementation of overflow checking to avoid having to replicate
       
    54 + * those checks in every caller.
       
    55 + */
       
    56 +void _XEatDataWords(Display *dpy, unsigned long n)
       
    57 +{
       
    58 +	if (n < ((INT_MAX - dpy->xcb->reply_consumed) >> 2))
       
    59 +		dpy->xcb->reply_consumed += (n << 2);
       
    60 +	else
       
    61 +		/* Overflow would happen, so just eat the rest of the reply */
       
    62 +		dpy->xcb->reply_consumed = dpy->xcb->reply_length;
       
    63 +	_XFreeReplyData(dpy, False);
       
    64 +}
       
    65 -- 
       
    66 1.7.9.2
       
    67 
       
    68 From 56623594bb0f132aa2792f76378a573c7efe31e2 Mon Sep 17 00:00:00 2001
       
    69 From: Alan Coopersmith <[email protected]>
       
    70 Date: Fri, 19 Apr 2013 14:30:40 -0700
       
    71 Subject: [PATCH:libX11 38/38] Give GNU & Solaris Studio compilers hints about
       
    72  XEatData branches
       
    73 
       
    74 Try to offset the cost of all the recent checks we've added by giving
       
    75 the compiler a hint that the branches that involve us eating data
       
    76 are less likely to be used than the ones that process it.
       
    77 
       
    78 Signed-off-by: Alan Coopersmith <[email protected]>
       
    79 ---
       
    80  include/X11/Xlibint.h |   16 ++++++++++++++--
       
    81  1 file changed, 14 insertions(+), 2 deletions(-)
       
    82 
       
    83 diff --git a/include/X11/Xlibint.h b/include/X11/Xlibint.h
       
    84 index d63a534..acbad6b 100644
       
    85 --- a/include/X11/Xlibint.h
       
    86 +++ b/include/X11/Xlibint.h
       
    87 @@ -832,6 +832,15 @@ typedef struct _XExten {		/* private to extension mechanism */
       
    88  	struct _XExten *next_flush;	/* next in list of those with flushes */
       
    89  } _XExtension;
       
    90  
       
    91 +/* Temporary definition until we can depend on an xproto release with it */
       
    92 +#ifdef _X_COLD
       
    93 +# define _XLIB_COLD _X_COLD
       
    94 +#elif defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 403) /* 4.3+ */
       
    95 +# define _XLIB_COLD __attribute__((__cold__))
       
    96 +#else
       
    97 +# define _XLIB_COLD /* nothing */
       
    98 +#endif
       
    99 +
       
   100  /* extension hooks */
       
   101  
       
   102  #ifdef DataRoutineIsProcedure
       
   103 @@ -854,11 +863,14 @@ extern int (*_XErrorFunction)(
       
   104  extern void _XEatData(
       
   105      Display*		/* dpy */,
       
   106      unsigned long	/* n */
       
   107 -);
       
   108 +) _XLIB_COLD;
       
   109  extern void _XEatDataWords(
       
   110      Display*		/* dpy */,
       
   111      unsigned long	/* n */
       
   112 -);
       
   113 +) _XLIB_COLD;
       
   114 +#if defined(__SUNPRO_C) /* Studio compiler alternative to "cold" attribute */
       
   115 +# pragma rarely_called(_XEatData, _XEatDataWords)
       
   116 +#endif
       
   117  extern char *_XAllocScratch(
       
   118      Display*		/* dpy */,
       
   119      unsigned long	/* nbytes */
       
   120 -- 
       
   121 1.7.9.2
       
   122 
       
   123 From d0944b52f8debb74eb48359a8dc20706b12da834 Mon Sep 17 00:00:00 2001
       
   124 From: Alan Coopersmith <[email protected]>
       
   125 Date: Sat, 2 Mar 2013 16:56:16 -0800
       
   126 Subject: [PATCH:libX11 34/38] Convert more _XEatData callers to
       
   127  _XEatDataWords
       
   128 
       
   129 Signed-off-by: Alan Coopersmith <[email protected]>
       
   130 Reviewed-by: Matthieu Herrb <[email protected]>
       
   131 ---
       
   132  src/GetAtomNm.c |    4 ++--
       
   133  src/LiICmaps.c  |    8 ++++----
       
   134  src/LiProps.c   |    8 ++++----
       
   135  src/OpenDis.c   |    2 +-
       
   136  src/QuColors.c  |   10 +++++-----
       
   137  src/QuTree.c    |    8 ++++----
       
   138  6 files changed, 20 insertions(+), 20 deletions(-)
       
   139 
       
   140 diff --git a/src/GetAtomNm.c b/src/GetAtomNm.c
       
   141 index 9823c69..996f7eb 100644
       
   142 --- a/src/GetAtomNm.c
       
   143 +++ b/src/GetAtomNm.c
       
   144 @@ -78,7 +78,7 @@ char *XGetAtomName(
       
   145  	name[rep.nameLength] = '\0';
       
   146  	_XUpdateAtomCache(dpy, name, atom, 0, -1, 0);
       
   147      } else {
       
   148 -	_XEatData(dpy, (unsigned long) (rep.nameLength + 3) & ~3);
       
   149 +	_XEatDataWords(dpy, rep.length);
       
   150  	name = (char *) NULL;
       
   151      }
       
   152      UnlockDisplay(dpy);
       
   153 @@ -176,7 +176,7 @@ XGetAtomNames (
       
   154  		_XUpdateAtomCache(dpy, names_return[missed], atoms[missed],
       
   155  				  0, -1, 0);
       
   156  	    } else {
       
   157 -		_XEatData(dpy, (unsigned long) (rep.nameLength + 3) & ~3);
       
   158 +		_XEatDataWords(dpy, rep.length);
       
   159  		async_state.status = 0;
       
   160  	    }
       
   161  	}
       
   162 diff --git a/src/LiICmaps.c b/src/LiICmaps.c
       
   163 index e981619..45a2f2f 100644
       
   164 --- a/src/LiICmaps.c
       
   165 +++ b/src/LiICmaps.c
       
   166 @@ -34,7 +34,7 @@ Colormap *XListInstalledColormaps(
       
   167      Window win,
       
   168      int *n)  /* RETURN */
       
   169  {
       
   170 -    long nbytes;
       
   171 +    unsigned long nbytes;
       
   172      Colormap *cmaps;
       
   173      xListInstalledColormapsReply rep;
       
   174      register xResourceReq *req;
       
   175 @@ -51,14 +51,14 @@ Colormap *XListInstalledColormaps(
       
   176  
       
   177      if (rep.nColormaps) {
       
   178  	nbytes = rep.nColormaps * sizeof(Colormap);
       
   179 -	cmaps = (Colormap *) Xmalloc((unsigned) nbytes);
       
   180 -	nbytes = rep.nColormaps << 2;
       
   181 +	cmaps = Xmalloc(nbytes);
       
   182  	if (! cmaps) {
       
   183 -	    _XEatData(dpy, (unsigned long) nbytes);
       
   184 +	    _XEatDataWords(dpy, rep.length);
       
   185  	    UnlockDisplay(dpy);
       
   186  	    SyncHandle();
       
   187  	    return((Colormap *) NULL);
       
   188  	}
       
   189 +	nbytes = rep.nColormaps << 2;
       
   190  	_XRead32 (dpy, (long *) cmaps, nbytes);
       
   191      }
       
   192      else cmaps = (Colormap *) NULL;
       
   193 diff --git a/src/LiProps.c b/src/LiProps.c
       
   194 index 72560ab..d9c7465 100644
       
   195 --- a/src/LiProps.c
       
   196 +++ b/src/LiProps.c
       
   197 @@ -34,7 +34,7 @@ Atom *XListProperties(
       
   198      Window window,
       
   199      int *n_props)  /* RETURN */
       
   200  {
       
   201 -    long nbytes;
       
   202 +    unsigned long nbytes;
       
   203      xListPropertiesReply rep;
       
   204      Atom *properties;
       
   205      register xResourceReq *req;
       
   206 @@ -50,14 +50,14 @@ Atom *XListProperties(
       
   207  
       
   208      if (rep.nProperties) {
       
   209  	nbytes = rep.nProperties * sizeof(Atom);
       
   210 -	properties = (Atom *) Xmalloc ((unsigned) nbytes);
       
   211 -	nbytes = rep.nProperties << 2;
       
   212 +	properties = Xmalloc (nbytes);
       
   213  	if (! properties) {
       
   214 -	    _XEatData(dpy, (unsigned long) nbytes);
       
   215 +	    _XEatDataWords(dpy, rep.length);
       
   216  	    UnlockDisplay(dpy);
       
   217  	    SyncHandle();
       
   218  	    return (Atom *) NULL;
       
   219  	}
       
   220 +	nbytes = rep.nProperties << 2;
       
   221  	_XRead32 (dpy, (long *) properties, nbytes);
       
   222      }
       
   223      else properties = (Atom *) NULL;
       
   224 diff --git a/src/OpenDis.c b/src/OpenDis.c
       
   225 index f6d8c70..0bf1b91 100644
       
   226 --- a/src/OpenDis.c
       
   227 +++ b/src/OpenDis.c
       
   228 @@ -552,7 +552,7 @@ XOpenDisplay (
       
   229  		    dpy->xdefaults[reply.nItems] = '\0';
       
   230  		}
       
   231  		else if (reply.propertyType != None)
       
   232 -		    _XEatData(dpy, reply.nItems * (reply.format >> 3));
       
   233 +		    _XEatDataWords(dpy, reply.length);
       
   234  	    }
       
   235  	}
       
   236  	UnlockDisplay(dpy);
       
   237 diff --git a/src/QuColors.c b/src/QuColors.c
       
   238 index 237b8bf..13a63eb 100644
       
   239 --- a/src/QuColors.c
       
   240 +++ b/src/QuColors.c
       
   241 @@ -37,9 +37,7 @@ _XQueryColors(
       
   242      int ncolors)
       
   243  {
       
   244      register int i;
       
   245 -    xrgb *color;
       
   246      xQueryColorsReply rep;
       
   247 -    long nbytes;
       
   248      register xQueryColorsReq *req;
       
   249  
       
   250      GetReq(QueryColors, req);
       
   251 @@ -52,8 +50,9 @@ _XQueryColors(
       
   252         /* XXX this isn't very efficient */
       
   253  
       
   254      if (_XReply(dpy, (xReply *) &rep, 0, xFalse) != 0) {
       
   255 -	if ((color = (xrgb *)
       
   256 -	    Xmalloc((unsigned) (nbytes = (long) ncolors * SIZEOF(xrgb))))) {
       
   257 +	unsigned long nbytes = (long) ncolors * SIZEOF(xrgb);
       
   258 +	xrgb *color = Xmalloc(nbytes);
       
   259 +	if (color != NULL) {
       
   260  
       
   261  	    _XRead(dpy, (char *) color, nbytes);
       
   262  
       
   263 @@ -67,7 +66,8 @@ _XQueryColors(
       
   264  	    }
       
   265  	    Xfree((char *)color);
       
   266  	}
       
   267 -	else _XEatData(dpy, (unsigned long) nbytes);
       
   268 +	else
       
   269 +	    _XEatDataWords(dpy, rep.length);
       
   270      }
       
   271  }
       
   272  
       
   273 diff --git a/src/QuTree.c b/src/QuTree.c
       
   274 index 3cea282..8da2ae2 100644
       
   275 --- a/src/QuTree.c
       
   276 +++ b/src/QuTree.c
       
   277 @@ -37,7 +37,7 @@ Status XQueryTree (
       
   278      Window **children,	/* RETURN */
       
   279      unsigned int *nchildren)  /* RETURN */
       
   280  {
       
   281 -    long nbytes;
       
   282 +    unsigned long nbytes;
       
   283      xQueryTreeReply rep;
       
   284      register xResourceReq *req;
       
   285  
       
   286 @@ -52,14 +52,14 @@ Status XQueryTree (
       
   287      *children = (Window *) NULL;
       
   288      if (rep.nChildren != 0) {
       
   289  	nbytes = rep.nChildren * sizeof(Window);
       
   290 -	*children = (Window *) Xmalloc((unsigned) nbytes);
       
   291 -	nbytes = rep.nChildren << 2;
       
   292 +	*children = Xmalloc(nbytes);
       
   293  	if (! *children) {
       
   294 -	    _XEatData(dpy, (unsigned long) nbytes);
       
   295 +	    _XEatDataWords(dpy, rep.length);
       
   296  	    UnlockDisplay(dpy);
       
   297  	    SyncHandle();
       
   298  	    return (0);
       
   299  	}
       
   300 +	nbytes = rep.nChildren << 2;
       
   301  	_XRead32 (dpy, (long *) *children, nbytes);
       
   302      }
       
   303      *parent = rep.parent;
       
   304 -- 
       
   305 1.7.9.2
       
   306