From 5b13d85464aa6dfbcc1a5611719e225a7cf977d6 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Mon, 12 May 2025 18:51:26 +0200 Subject: [PATCH] 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 --- randr/rrproperty.c | 18 ++++++++++-------- randr/rrproviderproperty.c | 17 +++++++++-------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/randr/rrproperty.c b/randr/rrproperty.c index 2a4e33705..cf4f712bb 100644 --- a/randr/rrproperty.c +++ b/randr/rrproperty.c @@ -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 diff --git a/randr/rrproviderproperty.c b/randr/rrproviderproperty.c index aec86e4d0..11d986b33 100644 --- a/randr/rrproviderproperty.c +++ b/randr/rrproviderproperty.c @@ -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