From 2ac3cacf91e3cc22e2ef2f04f18760a0edae9489 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Tue, 13 May 2025 16:16:18 +0200 Subject: [PATCH] (!1972) 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 b81edcc22..7ca21d410 100644 --- a/randr/rrscreen.c +++ b/randr/rrscreen.c @@ -489,10 +489,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); @@ -544,20 +540,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; @@ -605,8 +600,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) {