From 8c873c04cb35fceef64d7a3f66833b78648b3be7 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Thu, 10 Apr 2025 19:43:30 +0200 Subject: [PATCH] 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 --- randr/rrcrtc.c | 6 +++--- randr/rrdispatch.c | 4 ++-- randr/rrinfo.c | 2 +- randr/rrmode.c | 4 ++-- randr/rrmonitor.c | 2 +- randr/rroutput.c | 2 +- randr/rrproperty.c | 6 ++---- randr/rrprovider.c | 4 ++-- randr/rrproviderproperty.c | 4 ++-- randr/rrscreen.c | 5 ++--- 10 files changed, 18 insertions(+), 21 deletions(-) diff --git a/randr/rrcrtc.c b/randr/rrcrtc.c index e08afb595..e3de74a42 100644 --- a/randr/rrcrtc.c +++ b/randr/rrcrtc.c @@ -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; diff --git a/randr/rrdispatch.c b/randr/rrdispatch.c index 3a552f15c..3586fe016 100644 --- a/randr/rrdispatch.c +++ b/randr/rrdispatch.c @@ -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)) { diff --git a/randr/rrinfo.c b/randr/rrinfo.c index 44e25f622..2b52e5afa 100644 --- a/randr/rrinfo.c +++ b/randr/rrinfo.c @@ -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); diff --git a/randr/rrmode.c b/randr/rrmode.c index 6521b079b..108b9ad24 100644 --- a/randr/rrmode.c +++ b/randr/rrmode.c @@ -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); diff --git a/randr/rrmonitor.c b/randr/rrmonitor.c index b596b50da..5bca09d9b 100644 --- a/randr/rrmonitor.c +++ b/randr/rrmonitor.c @@ -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; diff --git a/randr/rroutput.c b/randr/rroutput.c index ac3c59dd9..f7eba84a9 100644 --- a/randr/rroutput.c +++ b/randr/rroutput.c @@ -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; diff --git a/randr/rrproperty.c b/randr/rrproperty.c index cf4f712bb..bca8d683f 100644 --- a/randr/rrproperty.c +++ b/randr/rrproperty.c @@ -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; } diff --git a/randr/rrprovider.c b/randr/rrprovider.c index b6e3d9ea3..0069d8aea 100644 --- a/randr/rrprovider.c +++ b/randr/rrprovider.c @@ -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; } diff --git a/randr/rrproviderproperty.c b/randr/rrproviderproperty.c index 1638ff4ca..d6e0d5788 100644 --- a/randr/rrproviderproperty.c +++ b/randr/rrproviderproperty.c @@ -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; } diff --git a/randr/rrscreen.c b/randr/rrscreen.c index 56e1a4a5f..3aaab5f4d 100644 --- a/randr/rrscreen.c +++ b/randr/rrscreen.c @@ -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)