randr: refine allocation and copying of optional buffers

Simplifying the code flow allocating/checking/copying some buffers in
RRConfigureOutputProperty() and RRConfigureProviderProperty() so it's
easier to understand for both the human reader as well as the analyzer.

Depending on whether we have elements to process, a temporary buffer needs
to be allocated, checked for successful allocation and copy over data. The
way it's currently done is technically correct, but unnecessarily complex to
understand: instead of just branching on whether there are elements and doing
all the buffer-related things only then, the branching is done just somewhere
in the middle, only on checking for allocation failure, and relying on both
calloc() and memcpy() not doing weird things when size is zero.

It's easy to simplify by putting it all behind one if statement and so make
things easier for both human reader as well as the analyzer (so it's not
spilling out false alarms here anymore) and also drops unnecessary calls
in the zero-size case.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2025-05-12 18:51:26 +02:00
parent ed289f734b
commit 5b13d85464
2 changed files with 19 additions and 16 deletions

View File

@ -360,7 +360,6 @@ RRConfigureOutputProperty(RROutputPtr output, Atom property,
{
RRPropertyPtr prop = RRQueryOutputProperty(output, property);
Bool add = FALSE;
INT32 *new_values;
if (!prop) {
prop = RRCreateOutputProperty(property);
@ -380,14 +379,17 @@ RRConfigureOutputProperty(RROutputPtr output, Atom property,
return BadMatch;
}
new_values = xallocarray(num_values, sizeof(INT32));
if (!new_values && num_values) {
if (add)
RRDestroyOutputProperty(prop);
return BadAlloc;
}
if (num_values)
INT32 *new_values = NULL;
if (num_values) {
new_values = calloc(num_values, sizeof(INT32));
if (!new_values) {
if (add)
RRDestroyOutputProperty(prop);
return BadAlloc;
}
memcpy(new_values, values, num_values * sizeof(INT32));
}
/*
* Property moving from pending to non-pending

View File

@ -277,7 +277,6 @@ RRConfigureProviderProperty(RRProviderPtr provider, Atom property,
{
RRPropertyPtr prop = RRQueryProviderProperty(provider, property);
Bool add = FALSE;
INT32 *new_values;
if (!prop) {
prop = RRCreateProviderProperty(property);
@ -297,14 +296,16 @@ RRConfigureProviderProperty(RRProviderPtr provider, Atom property,
return BadMatch;
}
new_values = xallocarray(num_values, sizeof(INT32));
if (!new_values && num_values) {
if (add)
RRDestroyProviderProperty(prop);
return BadAlloc;
}
if (num_values)
INT32 *new_values = NULL;
if (num_values) {
new_values = calloc(num_values, sizeof(INT32));
if (!new_values) {
if (add)
RRDestroyProviderProperty(prop);
return BadAlloc;
}
memcpy(new_values, values, num_values * sizeof(INT32));
}
/*
* Property moving from pending to non-pending