From a43105c0da6613798437afaa531c2a576f8efc99 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Sun, 20 Nov 2022 10:30:10 -0800 Subject: Variable scope reductions Signed-off-by: Alan Coopersmith --- src/XRes.c | 44 ++++++++++++++++---------------------------- 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/src/XRes.c b/src/XRes.c index dc5a548..506ca9e 100644 --- a/src/XRes.c +++ b/src/XRes.c @@ -92,7 +92,6 @@ XResQueryClients(Display *dpy, int *num_clients, XResClient **clients) XExtDisplayInfo *info = find_display(dpy); xXResQueryClientsReq *req; xXResQueryClientsReply rep; - XResClient *clnts; int result = 0; *num_clients = 0; @@ -111,16 +110,17 @@ XResQueryClients(Display *dpy, int *num_clients, XResClient **clients) } if (rep.num_clients) { + XResClient *clnts; + if (rep.num_clients < (INT_MAX / sizeof(XResClient))) clnts = Xmalloc(sizeof(XResClient) * rep.num_clients); else clnts = NULL; if (clnts != NULL) { - xXResClient scratch; - int i; + for (int i = 0; i < rep.num_clients; i++) { + xXResClient scratch; - for (i = 0; i < rep.num_clients; i++) { _XRead(dpy, (char *) &scratch, sz_xXResClient); clnts[i].resource_base = scratch.resource_base; clnts[i].resource_mask = scratch.resource_mask; @@ -146,7 +146,6 @@ XResQueryClientResources(Display *dpy, XID xid, XExtDisplayInfo *info = find_display(dpy); xXResQueryClientResourcesReq *req; xXResQueryClientResourcesReply rep; - XResType *typs; int result = 0; *num_types = 0; @@ -166,16 +165,17 @@ XResQueryClientResources(Display *dpy, XID xid, } if (rep.num_types) { + XResType *typs; + if (rep.num_types < (INT_MAX / sizeof(XResType))) typs = Xmalloc(sizeof(XResType) * rep.num_types); else typs = NULL; if (typs != NULL) { - xXResType scratch; - int i; + for (int i = 0; i < rep.num_types; i++) { + xXResType scratch; - for (i = 0; i < rep.num_types; i++) { _XRead(dpy, (char *) &scratch, sz_xXResType); typs[i].resource_type = scratch.resource_type; typs[i].count = scratch.count; @@ -231,9 +231,7 @@ static Bool ReadClientValues(Display *dpy, long num_ids, XResClientIdValue *client_ids /* out */) { - int c; - - for (c = 0; c < num_ids; ++c) { + for (int c = 0; c < num_ids; ++c) { XResClientIdValue *client = client_ids + c; long int value; @@ -262,7 +260,6 @@ XResQueryClientIds( XExtDisplayInfo *info = find_display(dpy); xXResQueryClientIdsReq *req; xXResQueryClientIdsReply rep; - int c; *num_ids = 0; @@ -274,7 +271,7 @@ XResQueryClientIds( req->length += num_specs * 2; /* 2 longs per client id spec */ req->numSpecs = num_specs; - for (c = 0; c < num_specs; ++c) { + for (int c = 0; c < num_specs; ++c) { Data32(dpy, &client_specs[c].client, 4); Data32(dpy, &client_specs[c].mask, 4); } @@ -306,9 +303,7 @@ XResQueryClientIds( void XResClientIdsDestroy(long num_ids, XResClientIdValue *client_ids) { - int c; - - for (c = 0; c < num_ids; ++c) { + for (int c = 0; c < num_ids; ++c) { free(client_ids[c].value); } free(client_ids); @@ -317,11 +312,10 @@ XResClientIdsDestroy(long num_ids, XResClientIdValue *client_ids) XResClientIdType XResGetClientIdType(XResClientIdValue *value) { - int bit; XResClientIdType idType = 0; Bool found = False; - for (bit = 0; bit < XRES_CLIENT_ID_NR; ++bit) { + for (int bit = 0; bit < XRES_CLIENT_ID_NR; ++bit) { if (value->spec.mask & (1 << bit)) { assert(!found); found = True; @@ -367,10 +361,7 @@ static Status ReadResourceSizeValues(Display *dpy, long num_sizes, XResResourceSizeValue *sizes) { - int c; - int d; - - for (c = 0; c < num_sizes; ++c) { + for (int c = 0; c < num_sizes; ++c) { long int num; ReadResourceSizeSpec(dpy, &sizes[c].size); @@ -378,7 +369,7 @@ ReadResourceSizeValues(Display *dpy, sizes[c].num_cross_references = num; sizes[c].cross_references = num ? calloc(num, sizeof(*sizes[c].cross_references)) : NULL; - for (d = 0; d < num; ++d) { + for (int d = 0; d < num; ++d) { ReadResourceSizeSpec(dpy, &sizes[c].cross_references[d]); } } @@ -398,7 +389,6 @@ XResQueryResourceBytes( XExtDisplayInfo *info = find_display(dpy); xXResQueryResourceBytesReq *req; xXResQueryResourceBytesReply rep; - int c; *num_sizes = 0; @@ -412,7 +402,7 @@ XResQueryResourceBytes( req->client = client; req->numSpecs = num_specs; - for (c = 0; c < num_specs; ++c) { + for (int c = 0; c < num_specs; ++c) { Data32(dpy, &resource_specs[c].resource, 4); Data32(dpy, &resource_specs[c].type, 4); } @@ -446,9 +436,7 @@ XResQueryResourceBytes( void XResResourceSizeValuesDestroy(long num_sizes, XResResourceSizeValue *sizes) { - int c; - - for (c = 0; c < num_sizes; ++c) { + for (int c = 0; c < num_sizes; ++c) { free(sizes[c].cross_references); } free(sizes); -- cgit v1.2.1