From c5ecfa5eea7638380603820a28018533a345b0a0 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Mon, 30 Mar 2026 17:17:35 -0700 Subject: [PATCH] randr: handle -Wanalyzer-null-dereference in ProcRRGetScreenInfo() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reported in #1817: xwayland-24.1.6/redhat-linux-build/../randr/rrscreen.c:848:13: warning[-Wanalyzer-null-dereference]: dereference of NULL ‘size’ Move the use of the pointer inside the body of the if statement that allocates the pointer so the static analyzer doesn't have to understand the various conditions are effectively equivalent, despite the different ways they are expressed. Signed-off-by: Alan Coopersmith Part-of: --- randr/rrscreen.c | 76 +++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/randr/rrscreen.c b/randr/rrscreen.c index 395b5738e..4932159e8 100644 --- a/randr/rrscreen.c +++ b/randr/rrscreen.c @@ -794,13 +794,8 @@ ProcRRGetScreenInfo(ClientPtr client) extraLen = 0; } else { - int i, j; - xScreenSizes *size; - CARD16 *rates; - CARD8 *data8; Bool has_rate = RRClientKnowsRates(client); RR10DataPtr pData; - RRScreenSizePtr pSize; pData = RR10GetData(pScreen, output); if (!pData) @@ -826,55 +821,62 @@ ProcRRGetScreenInfo(ClientPtr client) extraLen += rep.nrateEnts * sizeof(CARD16); if (extraLen) { + xScreenSizes *size; + CARD16 *rates; + CARD8 *data8; + extra = (CARD8 *) malloc(extraLen); if (!extra) { free(pData); return BadAlloc; } - } - else - extra = NULL; - /* - * First comes the size information - */ - size = (xScreenSizes *) extra; - rates = (CARD16 *) (size + rep.nSizes); - for (i = 0; i < pData->nsize; i++) { - pSize = &pData->sizes[i]; - size->widthInPixels = pSize->width; - size->heightInPixels = pSize->height; - size->widthInMillimeters = pSize->mmWidth; - size->heightInMillimeters = pSize->mmHeight; - if (client->swapped) { - swaps(&size->widthInPixels); - swaps(&size->heightInPixels); - swaps(&size->widthInMillimeters); - swaps(&size->heightInMillimeters); - } - size++; - if (has_rate) { - *rates = pSize->nRates; + /* + * First comes the size information + */ + size = (xScreenSizes *) extra; + rates = (CARD16 *) (size + rep.nSizes); + for (int i = 0; i < pData->nsize; i++) { + RRScreenSizePtr pSize = &pData->sizes[i]; + + size->widthInPixels = pSize->width; + size->heightInPixels = pSize->height; + size->widthInMillimeters = pSize->mmWidth; + size->heightInMillimeters = pSize->mmHeight; if (client->swapped) { - swaps(rates); + swaps(&size->widthInPixels); + swaps(&size->heightInPixels); + swaps(&size->widthInMillimeters); + swaps(&size->heightInMillimeters); } - rates++; - for (j = 0; j < pSize->nRates; j++) { - *rates = pSize->pRates[j].rate; + size++; + if (has_rate) { + *rates = pSize->nRates; if (client->swapped) { swaps(rates); } rates++; + for (int j = 0; j < pSize->nRates; j++) { + *rates = pSize->pRates[j].rate; + if (client->swapped) { + swaps(rates); + } + rates++; + } } } + + data8 = (CARD8 *) rates; + + if (data8 - (CARD8 *) extra != extraLen) + FatalError("RRGetScreenInfo bad extra len %ld != %ld\n", + (unsigned long) (data8 - (CARD8 *) extra), extraLen); } + else + extra = NULL; + free(pData); - data8 = (CARD8 *) rates; - - if (data8 - (CARD8 *) extra != extraLen) - FatalError("RRGetScreenInfo bad extra len %ld != %ld\n", - (unsigned long) (data8 - (CARD8 *) extra), extraLen); rep.length = bytes_to_int32(extraLen); } if (client->swapped) {