From d93d9af35a6e45c2471cac46c5ed274d58aabbfc Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Tue, 13 May 2025 16:16:18 +0200 Subject: [PATCH] randr: skip payload assembly in rrGetScreenResources() no data to send If there's no data to send, the whole reply payload can be skipped entirely. This can also ease the whole code flow, and we don't need to rely on the individual copy loops never trying to dereference a NULL pointer. (what the analyzer can't proof). Also scoping several some variables that are only used when there actually is data to send. Signed-off-by: Enrico Weigelt, metux IT consult --- randr/rrscreen.c | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/randr/rrscreen.c b/randr/rrscreen.c index 184b30f32..0209abfcb 100644 --- a/randr/rrscreen.c +++ b/randr/rrscreen.c @@ -488,10 +488,6 @@ rrGetScreenResources(ClientPtr client, Bool query) CARD8 *extra = NULL; unsigned long extraLen = 0; int i, rc, has_primary = 0; - RRCrtc *crtcs; - RROutput *outputs; - xRRModeInfo *modeinfos; - CARD8 *names; REQUEST_SIZE_MATCH(xRRGetScreenResourcesReq); rc = dixLookupWindow(&pWin, stuff->window, client, DixGetAttrAccess); @@ -543,20 +539,19 @@ rrGetScreenResources(ClientPtr client, Bool query) bytes_to_int32(rep.nbytesNames)); extraLen = rep.length << 2; - if (extraLen) { - extra = calloc(1, extraLen); - if (!extra) { - free(modes); - return BadAlloc; - } - } - else - extra = NULL; + if (!extraLen) + goto finish; - crtcs = (RRCrtc *) extra; - outputs = (RROutput *) (crtcs + pScrPriv->numCrtcs); - modeinfos = (xRRModeInfo *) (outputs + pScrPriv->numOutputs); - names = (CARD8 *) (modeinfos + num_modes); + extra = calloc(1, extraLen); + if (!extra) { + free(modes); + return BadAlloc; + } + + RRCrtc *crtcs = (RRCrtc *) extra; + RROutput *outputs = (RROutput *) (crtcs + pScrPriv->numCrtcs); + xRRModeInfo *modeinfos = (xRRModeInfo *) (outputs + pScrPriv->numOutputs); + CARD8* names = (CARD8 *) (modeinfos + num_modes); if (pScrPriv->primaryOutput && pScrPriv->primaryOutput->crtc) { has_primary = 1; @@ -604,8 +599,9 @@ rrGetScreenResources(ClientPtr client, Bool query) memcpy(names, mode->name, mode->mode.nameLength); names += mode->mode.nameLength; } - free(modes); assert(bytes_to_int32((char *) names - (char *) extra) == rep.length); +finish: + free(modes); } if (client->swapped) {