From 2d1b99e49fc18cc4e423e405fc55730134ab631f Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Thu, 10 Apr 2025 19:41:15 +0200 Subject: [PATCH] record: 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 --- record/record.c | 15 ++++++--------- record/set.c | 2 +- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/record/record.c b/record/record.c index a345798bb..428a70e5c 100644 --- a/record/record.c +++ b/record/record.c @@ -117,7 +117,7 @@ typedef struct _RecordClientsAndProtocolRec { short sizeClients; /* size of pClientIDs array */ unsigned int clientStarted:1; /* record new client connections? */ unsigned int clientDied:1; /* record client disconnections? */ - unsigned int clientIDsSeparatelyAllocated:1; /* pClientIDs malloced? */ + unsigned int clientIDsSeparatelyAllocated:1; /* pClientIDs calloced? */ } RecordClientsAndProtocolRec, *RecordClientsAndProtocolPtr; /* how much bigger to make pRCAP->pClientIDs when reallocing */ @@ -871,8 +871,7 @@ RecordInstallHooks(RecordClientsAndProtocolPtr pRCAP, XID oneclient) RecordClientPrivatePtr pClientPriv; /* no Record proc vector; allocate one */ - pClientPriv = (RecordClientPrivatePtr) - malloc(sizeof(RecordClientPrivateRec)); + pClientPriv = calloc(1, sizeof(RecordClientPrivateRec)); if (!pClientPriv) return BadAlloc; /* copy old proc vector to new */ @@ -1190,7 +1189,7 @@ RecordSanityCheckClientSpecifiers(ClientPtr client, XID *clientspecs, * - XRecordCurrentClients expanded to a list of all currently * connected clients - excludespec (if non-zero) * The returned array may be the passed array modified in place, or - * it may be an malloc'ed array. The caller should keep a pointer to the + * it may be an calloc'ed array. The caller should keep a pointer to the * original array and free the returned array if it is different. * * *pNumClientspecs is set to the number of elements in the returned @@ -1547,7 +1546,6 @@ RecordRegisterClients(RecordContextPtr pContext, ClientPtr client, int nClients; int sizeClients; int totRCAPsize; - RecordClientsAndProtocolPtr pRCAP; int pad; XID recordingClient; @@ -1685,7 +1683,7 @@ RecordRegisterClients(RecordContextPtr pContext, ClientPtr client, /* allocate memory for the whole RCAP */ - pRCAP = (RecordClientsAndProtocolPtr) malloc(totRCAPsize); + RecordClientsAndProtocolPtr pRCAP = calloc(1, totRCAPsize); if (!pRCAP) { err = BadAlloc; goto bailout; @@ -1838,14 +1836,13 @@ static int ProcRecordCreateContext(ClientPtr client) { REQUEST(xRecordCreateContextReq); - RecordContextPtr pContext; RecordContextPtr *ppNewAllContexts = NULL; int err = BadAlloc; REQUEST_AT_LEAST_SIZE(xRecordCreateContextReq); LEGAL_NEW_RESOURCE(stuff->context, client); - pContext = (RecordContextPtr) malloc(sizeof(RecordContextRec)); + RecordContextPtr pContext = calloc(1, sizeof(RecordContextRec)); if (!pContext) goto bailout; @@ -2637,7 +2634,7 @@ RecordConnectionSetupInfo(RecordContextPtr pContext, NewClientInfoRec * pci) int restsize = pci->prefix->length * 4; if (pci->client->swapped) { - char *pConnSetup = (char *) malloc(prefixsize + restsize); + char *pConnSetup = calloc(1, prefixsize + restsize); if (!pConnSetup) return; diff --git a/record/set.c b/record/set.c index e623b7c69..7a86d7b12 100644 --- a/record/set.c +++ b/record/set.c @@ -355,7 +355,7 @@ IntervalListCreateSet(RecordSetInterval * pIntervals, int nIntervals, } else { prls = (IntervalListSetPtr) - malloc(sizeof(IntervalListSet) + + calloc(1, sizeof(IntervalListSet) + nIntervals * sizeof(RecordSetInterval)); if (!prls) goto bailout;