Xext: 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 18:43:08 +02:00
parent 45d7b62d95
commit c96901a85a
18 changed files with 49 additions and 62 deletions

View File

@ -145,10 +145,9 @@ ProcDPMSSelectInput(register ClientPtr client)
}
/* build the entry */
pNewEvent = (DPMSEventPtr)malloc(sizeof(DPMSEventRec));
pNewEvent = calloc(1, sizeof(DPMSEventRec));
if (!pNewEvent)
return BadAlloc;
pNewEvent->next = 0;
pNewEvent->client = client;
pNewEvent->mask = stuff->eventMask;
/*
@ -164,7 +163,7 @@ ProcDPMSSelectInput(register ClientPtr client)
* of clients selecting input
*/
if (i != Success || !pHead) {
pHead = (DPMSEventPtr *)malloc(sizeof(DPMSEventPtr));
pHead = calloc(1, sizeof(DPMSEventPtr));
if (!pHead ||
!AddResource(eventResource, DPMSEventType, (void *)pHead)) {
FreeResource(clientResource, X11_RESTYPE_NONE);

View File

@ -39,7 +39,7 @@ ht_create(int keySize,
{
int c;
int numBuckets;
HashTable ht = malloc(sizeof(struct HashTableRec));
HashTable ht = calloc(1, sizeof(struct HashTableRec));
if (!ht) {
return NULL;
@ -127,7 +127,7 @@ ht_add(HashTable ht, const void *key)
if (!elem) {
goto outOfMemory;
}
elem->key = malloc(ht->keySize);
elem->key = calloc(1, ht->keySize);
if (!elem->key) {
goto outOfMemory;
}

View File

@ -368,7 +368,7 @@ XineramaRegisterConnectionBlockCallback(void (*func) (void))
{
XineramaConnectionCallbackList *newlist;
if (!(newlist = malloc(sizeof(XineramaConnectionCallbackList))))
if (!(newlist = calloc(1, sizeof(XineramaConnectionCallbackList))))
return FALSE;
newlist->next = ConnectionCallbackList;
@ -439,7 +439,6 @@ PanoramiXExtensionInit(void)
Bool success = FALSE;
ExtensionEntry *extEntry;
ScreenPtr pScreen = screenInfo.screens[0];
PanoramiXScreenPtr pScreenPriv;
if (noPanoramiXExtension)
return;
@ -476,7 +475,7 @@ PanoramiXExtensionInit(void)
FOR_NSCREENS_BACKWARD(i) {
pScreen = screenInfo.screens[i];
pScreenPriv = malloc(sizeof(PanoramiXScreenRec));
PanoramiXScreenPtr pScreenPriv = calloc(1, sizeof(PanoramiXScreenRec));
dixSetPrivate(&pScreen->devPrivates, PanoramiXScreenKey,
pScreenPriv);
if (!pScreenPriv) {

View File

@ -117,7 +117,7 @@ PanoramiXCreateWindow(ClientPtr client)
}
}
if (!(newWin = malloc(sizeof(PanoramiXRes))))
if (!(newWin = calloc(1, sizeof(PanoramiXRes))))
return BadAlloc;
newWin->type = XRT_WINDOW;
@ -694,7 +694,7 @@ PanoramiXCreatePixmap(ClientPtr client)
if (result != Success)
return (result == BadValue) ? BadDrawable : result;
if (!(newPix = malloc(sizeof(PanoramiXRes))))
if (!(newPix = calloc(1, sizeof(PanoramiXRes))))
return BadAlloc;
newPix->type = XRT_PIXMAP;
@ -801,7 +801,7 @@ PanoramiXCreateGC(ClientPtr client)
}
}
if (!(newGC = malloc(sizeof(PanoramiXRes))))
if (!(newGC = calloc(1, sizeof(PanoramiXRes))))
return BadAlloc;
newGC->type = XRT_GC;
@ -2289,7 +2289,7 @@ PanoramiXCreateColormap(ClientPtr client)
if (result != Success)
return result;
if (!(newCmap = malloc(sizeof(PanoramiXRes))))
if (!(newCmap = calloc(1, sizeof(PanoramiXRes))))
return BadAlloc;
newCmap->type = XRT_COLORMAP;
@ -2361,7 +2361,7 @@ PanoramiXCopyColormapAndFree(ClientPtr client)
if (result != Success)
return result;
if (!(newCmap = malloc(sizeof(PanoramiXRes))))
if (!(newCmap = calloc(1, sizeof(PanoramiXRes))))
return BadAlloc;
newCmap->type = XRT_COLORMAP;

View File

@ -1217,7 +1217,7 @@ ProcScreenSaverSuspend(ClientPtr client)
* to the record, so the screensaver will be re-enabled and the record freed
* if the client disconnects without reenabling it first.
*/
this = malloc(sizeof(ScreenSaverSuspensionRec));
this = calloc(1, sizeof(ScreenSaverSuspensionRec));
if (!this)
return BadAlloc;

View File

@ -385,7 +385,7 @@ SecurityEventSelectForAuthorization(SecurityAuthorizationPtr pAuth,
}
}
pEventClient = malloc(sizeof(OtherClients));
pEventClient = calloc(1, sizeof(OtherClients));
if (!pEventClient)
return BadAlloc;
pEventClient->mask = mask;
@ -406,7 +406,6 @@ ProcSecurityGenerateAuthorization(ClientPtr client)
REQUEST(xSecurityGenerateAuthorizationReq);
int len; /* request length in CARD32s */
Bool removeAuth = FALSE; /* if bailout, call RemoveAuthorization? */
SecurityAuthorizationPtr pAuth = NULL; /* auth we are creating */
int err; /* error to return from this function */
XID authId; /* authorization ID assigned by os layer */
xSecurityGenerateAuthorizationReply rep; /* reply struct */
@ -493,8 +492,7 @@ ProcSecurityGenerateAuthorization(ClientPtr client)
stuff->nbytesAuthData, protodata,
&authdata_len, &pAuthdata);
if ((XID) ~0L == authId) {
err = SecurityErrorBase + XSecurityBadAuthorizationProtocol;
goto bailout;
return SecurityErrorBase + XSecurityBadAuthorizationProtocol;
}
/* now that we've added the auth, remember to remove it if we have to
@ -504,7 +502,7 @@ ProcSecurityGenerateAuthorization(ClientPtr client)
/* associate additional information with this auth ID */
pAuth = malloc(sizeof(SecurityAuthorizationRec));
SecurityAuthorizationPtr pAuth = calloc(1, sizeof(SecurityAuthorizationRec));
if (!pAuth) {
err = BadAlloc;
goto bailout;

View File

@ -752,7 +752,7 @@ ProcShapeSelectInput(ClientPtr client)
}
/* build the entry */
pNewShapeEvent = malloc(sizeof(ShapeEventRec));
pNewShapeEvent = calloc(1, sizeof(ShapeEventRec));
if (!pNewShapeEvent)
return BadAlloc;
pNewShapeEvent->next = 0;
@ -773,7 +773,7 @@ ProcShapeSelectInput(ClientPtr client)
* done through the resource database.
*/
if (!pHead) {
pHead = malloc(sizeof(ShapeEventPtr));
pHead = calloc(1, sizeof(ShapeEventPtr));
if (!pHead ||
!AddResource(pWin->drawable.id, ShapeEventType,
(void *) pHead)) {
@ -962,7 +962,7 @@ ProcShapeGetRectangles(ClientPtr client)
}
if (!region) {
nrects = 1;
rects = malloc(sizeof(xRectangle));
rects = calloc(1, sizeof(xRectangle));
if (!rects)
return BadAlloc;
switch (stuff->kind) {

View File

@ -367,7 +367,7 @@ ProcShmAttach(ClientPtr client)
shmdesc->refcnt++;
}
else {
shmdesc = malloc(sizeof(ShmDescRec));
shmdesc = calloc(1, sizeof(ShmDescRec));
if (!shmdesc)
return BadAlloc;
#ifdef SHM_FD_PASSING
@ -756,7 +756,6 @@ static int
ProcPanoramiXShmGetImage(ClientPtr client)
{
PanoramiXRes *draw;
DrawablePtr *drawables;
DrawablePtr pDraw;
xShmGetImageReply xgi;
ShmDescPtr shmdesc;
@ -831,7 +830,7 @@ ProcPanoramiXShmGetImage(ClientPtr client)
VERIFY_SHMSIZE(shmdesc, stuff->offset, length, client);
drawables = calloc(PanoramiXNumScreens, sizeof(DrawablePtr));
DrawablePtr *drawables = calloc(PanoramiXNumScreens, sizeof(DrawablePtr));
if (!drawables)
return BadAlloc;
@ -951,7 +950,7 @@ ProcPanoramiXShmCreatePixmap(ClientPtr client)
VERIFY_SHMSIZE(shmdesc, stuff->offset, size, client);
if (!(newPix = malloc(sizeof(PanoramiXRes))))
if (!(newPix = calloc(1, sizeof(PanoramiXRes))))
return BadAlloc;
newPix->type = XRT_PIXMAP;
@ -1144,7 +1143,7 @@ ProcShmAttachFd(ClientPtr client)
return BadMatch;
}
shmdesc = malloc(sizeof(ShmDescRec));
shmdesc = calloc(1, sizeof(ShmDescRec));
if (!shmdesc) {
close(fd);
return BadAlloc;
@ -1261,7 +1260,7 @@ ProcShmCreateSegment(ClientPtr client)
close(fd);
return BadAlloc;
}
shmdesc = malloc(sizeof(ShmDescRec));
shmdesc = calloc(1, sizeof(ShmDescRec));
if (!shmdesc) {
close(fd);
return BadAlloc;

View File

@ -72,7 +72,7 @@ ClientSleepUntil(ClientPtr client,
TimeStamp *revive,
void (*notifyFunc) (ClientPtr, void *), void *closure)
{
SertafiedPtr pRequest, pReq, pPrev;
SertafiedPtr pReq, pPrev;
if (SertafiedGeneration != serverGeneration) {
SertafiedResType = CreateNewResourceType(SertafiedDelete,
@ -82,7 +82,8 @@ ClientSleepUntil(ClientPtr client,
SertafiedGeneration = serverGeneration;
BlockHandlerRegistered = FALSE;
}
pRequest = malloc(sizeof(SertafiedRec));
SertafiedPtr pRequest = calloc(1, sizeof(SertafiedRec));
if (!pRequest)
return FALSE;
pRequest->pClient = client;

View File

@ -764,8 +764,6 @@ SyncChangeCounter(SyncCounter * pCounter, int64_t newval)
static Bool
SyncEventSelectForAlarm(SyncAlarm * pAlarm, ClientPtr client, Bool wantevents)
{
SyncAlarmClientList *pClients;
if (client == pAlarm->client) { /* alarm owner */
pAlarm->events = wantevents;
return Success;
@ -773,7 +771,8 @@ SyncEventSelectForAlarm(SyncAlarm * pAlarm, ClientPtr client, Bool wantevents)
/* see if the client is already on the list (has events selected) */
for (pClients = pAlarm->pEventClients; pClients; pClients = pClients->next) {
for (SyncAlarmClientList *pClients = pClients = pAlarm->pEventClients;
pClients; pClients = pClients->next) {
if (pClients->client == client) {
/* client's presence on the list indicates desire for
* events. If the client doesn't want events, remove it
@ -799,7 +798,7 @@ SyncEventSelectForAlarm(SyncAlarm * pAlarm, ClientPtr client, Bool wantevents)
/* add new client to pAlarm->pEventClients */
pClients = malloc(sizeof(SyncAlarmClientList));
SyncAlarmClientList *pClients = calloc(1, sizeof(SyncAlarmClientList));
if (!pClients)
return BadAlloc;
@ -934,7 +933,7 @@ SyncCreate(ClientPtr client, XID id, unsigned char type)
switch (type) {
case SYNC_COUNTER:
pSync = malloc(sizeof(SyncCounter));
pSync = calloc(1, sizeof(SyncCounter));
resType = RTCounter;
break;
case SYNC_FENCE:
@ -1030,9 +1029,7 @@ SyncCreateSystemCounter(const char *name,
SyncCounter *pCounter = SyncCreateCounter(NULL, FakeClientID(0), initial);
if (pCounter) {
SysCounterInfo *psci;
psci = malloc(sizeof(SysCounterInfo));
SysCounterInfo *psci = calloc(1, sizeof(SysCounterInfo));
if (!psci) {
FreeResource(pCounter->sync.id, X11_RESTYPE_NONE);
return pCounter;
@ -1311,7 +1308,7 @@ ProcSyncListSystemCounters(ClientPtr client)
}
if (len) {
walklist = list = malloc(len);
walklist = list = calloc(1, len);
if (!list)
return BadAlloc;
}
@ -1739,7 +1736,7 @@ ProcSyncCreateAlarm(ClientPtr client)
if (len != (Ones(vmask) + Ones(vmask & (XSyncCAValue | XSyncCADelta))))
return BadLength;
if (!(pAlarm = malloc(sizeof(SyncAlarm)))) {
if (!(pAlarm = calloc(1, sizeof(SyncAlarm)))) {
return BadAlloc;
}

View File

@ -75,9 +75,7 @@ typedef struct {
static DisplayModePtr
VidModeCreateMode(void)
{
DisplayModePtr mode;
mode = malloc(sizeof(DisplayModeRec));
DisplayModePtr mode = calloc(1, sizeof(DisplayModeRec));
if (mode != NULL) {
mode->name = "";
mode->VScan = 1; /* divides refresh rate. default = 1 */
@ -1636,7 +1634,7 @@ ProcVidModeSetClientVersion(ClientPtr client)
REQUEST_SIZE_MATCH(xXF86VidModeSetClientVersionReq);
if ((pPriv = VM_GETPRIV(client)) == NULL) {
pPriv = malloc(sizeof(VidModePrivRec));
pPriv = calloc(1, sizeof(VidModePrivRec));
if (!pPriv)
return BadAlloc;
VM_SETPRIV(client, pPriv);

View File

@ -204,7 +204,6 @@ XaceCensorImage(ClientPtr client,
if (nRects > 0) { /* we have something to censor */
GCPtr pScratchGC = NULL;
PixmapPtr pPix = NULL;
xRectangle *pRects = NULL;
Bool failed = FALSE;
int depth = 1;
int bitsPerPixel = 1;
@ -213,7 +212,7 @@ XaceCensorImage(ClientPtr client,
/* convert region to list-of-rectangles for PolyFillRect */
pRects = malloc(nRects * sizeof(xRectangle));
xRectangle *pRects = calloc(1, nRects * sizeof(xRectangle));
if (!pRects) {
failed = TRUE;
goto failSafe;

View File

@ -147,7 +147,6 @@ static ShmDescPtr ShmList = (ShmDescPtr) NULL;
static ShmDescPtr
shmalloc(unsigned int size)
{
ShmDescPtr pDesc;
int shmid;
char *addr;
@ -165,7 +164,7 @@ shmalloc(unsigned int size)
if (size < 3500)
return (ShmDescPtr) NULL;
pDesc = malloc(sizeof(ShmDescRec));
ShmDescPtr pDesc = calloc(1, sizeof(ShmDescRec));
if (!pDesc)
return (ShmDescPtr) NULL;

View File

@ -100,7 +100,7 @@ typedef struct {
static void *
AddFragment(struct xorg_list *frags, int bytes)
{
FragmentList *f = malloc(sizeof(FragmentList) + bytes);
FragmentList *f = calloc(1, sizeof(FragmentList) + bytes);
if (!f) {
return NULL;
} else {

View File

@ -55,7 +55,7 @@ int selinuxEnforcingState = SELINUX_MODE_DEFAULT;
static char *
SELinuxCopyContext(char *ptr, unsigned len)
{
char *copy = malloc(len + 1);
char *copy = calloc(1, len + 1);
if (!copy)
return NULL;

View File

@ -992,7 +992,7 @@ ProcXvQueryImageAttributes(ClientPtr client)
num_planes = pImage->num_planes;
if (!(offsets = malloc(num_planes << 3)))
if (!(offsets = calloc(1, num_planes << 3)))
return BadAlloc;
pitches = offsets + num_planes;
@ -1745,7 +1745,7 @@ XineramifyXv(void)
/* now create a resource for each port */
for (j = 0; j < refAdapt->nPorts; j++) {
PanoramiXRes *port = malloc(sizeof(PanoramiXRes));
PanoramiXRes *port = calloc(1, sizeof(PanoramiXRes));
if (!port)
break;

View File

@ -270,8 +270,6 @@ static void XvPixmapDestroy(CallbackListPtr *pcbl, ScreenPtr pScreen, PixmapPtr
int
XvScreenInit(ScreenPtr pScreen)
{
XvScreenPtr pxvs;
if (XvScreenGeneration != serverGeneration) {
if (!CreateResourceTypes()) {
ErrorF("XvScreenInit: Unable to allocate resource types\n");
@ -292,7 +290,7 @@ XvScreenInit(ScreenPtr pScreen)
/* ALLOCATE SCREEN PRIVATE RECORD */
pxvs = malloc(sizeof(XvScreenRec));
XvScreenPtr pxvs = calloc(1, sizeof(XvScreenRec));
if (!pxvs) {
ErrorF("XvScreenInit: Unable to allocate screen private structure\n");
return BadAlloc;
@ -777,7 +775,7 @@ XvdiSelectVideoNotify(ClientPtr client, DrawablePtr pDraw, BOOL onoff)
WILL BE DELETED WHEN THE DRAWABLE IS DESTROYED */
if (!pn) {
if (!(tpn = malloc(sizeof(XvVideoNotifyRec))))
if (!(tpn = calloc(1, sizeof(XvVideoNotifyRec))))
return BadAlloc;
tpn->next = NULL;
tpn->client = NULL;
@ -813,7 +811,7 @@ XvdiSelectVideoNotify(ClientPtr client, DrawablePtr pDraw, BOOL onoff)
tpn = fpn;
}
else {
if (!(tpn = malloc(sizeof(XvVideoNotifyRec))))
if (!(tpn = calloc(1, sizeof(XvVideoNotifyRec))))
return BadAlloc;
tpn->next = pn->next;
pn->next = tpn;
@ -867,7 +865,7 @@ XvdiSelectPortNotify(ClientPtr client, XvPortPtr pPort, BOOL onoff)
CREATE A NEW ONE AND ADD IT TO THE BEGINNING OF THE LIST */
if (!tpn) {
if (!(tpn = malloc(sizeof(XvPortNotifyRec))))
if (!(tpn = calloc(1, sizeof(XvPortNotifyRec))))
return BadAlloc;
tpn->next = pPort->pNotify;
pPort->pNotify = tpn;

View File

@ -233,7 +233,7 @@ ProcXvMCCreateContext(ClientPtr client)
(stuff->height > surface->max_height))
return BadValue;
if (!(pContext = malloc(sizeof(XvMCContextRec)))) {
if (!(pContext = calloc(1, sizeof(XvMCContextRec)))) {
return BadAlloc;
}
@ -315,7 +315,7 @@ ProcXvMCCreateSurface(ClientPtr client)
pScreenPriv = XVMC_GET_PRIVATE(pContext->pScreen);
if (!(pSurface = malloc(sizeof(XvMCSurfaceRec))))
if (!(pSurface = calloc(1, sizeof(XvMCSurfaceRec))))
return BadAlloc;
pSurface->surface_id = stuff->surface_id;
@ -428,7 +428,7 @@ ProcXvMCCreateSubpicture(ClientPtr client)
(stuff->height > surface->subpicture_max_height))
return BadValue;
if (!(pSubpicture = malloc(sizeof(XvMCSubpictureRec))))
if (!(pSubpicture = calloc(1, sizeof(XvMCSubpictureRec))))
return BadAlloc;
pSubpicture->subpicture_id = stuff->subpicture_id;
@ -749,7 +749,7 @@ XvMCScreenInit(ScreenPtr pScreen, int num, XvMCAdaptorPtr pAdapt)
if (!dixRegisterPrivateKey(&XvMCScreenKeyRec, PRIVATE_SCREEN, 0))
return BadAlloc;
if (!(pScreenPriv = malloc(sizeof(XvMCScreenRec))))
if (!(pScreenPriv = calloc(1, sizeof(XvMCScreenRec))))
return BadAlloc;
dixSetPrivate(&pScreen->devPrivates, XvMCScreenKey, pScreenPriv);