From 9ebc53d7764a869a1216ba616e727e2f592c1cb2 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Thu, 10 Apr 2025 19:23:17 +0200 Subject: [PATCH] miext: 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 --- miext/damage/damage.c | 3 +-- miext/rootless/rootlessScreen.c | 12 ++---------- miext/rootless/rootlessWindow.c | 4 +--- miext/shadow/shadow.c | 3 +-- 4 files changed, 5 insertions(+), 17 deletions(-) diff --git a/miext/damage/damage.c b/miext/damage/damage.c index 0b626eba4..aa262c658 100644 --- a/miext/damage/damage.c +++ b/miext/damage/damage.c @@ -1661,7 +1661,6 @@ miDamageDestroy(DamagePtr pDamage) Bool DamageSetup(ScreenPtr pScreen) { - DamageScrPrivPtr pScrPriv; PictureScreenPtr ps = GetPictureScreenIfSet(pScreen); const DamageScreenFuncsRec miFuncs = { @@ -1684,7 +1683,7 @@ DamageSetup(ScreenPtr pScreen) if (!dixRegisterPrivateKey(&damageWinPrivateKeyRec, PRIVATE_WINDOW, 0)) return FALSE; - pScrPriv = malloc(sizeof(DamageScrPrivRec)); + DamageScrPrivPtr pScrPriv = calloc(1, sizeof(DamageScrPrivRec)); if (!pScrPriv) return FALSE; diff --git a/miext/rootless/rootlessScreen.c b/miext/rootless/rootlessScreen.c index ddb1cf607..71fb82dee 100644 --- a/miext/rootless/rootlessScreen.c +++ b/miext/rootless/rootlessScreen.c @@ -89,7 +89,7 @@ RootlessUpdateScreenPixmap(ScreenPtr pScreen) free(s->pixmap_data); s->pixmap_data_size = rowbytes; - s->pixmap_data = malloc(s->pixmap_data_size); + s->pixmap_data = calloc(1, s->pixmap_data_size); if (s->pixmap_data == NULL) return; @@ -617,8 +617,6 @@ RootlessWakeupHandler(void *data, int result) static Bool RootlessAllocatePrivates(ScreenPtr pScreen) { - RootlessScreenRec *s; - if (!dixRegisterPrivateKey (&rootlessGCPrivateKeyRec, PRIVATE_GC, sizeof(RootlessGCRec))) return FALSE; @@ -630,17 +628,11 @@ RootlessAllocatePrivates(ScreenPtr pScreen) (&rootlessWindowOldPixmapPrivateKeyRec, PRIVATE_WINDOW, 0)) return FALSE; - s = malloc(sizeof(RootlessScreenRec)); + RootlessScreenRec *s = calloc(1, sizeof(RootlessScreenRec)); if (!s) return FALSE; SETSCREENREC(pScreen, s); - s->pixmap_data = NULL; - s->pixmap_data_size = 0; - - s->redisplay_timer = NULL; - s->redisplay_timer_set = FALSE; - return TRUE; } diff --git a/miext/rootless/rootlessWindow.c b/miext/rootless/rootlessWindow.c index f1cdec307..8283a7418 100644 --- a/miext/rootless/rootlessWindow.c +++ b/miext/rootless/rootlessWindow.c @@ -377,7 +377,6 @@ static RootlessWindowRec * RootlessEnsureFrame(WindowPtr pWin) { ScreenPtr pScreen = pWin->drawable.pScreen; - RootlessWindowRec *winRec; RegionRec shape; RegionPtr pShape = NULL; @@ -390,8 +389,7 @@ RootlessEnsureFrame(WindowPtr pWin) if (pWin->drawable.class != InputOutput) return NULL; - winRec = malloc(sizeof(RootlessWindowRec)); - + RootlessWindowRec *winRec = calloc(1, sizeof(RootlessWindowRec)); if (!winRec) return NULL; diff --git a/miext/shadow/shadow.c b/miext/shadow/shadow.c index c0f08780a..2600a518f 100644 --- a/miext/shadow/shadow.c +++ b/miext/shadow/shadow.c @@ -111,7 +111,6 @@ shadowCloseScreen(ScreenPtr pScreen) Bool shadowSetup(ScreenPtr pScreen) { - shadowBufPtr pBuf; if (!dixRegisterPrivateKey(&shadowScrPrivateKeyRec, PRIVATE_SCREEN, 0)) return FALSE; @@ -119,7 +118,7 @@ shadowSetup(ScreenPtr pScreen) if (!DamageSetup(pScreen)) return FALSE; - pBuf = malloc(sizeof(shadowBufRec)); + shadowBufPtr pBuf = calloc(1, sizeof(shadowBufRec)); if (!pBuf) return FALSE; pBuf->pDamage = DamageCreate((DamageReportFunc) NULL,