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 <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2025-04-10 19:41:15 +02:00
parent a6ec907b22
commit 2d1b99e49f
2 changed files with 7 additions and 10 deletions

View File

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

View File

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