From c6b8b78a293f9343c21f8c1c0bdcdb78ce5bb795 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Thu, 10 Apr 2025 19:14:45 +0200 Subject: [PATCH] composite: 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 --- composite/compalloc.c | 14 ++++++-------- composite/compext.c | 4 ++-- composite/compinit.c | 4 +--- composite/compoverlay.c | 4 +--- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/composite/compalloc.c b/composite/compalloc.c index a3eb592bd..4a6107c0c 100644 --- a/composite/compalloc.c +++ b/composite/compalloc.c @@ -138,7 +138,6 @@ compRedirectWindow(ClientPtr pClient, WindowPtr pWin, int update) BUG_RETURN_VAL(!pClient, BadMatch); CompWindowPtr cw = GetCompWindow(pWin); - CompClientWindowPtr ccw; CompScreenPtr cs = GetCompScreen(pWin->drawable.pScreen); WindowPtr pLayerWin; Bool anyMarked = FALSE; @@ -155,7 +154,7 @@ compRedirectWindow(ClientPtr pClient, WindowPtr pWin, int update) * Only one Manual update is allowed */ if (cw && update == CompositeRedirectManual) - for (ccw = cw->clients; ccw; ccw = ccw->next) + for (CompClientWindowPtr ccw = cw->clients; ccw; ccw = ccw->next) if (ccw->update == CompositeRedirectManual) return BadAccess; @@ -164,7 +163,7 @@ compRedirectWindow(ClientPtr pClient, WindowPtr pWin, int update) * The client *could* allocate multiple, but while supported, * it is not expected to be common */ - ccw = malloc(sizeof(CompClientWindowRec)); + CompClientWindowPtr ccw = calloc(1, sizeof(CompClientWindowRec)); if (!ccw) return BadAlloc; ccw->id = FakeClientID(pClient->index); @@ -173,7 +172,7 @@ compRedirectWindow(ClientPtr pClient, WindowPtr pWin, int update) * Now make sure there's a per-window structure to hang this from */ if (!cw) { - cw = malloc(sizeof(CompWindowRec)); + cw = calloc(1, sizeof(CompWindowRec)); if (!cw) { free(ccw); return BadAlloc; @@ -348,14 +347,13 @@ int compRedirectSubwindows(ClientPtr pClient, WindowPtr pWin, int update) { CompSubwindowsPtr csw = GetCompSubwindows(pWin); - CompClientWindowPtr ccw; WindowPtr pChild; /* * Only one Manual update is allowed */ if (csw && update == CompositeRedirectManual) - for (ccw = csw->clients; ccw; ccw = ccw->next) + for (CompClientWindowPtr ccw = csw->clients; ccw; ccw = ccw->next) if (ccw->update == CompositeRedirectManual) return BadAccess; /* @@ -363,7 +361,7 @@ compRedirectSubwindows(ClientPtr pClient, WindowPtr pWin, int update) * The client *could* allocate multiple, but while supported, * it is not expected to be common */ - ccw = malloc(sizeof(CompClientWindowRec)); + CompClientWindowPtr ccw = calloc(1, sizeof(CompClientWindowRec)); if (!ccw) return BadAlloc; ccw->id = FakeClientID(pClient->index); @@ -372,7 +370,7 @@ compRedirectSubwindows(ClientPtr pClient, WindowPtr pWin, int update) * Now make sure there's a per-window structure to hang this from */ if (!csw) { - csw = malloc(sizeof(CompSubwindowsRec)); + csw = calloc(1, sizeof(CompSubwindowsRec)); if (!csw) { free(ccw); return BadAlloc; diff --git a/composite/compext.c b/composite/compext.c index 51ff41abb..fa4719ca7 100644 --- a/composite/compext.c +++ b/composite/compext.c @@ -700,7 +700,7 @@ PanoramiXCompositeNameWindowPixmap(ClientPtr client) LEGAL_NEW_RESOURCE(stuff->pixmap, client); - if (!(newPix = malloc(sizeof(PanoramiXRes)))) + if (!(newPix = calloc(1, sizeof(PanoramiXRes)))) return BadAlloc; newPix->type = XRT_PIXMAP; @@ -769,7 +769,7 @@ PanoramiXCompositeGetOverlayWindow(ClientPtr client) cs = GetCompScreen(screenInfo.screens[0]); if (!cs->pOverlayWin) { - if (!(overlayWin = malloc(sizeof(PanoramiXRes)))) + if (!(overlayWin = calloc(1, sizeof(PanoramiXRes)))) return BadAlloc; overlayWin->type = XRT_WINDOW; diff --git a/composite/compinit.c b/composite/compinit.c index f75a0a514..c1d57e220 100644 --- a/composite/compinit.c +++ b/composite/compinit.c @@ -331,8 +331,6 @@ compAddAlternateVisuals(ScreenPtr pScreen, CompScreenPtr cs) Bool compScreenInit(ScreenPtr pScreen) { - CompScreenPtr cs; - if (!dixRegisterPrivateKey(&CompScreenPrivateKeyRec, PRIVATE_SCREEN, 0)) return FALSE; if (!dixRegisterPrivateKey(&CompWindowPrivateKeyRec, PRIVATE_WINDOW, 0)) @@ -342,7 +340,7 @@ compScreenInit(ScreenPtr pScreen) if (GetCompScreen(pScreen)) return TRUE; - cs = (CompScreenPtr) malloc(sizeof(CompScreenRec)); + CompScreenPtr cs = calloc(1, sizeof(CompScreenRec)); if (!cs) return FALSE; diff --git a/composite/compoverlay.c b/composite/compoverlay.c index ffff34b7b..e2c4c58bf 100644 --- a/composite/compoverlay.c +++ b/composite/compoverlay.c @@ -96,9 +96,7 @@ CompOverlayClientPtr compCreateOverlayClient(ScreenPtr pScreen, ClientPtr pClient) { CompScreenPtr cs = GetCompScreen(pScreen); - CompOverlayClientPtr pOc; - - pOc = (CompOverlayClientPtr) malloc(sizeof(CompOverlayClientRec)); + CompOverlayClientPtr pOc = calloc(1, sizeof(CompOverlayClientRec)); if (pOc == NULL) return NULL;