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 <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2025-04-10 19:14:45 +02:00
parent c4c5f03cb7
commit c6b8b78a29
4 changed files with 10 additions and 16 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;