From 8710ed270fbb9ec905b906826cb09095c57003f8 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Mon, 17 Oct 2022 17:24:23 -0700 Subject: Variable scope reductions as recommended by cppcheck Signed-off-by: Alan Coopersmith --- src/Xrandr.c | 22 +++++++++------------- src/XrrConfig.c | 26 ++++++++++---------------- src/XrrCrtc.c | 8 ++++---- src/XrrMonitor.c | 12 ++++++------ src/XrrProperty.c | 11 +++++------ src/XrrProviderProperty.c | 11 +++++------ src/XrrScreen.c | 3 ++- 7 files changed, 41 insertions(+), 52 deletions(-) diff --git a/src/Xrandr.c b/src/Xrandr.c index fd247e0..9167247 100644 --- a/src/Xrandr.c +++ b/src/Xrandr.c @@ -286,12 +286,12 @@ static Status XRREventToWire(Display *dpy, XEvent *event, xEvent *wire) _X_HIDDEN XExtDisplayInfo * XRRFindDisplay (Display *dpy) { - XExtDisplayInfo *dpyinfo; - XRandRInfo *xrri; - int i, numscreens; + XExtDisplayInfo *dpyinfo = XextFindDisplay (&XRRExtensionInfo, dpy); - dpyinfo = XextFindDisplay (&XRRExtensionInfo, dpy); if (!dpyinfo) { + XRandRInfo *xrri; + int numscreens; + dpyinfo = XextAddDisplay (&XRRExtensionInfo, dpy, XRRExtensionName, &rr_extension_hooks, @@ -300,7 +300,7 @@ XRRFindDisplay (Display *dpy) xrri = Xmalloc (sizeof(XRandRInfo) + sizeof(char *) * numscreens); xrri->config = (XRRScreenConfiguration **)(xrri + 1); - for(i = 0; i < numscreens; i++) + for (int i = 0; i < numscreens; i++) xrri->config[i] = NULL; xrri->major_version = -1; dpyinfo->data = (char *) xrri; @@ -311,21 +311,18 @@ XRRFindDisplay (Display *dpy) static int XRRCloseDisplay (Display *dpy, XExtCodes *codes) { - int i; - XRRScreenConfiguration **configs; XExtDisplayInfo *info = XRRFindDisplay (dpy); - XRandRInfo *xrri; LockDisplay(dpy); /* * free cached data */ if (XextHasExtension(info)) { - xrri = (XRandRInfo *) info->data; + XRandRInfo *xrri = (XRandRInfo *) info->data; if (xrri) { - configs = xrri->config; + XRRScreenConfiguration **configs = xrri->config; - for (i = 0; i < ScreenCount(dpy); i++) { + for (int i = 0; i < ScreenCount(dpy); i++) { if (configs[i] != NULL) XFree (configs[i]); } XFree (xrri); @@ -337,8 +334,7 @@ XRRCloseDisplay (Display *dpy, XExtCodes *codes) int XRRRootToScreen(Display *dpy, Window root) { - int snum; - for (snum = 0; snum < ScreenCount(dpy); snum++) { + for (int snum = 0; snum < ScreenCount(dpy); snum++) { if (RootWindow(dpy, snum) == root) return snum; } return -1; diff --git a/src/XrrConfig.c b/src/XrrConfig.c index e68c45a..48b7987 100644 --- a/src/XrrConfig.c +++ b/src/XrrConfig.c @@ -103,12 +103,9 @@ static XRRScreenConfiguration *_XRRValidateCache (Display *dpy, XExtDisplayInfo *info, int screen) { - XRRScreenConfiguration **configs; - XRandRInfo *xrri; - if ((screen >= 0) && (screen < ScreenCount(dpy)) && XextHasExtension(info)) { - xrri = (XRandRInfo *) info->data; - configs = xrri->config; + XRandRInfo *xrri = (XRandRInfo *) info->data; + XRRScreenConfiguration **configs = xrri->config; if (configs[screen] == NULL) configs[screen] = _XRRGetScreenInfo (dpy, info, RootWindow(dpy, screen)); @@ -123,11 +120,11 @@ Rotation XRRRotations(Display *dpy, int screen, Rotation *current_rotation) { XRRScreenConfiguration *config; XExtDisplayInfo *info = XRRFindDisplay(dpy); - Rotation cr; + LockDisplay(dpy); if ((config = _XRRValidateCache(dpy, info, screen))) { + Rotation cr = config->rotations; *current_rotation = config->current_rotation; - cr = config->rotations; UnlockDisplay(dpy); return cr; } @@ -143,12 +140,11 @@ XRRScreenSize *XRRSizes(Display *dpy, int screen, int *nsizes) { XRRScreenConfiguration *config; XExtDisplayInfo *info = XRRFindDisplay(dpy); - XRRScreenSize *sizes; LockDisplay(dpy); if ((config = _XRRValidateCache(dpy, info, screen))) { + XRRScreenSize *sizes = config->sizes; *nsizes = config->nsizes; - sizes = config->sizes; UnlockDisplay(dpy); return sizes; } @@ -163,11 +159,10 @@ short *XRRRates (Display *dpy, int screen, int sizeID, int *nrates) { XRRScreenConfiguration *config; XExtDisplayInfo *info = XRRFindDisplay(dpy); - short *rates; LockDisplay(dpy); if ((config = _XRRValidateCache(dpy, info, screen))) { - rates = XRRConfigRates (config, sizeID, nrates); + short *rates = XRRConfigRates (config, sizeID, nrates); UnlockDisplay(dpy); return rates; } @@ -183,12 +178,11 @@ Time XRRTimes (Display *dpy, int screen, Time *config_timestamp) { XRRScreenConfiguration *config; XExtDisplayInfo *info = XRRFindDisplay(dpy); - Time ts; LockDisplay(dpy); if ((config = _XRRValidateCache(dpy, info, screen))) { + Time ts = config->timestamp; *config_timestamp = config->config_timestamp; - ts = config->timestamp; UnlockDisplay(dpy); return ts; } else { @@ -207,12 +201,10 @@ static XRRScreenConfiguration *_XRRGetScreenInfo (Display *dpy, _XAsyncHandler async; _XRRVersionState async_state; int nbytes, nbytesRead, rbytes; - int i; xScreenSizes size; struct _XRRScreenConfiguration *scp; XRRScreenSize *ssp; short *rates; - xRRQueryVersionReq *vreq; XRandRInfo *xrri; Bool getting_version = False; @@ -222,6 +214,8 @@ static XRRScreenConfiguration *_XRRGetScreenInfo (Display *dpy, if (xrri->major_version == -1) { + xRRQueryVersionReq *vreq; + /* hide a version query in the request */ GetReq (RRQueryVersion, vreq); vreq->reqType = info->codes->major_opcode; @@ -328,7 +322,7 @@ static XRRScreenConfiguration *_XRRGetScreenInfo (Display *dpy, /* * First the size information */ - for (i = 0; i < rep.nSizes; i++) { + for (unsigned int i = 0; i < rep.nSizes; i++) { _XReadPad (dpy, (char *) &size, SIZEOF (xScreenSizes)); ssp[i].width = size.widthInPixels; diff --git a/src/XrrCrtc.c b/src/XrrCrtc.c index 8316b78..2da7ed2 100644 --- a/src/XrrCrtc.c +++ b/src/XrrCrtc.c @@ -375,11 +375,9 @@ XRRGetCrtcTransform (Display *dpy, { XExtDisplayInfo *info = XRRFindDisplay(dpy); xRRGetCrtcTransformReply rep; - xRRGetCrtcTransformReq *req; int major_version, minor_version; XRRCrtcTransformAttributes *attr; char *extra = NULL, *end = NULL, *e; - int p; *attributes = NULL; @@ -398,6 +396,8 @@ XRRGetCrtcTransform (Display *dpy, } else { + xRRGetCrtcTransformReq *req; + LockDisplay (dpy); GetReq (RRGetCrtcTransform, req); req->reqType = info->codes->major_opcode; @@ -466,7 +466,7 @@ XRRGetCrtcTransform (Display *dpy, memcpy (attr->pendingFilter, e, rep.pendingNbytesFilter); attr->pendingFilter[rep.pendingNbytesFilter] = '\0'; e += (rep.pendingNbytesFilter + 3) & ~3; - for (p = 0; p < rep.pendingNparamsFilter; p++) { + for (unsigned int p = 0; p < rep.pendingNparamsFilter; p++) { INT32 f; if (e + 4 > end) { XFree (attr); @@ -487,7 +487,7 @@ XRRGetCrtcTransform (Display *dpy, memcpy (attr->currentFilter, e, rep.currentNbytesFilter); attr->currentFilter[rep.currentNbytesFilter] = '\0'; e += (rep.currentNbytesFilter + 3) & ~3; - for (p = 0; p < rep.currentNparamsFilter; p++) { + for (unsigned int p = 0; p < rep.currentNparamsFilter; p++) { INT32 f; if (e + 4 > end) { XFree (attr); diff --git a/src/XrrMonitor.c b/src/XrrMonitor.c index adc5330..d9acb80 100644 --- a/src/XrrMonitor.c +++ b/src/XrrMonitor.c @@ -39,10 +39,8 @@ XRRGetMonitors(Display *dpy, Window window, Bool get_active, int *nmonitors) XExtDisplayInfo *info = XRRFindDisplay(dpy); xRRGetMonitorsReply rep; xRRGetMonitorsReq *req; - int nbytes, nbytesRead, rbytes; + int nbytes, nbytesRead; int nmon, noutput; - int m, o; - char *buf, *buf_head; xRRMonitorInfo *xmon; CARD32 *xoutput; XRRMonitorInfo *mon = NULL; @@ -81,6 +79,7 @@ XRRGetMonitors(Display *dpy, Window window, Bool get_active, int *nmonitors) nbytesRead = nmon * SIZEOF(xRRMonitorInfo) + noutput * 4; if (nmon > 0) { + char *buf, *buf_head; /* * first we must compute how much space to allocate for @@ -88,7 +87,8 @@ XRRGetMonitors(Display *dpy, Window window, Bool get_active, int *nmonitors) * allocation, on cleanlyness grounds. */ - rbytes = nmon * sizeof (XRRMonitorInfo) + noutput * sizeof(RROutput); + size_t rbytes = (nmon * sizeof (XRRMonitorInfo)) + + (noutput * sizeof(RROutput)); buf = buf_head = Xmalloc (nbytesRead); mon = Xmalloc (rbytes); @@ -106,7 +106,7 @@ XRRGetMonitors(Display *dpy, Window window, Bool get_active, int *nmonitors) output = (RROutput *) (mon + nmon); - for (m = 0; m < nmon; m++) { + for (int m = 0; m < nmon; m++) { xmon = (xRRMonitorInfo *) buf; mon[m].name = xmon->name; mon[m].primary = xmon->primary; @@ -129,7 +129,7 @@ XRRGetMonitors(Display *dpy, Window window, Bool get_active, int *nmonitors) return NULL; } rep.noutputs -= xmon->noutput; - for (o = 0; o < xmon->noutput; o++) + for (int o = 0; o < xmon->noutput; o++) output[o] = xoutput[o]; output += xmon->noutput; buf += xmon->noutput * 4; diff --git a/src/XrrProperty.c b/src/XrrProperty.c index 502e834..0a4471a 100644 --- a/src/XrrProperty.c +++ b/src/XrrProperty.c @@ -39,7 +39,6 @@ XRRListOutputProperties (Display *dpy, RROutput output, int *nprop) XExtDisplayInfo *info = XRRFindDisplay(dpy); xRRListOutputPropertiesReply rep; xRRListOutputPropertiesReq *req; - int nbytes, rbytes; Atom *props = NULL; RRCheckExtension (dpy, info, NULL); @@ -58,8 +57,8 @@ XRRListOutputProperties (Display *dpy, RROutput output, int *nprop) } if (rep.nAtoms) { - rbytes = rep.nAtoms * sizeof (Atom); - nbytes = rep.nAtoms << 2; + size_t rbytes = rep.nAtoms * sizeof (Atom); + size_t nbytes = rep.nAtoms << 2; props = (Atom *) Xmalloc (rbytes); if (props == NULL) { @@ -85,7 +84,7 @@ XRRQueryOutputProperty (Display *dpy, RROutput output, Atom property) XExtDisplayInfo *info = XRRFindDisplay(dpy); xRRQueryOutputPropertyReply rep; xRRQueryOutputPropertyReq *req; - unsigned int rbytes, nbytes; + unsigned int nbytes; XRRPropertyInfo *prop_info; RRCheckExtension (dpy, info, NULL); @@ -104,7 +103,7 @@ XRRQueryOutputProperty (Display *dpy, RROutput output, Atom property) } if (rep.length < ((INT_MAX / sizeof(long)) - sizeof (XRRPropertyInfo))) { - rbytes = sizeof (XRRPropertyInfo) + (rep.length * sizeof (long)); + size_t rbytes = sizeof (XRRPropertyInfo) + (rep.length * sizeof (long)); nbytes = rep.length << 2; prop_info = Xmalloc (rbytes); @@ -257,7 +256,6 @@ XRRGetOutputProperty (Display *dpy, RROutput output, XExtDisplayInfo *info = XRRFindDisplay(dpy); xRRGetOutputPropertyReply rep; xRRGetOutputPropertyReq *req; - unsigned long nbytes, rbytes; /* Always initialize return values, in case callers fail to initialize them and fail to check the return code for an error. */ @@ -289,6 +287,7 @@ XRRGetOutputProperty (Display *dpy, RROutput output, if (rep.propertyType != None) { int format = rep.format; + size_t nbytes, rbytes; /* * Protect against both integer overflow and just plain oversized diff --git a/src/XrrProviderProperty.c b/src/XrrProviderProperty.c index 241e8ee..6d0d186 100644 --- a/src/XrrProviderProperty.c +++ b/src/XrrProviderProperty.c @@ -39,7 +39,6 @@ XRRListProviderProperties (Display *dpy, RRProvider provider, int *nprop) XExtDisplayInfo *info = XRRFindDisplay(dpy); xRRListProviderPropertiesReply rep; xRRListProviderPropertiesReq *req; - int nbytes, rbytes; Atom *props = NULL; RRCheckExtension (dpy, info, NULL); @@ -58,8 +57,8 @@ XRRListProviderProperties (Display *dpy, RRProvider provider, int *nprop) } if (rep.nAtoms) { - rbytes = rep.nAtoms * sizeof (Atom); - nbytes = rep.nAtoms << 2; + size_t rbytes = rep.nAtoms * sizeof (Atom); + size_t nbytes = rep.nAtoms << 2; props = (Atom *) Xmalloc (rbytes); if (props == NULL) { @@ -85,7 +84,7 @@ XRRQueryProviderProperty (Display *dpy, RRProvider provider, Atom property) XExtDisplayInfo *info = XRRFindDisplay(dpy); xRRQueryProviderPropertyReply rep; xRRQueryProviderPropertyReq *req; - unsigned int rbytes, nbytes; + unsigned int nbytes; XRRPropertyInfo *prop_info; RRCheckExtension (dpy, info, NULL); @@ -104,7 +103,7 @@ XRRQueryProviderProperty (Display *dpy, RRProvider provider, Atom property) } if (rep.length < ((INT_MAX / sizeof(long)) - sizeof (XRRPropertyInfo))) { - rbytes = sizeof (XRRPropertyInfo) + (rep.length * sizeof (long)); + size_t rbytes = sizeof (XRRPropertyInfo) + (rep.length * sizeof (long)); nbytes = rep.length << 2; prop_info = Xmalloc (rbytes); @@ -257,7 +256,6 @@ XRRGetProviderProperty (Display *dpy, RRProvider provider, XExtDisplayInfo *info = XRRFindDisplay(dpy); xRRGetProviderPropertyReply rep; xRRGetProviderPropertyReq *req; - unsigned long nbytes, rbytes; /* Always initialize return values, in case callers fail to initialize them and fail to check the return code for an error. */ @@ -289,6 +287,7 @@ XRRGetProviderProperty (Display *dpy, RRProvider provider, if (rep.propertyType != None) { int format = rep.format; + unsigned long nbytes, rbytes; /* * Protect against both integer overflow and just plain oversized diff --git a/src/XrrScreen.c b/src/XrrScreen.c index 1f7ffe6..3773c88 100644 --- a/src/XrrScreen.c +++ b/src/XrrScreen.c @@ -47,7 +47,6 @@ doGetScreenResources (Display *dpy, Window window, int poll) _XRRVersionState async_state; int nbytes, nbytesRead, rbytes; int i; - xRRQueryVersionReq *vreq; XRRScreenResources *xrsr; char *names; char *wire_names, *wire_name; @@ -61,6 +60,8 @@ doGetScreenResources (Display *dpy, Window window, int poll) if (xrri->major_version == -1) { + xRRQueryVersionReq *vreq; + /* hide a version query in the request */ GetReq (RRQueryVersion, vreq); vreq->reqType = info->codes->major_opcode; -- cgit v1.2.1