From e3c51160c87bc8cfe43f944df641bc1e627797ec Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Fri, 3 May 2013 23:29:22 -0700 Subject: [PATCH] Use _XEatDataWords to avoid overflow of rep.length bit shifting rep.length is a CARD32, so rep.length << 2 could overflow in 32-bit builds Signed-off-by: Alan Coopersmith Signed-off-by: Julien Cristau --- configure.ac | 6 ++++++ src/Xrandrint.h | 13 +++++++++++++ src/XrrCrtc.c | 6 +++--- src/XrrOutput.c | 2 +- src/XrrProperty.c | 9 ++++----- src/XrrScreen.c | 2 +- 6 files changed, 28 insertions(+), 10 deletions(-) diff --git a/configure.ac b/configure.ac index 8c74adb..46401ef 100644 --- a/configure.ac +++ b/configure.ac @@ -56,6 +56,12 @@ AC_SUBST(RANDR_VERSION) # Obtain compiler/linker options for depedencies PKG_CHECK_MODULES(RANDR, x11 randrproto >= $RANDR_VERSION xext xextproto xrender renderproto) +# Check for _XEatDataWords function that may be patched into older Xlib release +SAVE_LIBS="$LIBS" +LIBS="$RANDR_LIBS" +AC_CHECK_FUNCS([_XEatDataWords]) +LIBS="$SAVE_LIBS" + AC_CONFIG_FILES([Makefile src/Makefile man/Makefile diff --git a/src/Xrandrint.h b/src/Xrandrint.h index 7fb5831..791b36b 100644 --- a/src/Xrandrint.h +++ b/src/Xrandrint.h @@ -42,6 +42,19 @@ extern char XRRExtensionName[]; XExtDisplayInfo *XRRFindDisplay (Display *dpy); +#ifndef HAVE__XEATDATAWORDS +#include /* for LONG64 on 64-bit platforms */ +#include + +static inline void _XEatDataWords(Display *dpy, unsigned long n) +{ +# ifndef LONG64 + if (n >= (ULONG_MAX >> 2)) + _XIOError(dpy); +# endif + _XEatData (dpy, n << 2); +} +#endif /* deliberately opaque internal data structure; can be extended, but not reordered */ diff --git a/src/XrrCrtc.c b/src/XrrCrtc.c index 697987a..d3c748b 100644 --- a/src/XrrCrtc.c +++ b/src/XrrCrtc.c @@ -74,7 +74,7 @@ XRRGetCrtcInfo (Display *dpy, XRRScreenResources *resources, RRCrtc crtc) xci = (XRRCrtcInfo *) Xmalloc(rbytes); if (xci == NULL) { - _XEatData (dpy, (unsigned long) nbytes); + _XEatDataWords (dpy, rep.length); UnlockDisplay (dpy); SyncHandle (); return NULL; @@ -203,7 +203,7 @@ XRRGetCrtcGamma (Display *dpy, RRCrtc crtc) if (!crtc_gamma) { - _XEatData (dpy, (unsigned long) nbytes); + _XEatDataWords (dpy, rep.length); goto out; } _XRead16 (dpy, crtc_gamma->red, rep.size * 2); @@ -397,7 +397,7 @@ XRRGetCrtcTransform (Display *dpy, int extraBytes = rep.length * 4 - CrtcTransformExtra; extra = Xmalloc (extraBytes); if (!extra) { - _XEatData (dpy, extraBytes); + _XEatDataWords (dpy, rep.length - (CrtcTransformExtra >> 2)); UnlockDisplay (dpy); SyncHandle (); return False; diff --git a/src/XrrOutput.c b/src/XrrOutput.c index 18863bd..487d38a 100644 --- a/src/XrrOutput.c +++ b/src/XrrOutput.c @@ -81,7 +81,7 @@ XRRGetOutputInfo (Display *dpy, XRRScreenResources *resources, RROutput output) xoi = (XRROutputInfo *) Xmalloc(rbytes); if (xoi == NULL) { - _XEatData (dpy, (unsigned long) nbytes); + _XEatDataWords (dpy, rep.length - (OutputInfoExtra >> 2)); UnlockDisplay (dpy); SyncHandle (); return NULL; diff --git a/src/XrrProperty.c b/src/XrrProperty.c index 1a125b2..afa9d24 100644 --- a/src/XrrProperty.c +++ b/src/XrrProperty.c @@ -62,7 +62,7 @@ XRRListOutputProperties (Display *dpy, RROutput output, int *nprop) props = (Atom *) Xmalloc (rbytes); if (props == NULL) { - _XEatData (dpy, nbytes); + _XEatDataWords (dpy, rep.length); UnlockDisplay (dpy); SyncHandle (); *nprop = 0; @@ -107,7 +107,7 @@ XRRQueryOutputProperty (Display *dpy, RROutput output, Atom property) prop_info = (XRRPropertyInfo *) Xmalloc (rbytes); if (prop_info == NULL) { - _XEatData (dpy, nbytes); + _XEatDataWords(dpy, rep.length); UnlockDisplay (dpy); SyncHandle (); return NULL; @@ -313,14 +313,13 @@ XRRGetOutputProperty (Display *dpy, RROutput output, * This part of the code should never be reached. If it is, * the server sent back a property with an invalid format. */ - nbytes = rep.length << 2; - _XEatData(dpy, (unsigned long) nbytes); + _XEatDataWords(dpy, rep.length); UnlockDisplay(dpy); SyncHandle(); return(BadImplementation); } if (! *prop) { - _XEatData(dpy, (unsigned long) nbytes); + _XEatDataWords(dpy, rep.length); UnlockDisplay(dpy); SyncHandle(); return(BadAlloc); diff --git a/src/XrrScreen.c b/src/XrrScreen.c index 54bc2ca..c4e9eb5 100644 --- a/src/XrrScreen.c +++ b/src/XrrScreen.c @@ -129,7 +129,7 @@ doGetScreenResources (Display *dpy, Window window, int poll) if (xrsr == NULL || wire_names == NULL) { if (xrsr) Xfree (xrsr); if (wire_names) Xfree (wire_names); - _XEatData (dpy, (unsigned long) nbytes); + _XEatDataWords (dpy, rep.length); UnlockDisplay (dpy); SyncHandle (); return NULL; -- 1.7.2.5