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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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