From c96901a85a08da1f379dba2c6303060eee1032ea Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Thu, 10 Apr 2025 18:43:08 +0200 Subject: [PATCH] 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 --- Xext/dpms.c | 5 ++--- Xext/hashtable.c | 4 ++-- Xext/panoramiX.c | 5 ++--- Xext/panoramiXprocs.c | 10 +++++----- Xext/saver.c | 2 +- Xext/security.c | 8 +++----- Xext/shape.c | 6 +++--- Xext/shm.c | 11 +++++------ Xext/sleepuntil.c | 5 +++-- Xext/sync.c | 17 +++++++---------- Xext/vidmode.c | 6 ++---- Xext/xace.c | 3 +-- Xext/xf86bigfont.c | 3 +-- Xext/xres.c | 2 +- Xext/xselinux_ext.c | 2 +- Xext/xvdisp.c | 4 ++-- Xext/xvmain.c | 10 ++++------ Xext/xvmc.c | 8 ++++---- 18 files changed, 49 insertions(+), 62 deletions(-) diff --git a/Xext/dpms.c b/Xext/dpms.c index 7839cd2a1..678ba6944 100644 --- a/Xext/dpms.c +++ b/Xext/dpms.c @@ -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); diff --git a/Xext/hashtable.c b/Xext/hashtable.c index 13db19f9d..51f362c9a 100644 --- a/Xext/hashtable.c +++ b/Xext/hashtable.c @@ -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; } diff --git a/Xext/panoramiX.c b/Xext/panoramiX.c index 24b62bb47..048b22a29 100644 --- a/Xext/panoramiX.c +++ b/Xext/panoramiX.c @@ -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) { diff --git a/Xext/panoramiXprocs.c b/Xext/panoramiXprocs.c index 7ceb4d84a..b72005969 100644 --- a/Xext/panoramiXprocs.c +++ b/Xext/panoramiXprocs.c @@ -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; diff --git a/Xext/saver.c b/Xext/saver.c index a91bc0aed..0085d5665 100644 --- a/Xext/saver.c +++ b/Xext/saver.c @@ -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; diff --git a/Xext/security.c b/Xext/security.c index b0715e3df..54bd4efab 100644 --- a/Xext/security.c +++ b/Xext/security.c @@ -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; diff --git a/Xext/shape.c b/Xext/shape.c index 8b8615828..a463f9741 100644 --- a/Xext/shape.c +++ b/Xext/shape.c @@ -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) { diff --git a/Xext/shm.c b/Xext/shm.c index 39ff0ca48..3028a9517 100644 --- a/Xext/shm.c +++ b/Xext/shm.c @@ -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; diff --git a/Xext/sleepuntil.c b/Xext/sleepuntil.c index 0fbfab10a..24b42c9ae 100644 --- a/Xext/sleepuntil.c +++ b/Xext/sleepuntil.c @@ -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; diff --git a/Xext/sync.c b/Xext/sync.c index d5aff6b0d..60e1e5ecf 100644 --- a/Xext/sync.c +++ b/Xext/sync.c @@ -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; } diff --git a/Xext/vidmode.c b/Xext/vidmode.c index ff6f954fa..067326614 100644 --- a/Xext/vidmode.c +++ b/Xext/vidmode.c @@ -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); diff --git a/Xext/xace.c b/Xext/xace.c index 2ceea394d..d9472e8cd 100644 --- a/Xext/xace.c +++ b/Xext/xace.c @@ -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; diff --git a/Xext/xf86bigfont.c b/Xext/xf86bigfont.c index 6dfa811e6..9adc49829 100644 --- a/Xext/xf86bigfont.c +++ b/Xext/xf86bigfont.c @@ -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; diff --git a/Xext/xres.c b/Xext/xres.c index d238511e8..bd7580d2b 100644 --- a/Xext/xres.c +++ b/Xext/xres.c @@ -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 { diff --git a/Xext/xselinux_ext.c b/Xext/xselinux_ext.c index 5fe7fdc86..f1248ea77 100644 --- a/Xext/xselinux_ext.c +++ b/Xext/xselinux_ext.c @@ -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; diff --git a/Xext/xvdisp.c b/Xext/xvdisp.c index 7c9ab7089..9d3f7ddec 100644 --- a/Xext/xvdisp.c +++ b/Xext/xvdisp.c @@ -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; diff --git a/Xext/xvmain.c b/Xext/xvmain.c index 488ee70f1..818452998 100644 --- a/Xext/xvmain.c +++ b/Xext/xvmain.c @@ -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; diff --git a/Xext/xvmc.c b/Xext/xvmc.c index aff4ae99a..b612daacc 100644 --- a/Xext/xvmc.c +++ b/Xext/xvmc.c @@ -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);