randr: use calloc() instead of malloc()

Using calloc() instead of malloc() as preventive measure, so there
never can be any hidden bugs or leaks due uninitialized memory.

The extra cost of using this compiler intrinsic should be practically
impossible to measure - in many cases a good compiler can even deduce
if certain areas really don't need to be zero'd (because they're written
to right after allocation) and create more efficient machine code.

The code pathes in question are pretty cold anyways, so it's probably
not worth even thinking about potential extra runtime costs.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2025-04-10 19:43:30 +02:00
parent 2d1b99e49f
commit 8c873c04cb
10 changed files with 18 additions and 21 deletions

View File

@ -1204,7 +1204,7 @@ ProcRRGetCrtcInfo(ClientPtr client)
rep.length = bytes_to_int32(extraLen);
if (extraLen) {
extra = malloc(extraLen);
extra = calloc(1, extraLen);
if (!extra)
return BadAlloc;
}
@ -1658,7 +1658,7 @@ ProcRRGetCrtcGamma(ClientPtr client)
len = crtc->gammaSize * 3 * 2;
if (crtc->gammaSize) {
extra = malloc(len);
extra = calloc(1, len);
if (!extra)
return BadAlloc;
}
@ -1954,7 +1954,7 @@ RRReplaceScanoutPixmap(DrawablePtr pDrawable, PixmapPtr pPixmap, Bool enable)
PixmapPtr *saved_scanout_pixmap;
int i;
saved_scanout_pixmap = malloc(sizeof(PixmapPtr)*pScrPriv->numCrtcs);
saved_scanout_pixmap = calloc(pScrPriv->numCrtcs, sizeof(PixmapPtr));
if (saved_scanout_pixmap == NULL)
return FALSE;

View File

@ -113,7 +113,7 @@ ProcRRSelectInput(ClientPtr client)
if (!pRREvent) {
/* build the entry */
pRREvent = (RREventPtr) malloc(sizeof(RREventRec));
pRREvent = calloc(1, sizeof(RREventRec));
if (!pRREvent)
return BadAlloc;
pRREvent->next = 0;
@ -135,7 +135,7 @@ ProcRRSelectInput(ClientPtr client)
* done through the resource database.
*/
if (!pHead) {
pHead = (RREventPtr *) malloc(sizeof(RREventPtr));
pHead = calloc(1, sizeof(RREventPtr));
if (!pHead ||
!AddResource(pWin->drawable.id, RREventType,
(void *) pHead)) {

View File

@ -59,7 +59,7 @@ RROldModeAdd(RROutputPtr output, RRScreenSizePtr size, int refresh)
modes = reallocarray(output->modes,
output->numModes + 1, sizeof(RRModePtr));
else
modes = malloc(sizeof(RRModePtr));
modes = calloc(1, sizeof(RRModePtr));
if (!modes) {
RRModeDestroy(mode);
FreeResource(mode->mode.id, 0);

View File

@ -71,7 +71,7 @@ RRModeCreate(xRRModeInfo * modeInfo, const char *name, ScreenPtr userScreen)
if (!RRInit())
return NULL;
mode = malloc(sizeof(RRModeRec) + modeInfo->nameLength + 1);
mode = calloc(1, sizeof(RRModeRec) + modeInfo->nameLength + 1);
if (!mode)
return NULL;
mode->refcnt = 1;
@ -84,7 +84,7 @@ RRModeCreate(xRRModeInfo * modeInfo, const char *name, ScreenPtr userScreen)
if (num_modes)
newModes = reallocarray(modes, num_modes + 1, sizeof(RRModePtr));
else
newModes = malloc(sizeof(RRModePtr));
newModes = calloc(1, sizeof(RRModePtr));
if (!newModes) {
free(mode);

View File

@ -502,7 +502,7 @@ RRMonitorAdd(ClientPtr client, ScreenPtr screen, RRMonitorPtr monitor)
pScrPriv->numMonitors + 1,
sizeof (RRMonitorPtr));
else
monitors = malloc(sizeof (RRMonitorPtr));
monitors = calloc(1, sizeof(RRMonitorPtr));
if (!monitors)
return BadAlloc;

View File

@ -209,7 +209,7 @@ RROutputAddUserMode(RROutputPtr output, RRModePtr mode)
newModes = reallocarray(output->userModes,
output->numUserModes + 1, sizeof(RRModePtr));
else
newModes = malloc(sizeof(RRModePtr));
newModes = calloc(1, sizeof(RRModePtr));
if (!newModes)
return BadAlloc;

View File

@ -108,9 +108,7 @@ RRInitOutputPropertyValue(RRPropertyValuePtr property_value)
static RRPropertyPtr
RRCreateOutputProperty(Atom property)
{
RRPropertyPtr prop;
prop = (RRPropertyPtr) malloc(sizeof(RRPropertyRec));
RRPropertyPtr prop = calloc(1, sizeof(RRPropertyRec));
if (!prop)
return NULL;
prop->next = NULL;
@ -705,7 +703,7 @@ ProcRRGetOutputProperty(ClientPtr client)
len = min(n - ind, 4 * stuff->longLength);
if (len) {
extra = malloc(len);
extra = calloc(1, len);
if (!extra)
return BadAlloc;
}

View File

@ -107,7 +107,7 @@ ProcRRGetProviders (ClientPtr client)
};
if (extraLen) {
extra = malloc(extraLen);
extra = calloc(1, extraLen);
if (!extra)
return BadAlloc;
} else
@ -184,7 +184,7 @@ ProcRRGetProviderInfo (ClientPtr client)
extraLen = rep.length << 2;
if (extraLen) {
extra = malloc(extraLen);
extra = calloc(1, extraLen);
if (!extra)
return BadAlloc;
}

View File

@ -168,7 +168,7 @@ RRChangeProviderProperty(RRProviderPtr provider, Atom property, Atom type,
void *new_data = NULL, *old_data = NULL;
total_size = total_len * size_in_bytes;
new_value.data = (void *) malloc(total_size);
new_value.data = calloc(1, total_size);
if (!new_value.data && total_size) {
if (add)
RRDestroyProviderProperty(prop);
@ -604,7 +604,7 @@ ProcRRGetProviderProperty(ClientPtr client)
len = min(n - ind, 4 * stuff->longLength);
if (len) {
extra = malloc(len);
extra = calloc(1, len);
if (!extra)
return BadAlloc;
}

View File

@ -422,7 +422,7 @@ rrGetMultiScreenResources(ClientPtr client, Bool query, ScreenPtr pScreen)
extraLen = rep.length << 2;
if (extraLen) {
extra = malloc(extraLen);
extra = calloc(1, extraLen);
if (!extra) {
return BadAlloc;
}
@ -649,7 +649,6 @@ typedef struct _RR10Data {
static RR10DataPtr
RR10GetData(ScreenPtr pScreen, RROutputPtr output)
{
RR10DataPtr data;
RRScreenSizePtr size;
int nmode = output->numModes + output->numUserModes;
int o, os, l, r;
@ -659,7 +658,7 @@ RR10GetData(ScreenPtr pScreen, RROutputPtr output)
Bool *used;
/* Make sure there is plenty of space for any combination */
data = malloc(sizeof(RR10DataRec) +
RR10DataPtr data = calloc(1, sizeof(RR10DataRec) +
sizeof(RRScreenSize) * nmode +
sizeof(RRScreenRate) * nmode + sizeof(Bool) * nmode);
if (!data)