From ccf25f25c076f82f5bc91d869de7e1a024d3291a 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 4a1243170..56d17820b 100644 --- a/composite/compalloc.c +++ b/composite/compalloc.c @@ -134,7 +134,6 @@ int compRedirectWindow(ClientPtr pClient, WindowPtr pWin, int update) { CompWindowPtr cw = GetCompWindow(pWin); - CompClientWindowPtr ccw; CompScreenPtr cs = GetCompScreen(pWin->drawable.pScreen); WindowPtr pLayerWin; Bool anyMarked = FALSE; @@ -151,7 +150,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; @@ -160,7 +159,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); @@ -169,7 +168,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; @@ -342,14 +341,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; /* @@ -357,7 +355,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); @@ -366,7 +364,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 bb8f5946d..b55882974 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 e0a565365..79ce2ef3c 100644 --- a/composite/compinit.c +++ b/composite/compinit.c @@ -334,8 +334,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)) @@ -345,7 +343,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;