randr: RROutputSetModes(): simplify buffer allocation / copying

Instead of relying on memcpy() coping with NULL buffer when size == 0,
move the call to the branch where we actually have things to copy.

This also silences yet another analyzer warning.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2025-05-13 11:38:46 +02:00
parent 3279b81d7b
commit acec1156b3

View File

@ -149,7 +149,6 @@ Bool
RROutputSetModes(RROutputPtr output, RROutputSetModes(RROutputPtr output,
RRModePtr * modes, int numModes, int numPreferred) RRModePtr * modes, int numModes, int numPreferred)
{ {
RRModePtr *newModes;
int i; int i;
if (numModes == output->numModes && numPreferred == output->numPreferred) { if (numModes == output->numModes && numPreferred == output->numPreferred) {
@ -163,19 +162,19 @@ RROutputSetModes(RROutputPtr output,
} }
} }
RRModePtr *newModes = NULL;
if (numModes) { if (numModes) {
newModes = xallocarray(numModes, sizeof(RRModePtr)); newModes = xallocarray(numModes, sizeof(RRModePtr));
if (!newModes) if (!newModes)
return FALSE; return FALSE;
memcpy(newModes, modes, numModes * sizeof(RRModePtr));
} }
else
newModes = NULL;
if (output->modes) { if (output->modes) {
for (i = 0; i < output->numModes; i++) for (i = 0; i < output->numModes; i++)
RRModeDestroy(output->modes[i]); RRModeDestroy(output->modes[i]);
free(output->modes); free(output->modes);
} }
memcpy(newModes, modes, numModes * sizeof(RRModePtr));
output->modes = newModes; output->modes = newModes;
output->numModes = numModes; output->numModes = numModes;
output->numPreferred = numPreferred; output->numPreferred = numPreferred;