randr: handle -Wanalyzer-null-dereference in ProcRRGetScreenInfo()

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 <alan.coopersmith@oracle.com>
Part-of: <https://gitlab.freedesktop.org/xorg/xserver/-/merge_requests/2165>
This commit is contained in:
Alan Coopersmith
2026-03-30 17:17:35 -07:00
committed by Marge Bot
parent d7775b5682
commit c5ecfa5eea

View File

@@ -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) {