From 0127d6ef135ebbad859365eb34b8b3409c1a23cc Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Thu, 10 Apr 2025 19:53:04 +0200 Subject: [PATCH] dix: 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 --- dix/atom.c | 4 +--- dix/colormap.c | 12 +++++------- dix/cursor.c | 4 ++-- dix/devices.c | 25 +++++++------------------ dix/dispatch.c | 17 ++++++++--------- dix/dixfonts.c | 35 ++++++++++++++++------------------- dix/dixutils.c | 16 +++++----------- dix/events.c | 5 ++--- dix/extension.c | 2 +- dix/gc.c | 16 ++++++---------- dix/getevents.c | 2 +- dix/grabs.c | 7 +++---- dix/privates.c | 8 +++----- dix/property.c | 9 ++++----- dix/region.c | 16 ++++++---------- dix/resource.c | 8 ++++---- dix/swaprep.c | 8 +++----- dix/window.c | 11 +++++------ 18 files changed, 82 insertions(+), 123 deletions(-) diff --git a/dix/atom.c b/dix/atom.c index 1e6524c92..02d68d934 100644 --- a/dix/atom.c +++ b/dix/atom.c @@ -101,9 +101,7 @@ MakeAtom(const char *string, unsigned len, Bool makeit) } } if (makeit) { - NodePtr nd; - - nd = malloc(sizeof(NodeRec)); + NodePtr nd = calloc(1, sizeof(NodeRec)); if (!nd) return BAD_RESOURCE; if (lastAtom < XA_LAST_PREDEFINED) { diff --git a/dix/colormap.c b/dix/colormap.c index 54058541b..a13538889 100644 --- a/dix/colormap.c +++ b/dix/colormap.c @@ -262,7 +262,7 @@ CreateColormap(Colormap mid, ScreenPtr pScreen, VisualPtr pVisual, sizebytes *= 3; sizebytes += sizeof(ColormapRec); if (mid == pScreen->defColormap) { - pmap = malloc(sizebytes); + pmap = calloc(1, sizebytes); if (!pmap) return BadAlloc; if (!dixAllocatePrivates(&pmap->devPrivates, PRIVATE_COLORMAP)) { @@ -1083,9 +1083,7 @@ AllocColor(ColormapPtr pmap, * should be freed when the client dies */ if ((pmap->numPixelsRed[client] == 1) && (CLIENT_ID(pmap->mid) != client) && !(pmap->flags & CM_BeingCreated)) { - colorResource *pcr; - - pcr = malloc(sizeof(colorResource)); + colorResource *pcr = calloc(1, sizeof(colorResource)); if (!pcr) { (void) FreeColors(pmap, client, 1, pPix, (Pixel) 0); return BadAlloc; @@ -1503,7 +1501,7 @@ AllocColorCells(int client, ColormapPtr pmap, int colors, int planes, if (pmap->class == DirectColor) oldcount += pmap->numPixelsGreen[client] + pmap->numPixelsBlue[client]; if (!oldcount && (CLIENT_ID(pmap->mid) != client)) { - pcr = malloc(sizeof(colorResource)); + pcr = calloc(1, sizeof(colorResource)); if (!pcr) return BadAlloc; } @@ -1570,7 +1568,7 @@ AllocColorPlanes(int client, ColormapPtr pmap, int colors, if (class == DirectColor) oldcount += pmap->numPixelsGreen[client] + pmap->numPixelsBlue[client]; if (!oldcount && (CLIENT_ID(pmap->mid) != client)) { - pcr = malloc(sizeof(colorResource)); + pcr = calloc(1, sizeof(colorResource)); if (!pcr) return BadAlloc; } @@ -1973,7 +1971,7 @@ AllocShared(ColormapPtr pmap, Pixel * ppix, int c, int r, int g, int b, return FALSE; ppshared = psharedList; for (z = npixShared; --z >= 0;) { - if (!(ppshared[z] = malloc(sizeof(SHAREDCOLOR)))) { + if (!(ppshared[z] = calloc(1, sizeof(SHAREDCOLOR)))) { for (z++; z < npixShared; z++) free(ppshared[z]); free(psharedList); diff --git a/dix/cursor.c b/dix/cursor.c index 8d8bfeb8c..164ac9102 100644 --- a/dix/cursor.c +++ b/dix/cursor.c @@ -376,7 +376,7 @@ AllocGlyphCursor(Font source, unsigned sourceChar, Font mask, unsigned maskChar, unsigned char *mskptr; n = BitmapBytePad(cm.width) * (long) cm.height; - mskptr = mskbits = malloc(n); + mskptr = mskbits = calloc(1, n); if (!mskptr) return BadAlloc; while (--n >= 0) @@ -427,7 +427,7 @@ AllocGlyphCursor(Font source, unsigned sourceChar, Font mask, unsigned maskChar, bits->refcnt = -1; else { bits->refcnt = 1; - pShare = malloc(sizeof(GlyphShare)); + pShare = calloc(1, sizeof(GlyphShare)); if (!pShare) { FreeCursorBits(bits); return BadAlloc; diff --git a/dix/devices.c b/dix/devices.c index 434bd317b..b856ca5f8 100644 --- a/dix/devices.c +++ b/dix/devices.c @@ -1202,7 +1202,7 @@ CloseOneDevice(const DeviceIntPtr dev, DeviceIntPtr *listHead) * the removal of the device. * * No PresenceNotify is sent for device that the client never saw. This can - * happen if a malloc fails during the addition of master devices. If + * happen if a calloc fails during the addition of master devices. If * dev->init is FALSE it means the client never received a DeviceAdded event, * so let's not send a DeviceRemoved event either. * @@ -1475,12 +1475,10 @@ InitPointerAccelerationScheme(DeviceIntPtr dev, int scheme) Bool InitFocusClassDeviceStruct(DeviceIntPtr dev) { - FocusClassPtr focc; - BUG_RETURN_VAL(dev == NULL, FALSE); BUG_RETURN_VAL(dev->focus != NULL, FALSE); - focc = malloc(sizeof(FocusClassRec)); + FocusClassPtr focc = calloc(1, sizeof(FocusClassRec)); if (!focc) return FALSE; UpdateCurrentTimeIf(); @@ -1498,11 +1496,9 @@ InitFocusClassDeviceStruct(DeviceIntPtr dev) Bool InitPtrFeedbackClassDeviceStruct(DeviceIntPtr dev, PtrCtrlProcPtr controlProc) { - PtrFeedbackPtr feedc; - BUG_RETURN_VAL(dev == NULL, FALSE); - feedc = malloc(sizeof(PtrFeedbackClassRec)); + PtrFeedbackPtr feedc = calloc(1, sizeof(PtrFeedbackClassRec)); if (!feedc) return FALSE; feedc->CtrlProc = controlProc; @@ -1541,11 +1537,10 @@ InitStringFeedbackClassDeviceStruct(DeviceIntPtr dev, KeySym * symbols) { int i; - StringFeedbackPtr feedc; BUG_RETURN_VAL(dev == NULL, FALSE); - feedc = malloc(sizeof(StringFeedbackClassRec)); + StringFeedbackPtr feedc = calloc(1, sizeof(StringFeedbackClassRec)); if (!feedc) return FALSE; feedc->CtrlProc = controlProc; @@ -1577,11 +1572,9 @@ Bool InitBellFeedbackClassDeviceStruct(DeviceIntPtr dev, BellProcPtr bellProc, BellCtrlProcPtr controlProc) { - BellFeedbackPtr feedc; - BUG_RETURN_VAL(dev == NULL, FALSE); - feedc = malloc(sizeof(BellFeedbackClassRec)); + BellFeedbackPtr feedc = calloc(1, sizeof(BellFeedbackClassRec)); if (!feedc) return FALSE; feedc->CtrlProc = controlProc; @@ -1598,11 +1591,9 @@ InitBellFeedbackClassDeviceStruct(DeviceIntPtr dev, BellProcPtr bellProc, Bool InitLedFeedbackClassDeviceStruct(DeviceIntPtr dev, LedCtrlProcPtr controlProc) { - LedFeedbackPtr feedc; - BUG_RETURN_VAL(dev == NULL, FALSE); - feedc = malloc(sizeof(LedFeedbackClassRec)); + LedFeedbackPtr feedc = calloc(1, sizeof(LedFeedbackClassRec)); if (!feedc) return FALSE; feedc->CtrlProc = controlProc; @@ -1620,11 +1611,9 @@ Bool InitIntegerFeedbackClassDeviceStruct(DeviceIntPtr dev, IntegerCtrlProcPtr controlProc) { - IntegerFeedbackPtr feedc; - BUG_RETURN_VAL(dev == NULL, FALSE); - feedc = malloc(sizeof(IntegerFeedbackClassRec)); + IntegerFeedbackPtr feedc = calloc(1, sizeof(IntegerFeedbackClassRec)); if (!feedc) return FALSE; feedc->CtrlProc = controlProc; diff --git a/dix/dispatch.c b/dix/dispatch.c index 2c46500ed..ff5f510e9 100644 --- a/dix/dispatch.c +++ b/dix/dispatch.c @@ -628,7 +628,7 @@ CreateConnectionBlock(void) pad_to_int32(setup.nbytesVendor) + (setup.numFormats * sizeof(xPixmapFormat)) + (setup.numRoots * sizeof(xWindowRoot)); - ConnectionInfo = malloc(lenofblock); + ConnectionInfo = calloc(1, lenofblock); if (!ConnectionInfo) return FALSE; @@ -2559,7 +2559,6 @@ ProcUninstallColormap(ClientPtr client) int ProcListInstalledColormaps(ClientPtr client) { - xListInstalledColormapsReply *preply; int nummaps, rc; WindowPtr pWin; @@ -2574,7 +2573,8 @@ ProcListInstalledColormaps(ClientPtr client) if (rc != Success) return rc; - preply = malloc(sizeof(xListInstalledColormapsReply) + + xListInstalledColormapsReply *preply = calloc(1, + sizeof(xListInstalledColormapsReply) + pWin->drawable.pScreen->maxInstalledCmaps * sizeof(Colormap)); if (!preply) @@ -2689,7 +2689,7 @@ ProcAllocColorCells(ClientPtr client) if (rc == Success) { int npixels, nmasks; long length; - Pixel *ppixels, *pmasks; + Pixel *pmasks; npixels = stuff->colors; if (!npixels) { @@ -2702,7 +2702,7 @@ ProcAllocColorCells(ClientPtr client) } nmasks = stuff->planes; length = ((long) npixels + (long) nmasks) * sizeof(Pixel); - ppixels = malloc(length); + Pixel *ppixels = calloc(1, length); if (!ppixels) return BadAlloc; pmasks = ppixels + npixels; @@ -2751,7 +2751,6 @@ ProcAllocColorPlanes(ClientPtr client) xAllocColorPlanesReply acpr; int npixels; long length; - Pixel *ppixels; npixels = stuff->colors; if (!npixels) { @@ -2769,7 +2768,7 @@ ProcAllocColorPlanes(ClientPtr client) }; length = (long) npixels *sizeof(Pixel); - ppixels = malloc(length); + Pixel *ppixels = calloc(1, length); if (!ppixels) return BadAlloc; if ((rc = AllocColorPlanes(client->index, pcmp, npixels, @@ -2981,7 +2980,6 @@ ProcCreateCursor(ClientPtr client) PixmapPtr src; PixmapPtr msk; unsigned char *srcbits; - unsigned char *mskbits; unsigned short width, height; long n; CursorMetricRec cm; @@ -3029,7 +3027,8 @@ ProcCreateCursor(ClientPtr client) if (!srcbits) return BadAlloc; n = BitmapBytePad(width) * height; - mskbits = malloc(n); + + unsigned char *mskbits = calloc(1, n); if (!mskbits) { free(srcbits); return BadAlloc; diff --git a/dix/dixfonts.c b/dix/dixfonts.c index b35ba75d2..25c5ad0ce 100644 --- a/dix/dixfonts.c +++ b/dix/dixfonts.c @@ -379,7 +379,6 @@ int OpenFont(ClientPtr client, XID fid, Mask flags, unsigned lenfname, const char *pfontname) { - OFclosurePtr c; int i; FontPtr cached = (FontPtr) 0; @@ -412,10 +411,10 @@ OpenFont(ClientPtr client, XID fid, Mask flags, unsigned lenfname, return Success; } } - c = malloc(sizeof(OFclosureRec)); + OFclosurePtr c = calloc(1, sizeof(OFclosureRec)); if (!c) return BadAlloc; - c->fontname = malloc(lenfname); + c->fontname = calloc(1, lenfname); c->origFontName = pfontname; c->origFontNameLen = lenfname; if (!c->fontname) { @@ -644,7 +643,7 @@ doListFontsAndAliases(ClientPtr client, LFclosurePtr c) } if (err == FontNameAlias) { free(resolved); - resolved = malloc(resolvedlen + 1); + resolved = calloc(1, resolvedlen + 1); if (resolved) memcpy(resolved, tmpname, resolvedlen + 1); } @@ -694,7 +693,7 @@ doListFontsAndAliases(ClientPtr client, LFclosurePtr c) c->saved = c->current; c->haveSaved = TRUE; free(c->savedName); - c->savedName = malloc(namelen + 1); + c->savedName = calloc(1, namelen + 1); if (c->savedName) memcpy(c->savedName, name, namelen + 1); c->savedNameLen = namelen; @@ -756,7 +755,7 @@ doListFontsAndAliases(ClientPtr client, LFclosurePtr c) .sequenceNumber = client->sequence }; - bufptr = bufferStart = malloc(reply.length << 2); + bufptr = bufferStart = calloc(1, reply.length << 2); if (!bufptr && reply.length) { SendErrorToClient(client, X_ListFonts, 0, 0, BadAlloc); @@ -814,7 +813,7 @@ ListFonts(ClientPtr client, unsigned char *pattern, unsigned length, if (i != Success) return i; - if (!(c = malloc(sizeof *c))) + if (!(c = calloc(1, sizeof *c))) return BadAlloc; c->fpe_list = xallocarray(num_fpes, sizeof(FontPathElementPtr)); if (!c->fpe_list) { @@ -934,7 +933,7 @@ doListFontsWithInfo(ClientPtr client, LFWIclosurePtr c) c->haveSaved = TRUE; c->savedNumFonts = numFonts; free(c->savedName); - c->savedName = malloc(namelen + 1); + c->savedName = calloc(1, namelen + 1); if (c->savedName) memcpy(c->savedName, name, namelen + 1); aliascount = 20; @@ -1060,7 +1059,7 @@ StartListFontsWithInfo(ClientPtr client, int length, unsigned char *pattern, if (i != Success) return i; - if (!(c = malloc(sizeof *c))) + if (!(c = calloc(1, sizeof *c))) goto badAlloc; c->fpe_list = xallocarray(num_fpes, sizeof(FontPathElementPtr)); if (!c->fpe_list) { @@ -1219,7 +1218,7 @@ doPolyText(ClientPtr client, PTclosurePtr c) /* We're putting the client to sleep. We need to do a few things to ensure successful and atomic-appearing execution of the remainder of the request. First, copy the remainder of the - request into a safe malloc'd area. Second, create a scratch GC + request into a safe calloc'd area. Second, create a scratch GC to use for the remainder of the request. Third, mark all fonts referenced in the remainder of the request to prevent their deallocation. Fourth, make the original GC look like the @@ -1231,9 +1230,9 @@ doPolyText(ClientPtr client, PTclosurePtr c) indicated by client_state = START_SLEEP. */ /* Step 1 */ - /* Allocate a malloc'd closure structure to replace + /* Allocate a calloc'd closure structure to replace the local one we were passed */ - new_closure = malloc(sizeof(PTclosureRec)); + new_closure = calloc(1, sizeof(PTclosureRec)); if (!new_closure) { err = BadAlloc; goto bail; @@ -1241,7 +1240,7 @@ doPolyText(ClientPtr client, PTclosurePtr c) *new_closure = *c; len = new_closure->endReq - new_closure->pElt; - new_closure->data = malloc(len); + new_closure->data = calloc(1, len); if (!new_closure->data) { free(new_closure); err = BadAlloc; @@ -1415,7 +1414,6 @@ doImageText(ClientPtr client, ITclosurePtr c) if (!ClientIsAsleep(client)) { GC *pGC; unsigned char *data; - ITclosurePtr new_closure; ITclosurePtr old_closure; /* We're putting the client to sleep. We need to @@ -1423,7 +1421,7 @@ doImageText(ClientPtr client, ITclosurePtr c) in doPolyText, but much simpler because the request structure is much simpler. */ - new_closure = malloc(sizeof(ITclosureRec)); + ITclosurePtr new_closure = calloc(1, sizeof(ITclosureRec)); if (!new_closure) { err = BadAlloc; goto bail; @@ -1624,13 +1622,12 @@ SetFontPathElements(int npaths, unsigned char *paths, int *bad, Bool persist) } /* if error or can't do it, act like it's a new one */ if (!fpe) { - char *name; - fpe = malloc(sizeof(FontPathElementRec)); + fpe = calloc(1, sizeof(FontPathElementRec)); if (!fpe) { err = BadAlloc; goto bail; } - name = malloc(len + 1); + char *name = calloc(1, len + 1); if (!name) { free(fpe); err = BadAlloc; @@ -1737,7 +1734,7 @@ SetDefaultFontPath(const char *path) /* get enough for string, plus values -- use up commas */ len = strlen(temp_path) + 1; - nump = cp = newpath = malloc(len); + nump = cp = newpath = calloc(1, len); if (!newpath) { free(temp_path); return BadAlloc; diff --git a/dix/dixutils.c b/dix/dixutils.c index b3708cec9..a5cff7553 100644 --- a/dix/dixutils.c +++ b/dix/dixutils.c @@ -531,9 +531,7 @@ Bool QueueWorkProc(Bool (*function) (ClientPtr pClient, void *closure), ClientPtr client, void *closure) { - WorkQueuePtr q; - - q = malloc(sizeof *q); + WorkQueuePtr q = calloc(1, sizeof *q); if (!q) return FALSE; q->function = function; @@ -565,9 +563,7 @@ static SleepQueuePtr sleepQueue = NULL; Bool ClientSleep(ClientPtr client, ClientSleepProcPtr function, void *closure) { - SleepQueuePtr q; - - q = malloc(sizeof *q); + SleepQueuePtr q = calloc(1, sizeof *q); if (!q) return FALSE; @@ -653,9 +649,7 @@ static CallbackListPtr **listsToCleanup = NULL; static Bool _AddCallback(CallbackListPtr *pcbl, CallbackProcPtr callback, void *data) { - CallbackPtr cbr; - - cbr = malloc(sizeof(CallbackRec)); + CallbackPtr cbr = calloc(1, sizeof(CallbackRec)); if (!cbr) return FALSE; cbr->proc = callback; @@ -774,12 +768,12 @@ void DeleteCallbackList(CallbackListPtr *pcbl) static Bool CreateCallbackList(CallbackListPtr *pcbl) { - CallbackListPtr cbl; int i; if (!pcbl) return FALSE; - cbl = malloc(sizeof(CallbackListRec)); + + CallbackListPtr cbl = calloc(1, sizeof(CallbackListRec)); if (!cbl) return FALSE; cbl->inCallback = 0; diff --git a/dix/events.c b/dix/events.c index 585f84c1f..149f8cbe6 100644 --- a/dix/events.c +++ b/dix/events.c @@ -1143,7 +1143,6 @@ void EnqueueEvent(InternalEvent *ev, DeviceIntPtr device) { QdEventPtr tail = NULL; - QdEventPtr qe; SpritePtr pSprite = device->spriteInfo->sprite; int eventlen; DeviceEvent *event = &ev->device_event; @@ -1204,7 +1203,7 @@ EnqueueEvent(InternalEvent *ev, DeviceIntPtr device) eventlen = sizeof(InternalEvent); - qe = malloc(sizeof(QdEventRec) + eventlen); + QdEventPtr qe = calloc(1, sizeof(QdEventRec) + eventlen); if (!qe) return; xorg_list_init(&qe->next); @@ -4588,7 +4587,7 @@ XRetCode EventSelectForWindow(WindowPtr pWin, ClientPtr client, Mask mask) check = 0; if (!pWin->optional && !MakeWindowOptional(pWin)) return BadAlloc; - others = malloc(sizeof(OtherClients)); + others = calloc(1, sizeof(OtherClients)); if (!others) return BadAlloc; others->mask = mask; diff --git a/dix/extension.c b/dix/extension.c index 13efd45a4..ecfd3a74a 100644 --- a/dix/extension.c +++ b/dix/extension.c @@ -284,7 +284,7 @@ ProcListExtensions(ClientPtr client) reply.nExtensions += 1; } reply.length = bytes_to_int32(total_length); - buffer = bufptr = malloc(total_length); + buffer = bufptr = calloc(1, total_length); if (!buffer) return BadAlloc; for (i = 0; i < NumExtensions; i++) { diff --git a/dix/gc.c b/dix/gc.c index 03c68a22e..0ea8009fa 100644 --- a/dix/gc.c +++ b/dix/gc.c @@ -361,9 +361,7 @@ ChangeGC(ClientPtr client, GC * pGC, BITS32 mask, ChangeGCValPtr pUnion) } } else if (newdash != 0) { - unsigned char *dash; - - dash = malloc(2 * sizeof(unsigned char)); + unsigned char *dash = calloc(2, sizeof(unsigned char)); if (dash) { if (pGC->dash != DefaultDash) free(pGC->dash); @@ -726,10 +724,8 @@ CopyGC(GC * pgcSrc, GC * pgcDst, BITS32 mask) } } else { - unsigned char *dash; unsigned int i; - - dash = malloc(pgcSrc->numInDashList * sizeof(unsigned char)); + unsigned char *dash = calloc(pgcSrc->numInDashList, sizeof(unsigned char)); if (dash) { if (pgcDst->dash != DefaultDash) free(pgcDst->dash); @@ -920,9 +916,9 @@ SetDashes(GCPtr pGC, unsigned offset, unsigned ndash, unsigned char *pdash) } if (ndash & 1) - p = malloc(2 * ndash * sizeof(unsigned char)); + p = calloc(2 * ndash, sizeof(unsigned char)); else - p = malloc(ndash * sizeof(unsigned char)); + p = calloc(ndash, sizeof(unsigned char)); if (!p) return BadAlloc; @@ -1001,13 +997,13 @@ SetClipRects(GCPtr pGC, int xOrigin, int yOrigin, int nrects, xRectangle *prects, int ordering) { int newct, size; - xRectangle *prectsNew; newct = VerifyRectOrder(nrects, prects, ordering); if (newct < 0) return BadMatch; size = nrects * sizeof(xRectangle); - prectsNew = malloc(size); + + xRectangle *prectsNew = calloc(1, size); if (!prectsNew && size) return BadAlloc; diff --git a/dix/getevents.c b/dix/getevents.c index 01200faea..17c2fa149 100644 --- a/dix/getevents.c +++ b/dix/getevents.c @@ -432,7 +432,7 @@ GetMotionHistory(DeviceIntPtr pDev, xTimecoord ** buff, unsigned long start, else size = (sizeof(INT32) * pDev->valuator->numAxes) + sizeof(Time); - *buff = malloc(size * pDev->valuator->numMotionEvents); + *buff = calloc(size, pDev->valuator->numMotionEvents); if (!(*buff)) return 0; obuff = (char *) *buff; diff --git a/dix/grabs.c b/dix/grabs.c index bb13710a9..27f86a547 100644 --- a/dix/grabs.c +++ b/dix/grabs.c @@ -279,7 +279,7 @@ CopyGrab(GrabPtr dst, const GrabPtr src) if (src->modifiersDetail.pMask) { int len = MasksPerDetailMask * sizeof(Mask); - mdetails_mask = malloc(len); + mdetails_mask = calloc(1, len); if (!mdetails_mask) return FALSE; memcpy(mdetails_mask, src->modifiersDetail.pMask, len); @@ -288,7 +288,7 @@ CopyGrab(GrabPtr dst, const GrabPtr src) if (src->detail.pMask) { int len = MasksPerDetailMask * sizeof(Mask); - details_mask = malloc(len); + details_mask = calloc(1, len); if (!details_mask) { free(mdetails_mask); return FALSE; @@ -345,10 +345,9 @@ DeletePassiveGrab(void *value, XID id) static Mask * DeleteDetailFromMask(Mask *pDetailMask, unsigned int detail) { - Mask *mask; int i; - mask = malloc(sizeof(Mask) * MasksPerDetailMask); + Mask *mask = calloc(MasksPerDetailMask, sizeof(Mask)); if (mask) { if (pDetailMask) for (i = 0; i < MasksPerDetailMask; i++) diff --git a/dix/privates.c b/dix/privates.c index 62309d769..8f025e239 100644 --- a/dix/privates.c +++ b/dix/privates.c @@ -469,7 +469,6 @@ _dixAllocateObjectWithPrivates(unsigned baseSize, unsigned clear, unsigned offset, DevPrivateType type) { unsigned totalSize; - void *object; PrivatePtr privates; PrivatePtr *devPrivates; @@ -480,7 +479,7 @@ _dixAllocateObjectWithPrivates(unsigned baseSize, unsigned clear, /* round up so that void * is aligned */ baseSize = (baseSize + sizeof(void *) - 1) & ~(sizeof(void *) - 1); totalSize = baseSize + global_keys[type].offset; - object = malloc(totalSize); + void *object = calloc(1, totalSize); if (!object) return NULL; @@ -512,7 +511,7 @@ dixAllocatePrivates(PrivatePtr *privates, DevPrivateType type) p = NULL; } else { - if (!(p = malloc(size))) + if (!(p = calloc(1, size))) return FALSE; } @@ -692,7 +691,6 @@ _dixAllocateScreenObjectWithPrivates(ScreenPtr pScreen, DevPrivateType type) { unsigned totalSize; - void *object; PrivatePtr privates; PrivatePtr *devPrivates; int privates_size; @@ -708,7 +706,7 @@ _dixAllocateScreenObjectWithPrivates(ScreenPtr pScreen, /* round up so that pointer is aligned */ baseSize = (baseSize + sizeof(void *) - 1) & ~(sizeof(void *) - 1); totalSize = baseSize + privates_size; - object = malloc(totalSize); + void *object = calloc(1, totalSize); if (!object) return NULL; diff --git a/dix/property.c b/dix/property.c index 032b51ea2..d42b76ce2 100644 --- a/dix/property.c +++ b/dix/property.c @@ -262,7 +262,6 @@ dixChangeWindowProperty(ClientPtr pClient, WindowPtr pWin, Atom property, PropertyPtr pProp; PropertyRec savedProp; int sizeInBytes, totalSize, rc; - unsigned char *data; Mask access_mode; sizeInBytes = format >> 3; @@ -278,7 +277,7 @@ dixChangeWindowProperty(ClientPtr pClient, WindowPtr pWin, Atom property, pProp = dixAllocateObjectWithPrivates(PropertyRec, PRIVATE_PROPERTY); if (!pProp) return BadAlloc; - data = malloc(totalSize); + unsigned char *data = calloc(1, totalSize); if (totalSize) { if (!data) { dixFreeObjectWithPrivates(pProp, PRIVATE_PROPERTY); @@ -317,7 +316,7 @@ dixChangeWindowProperty(ClientPtr pClient, WindowPtr pWin, Atom property, savedProp = *pProp; if (mode == PropModeReplace) { - data = malloc(totalSize); + unsigned char *data = calloc(1, totalSize); if (totalSize) { if (!data) return BadAlloc; @@ -332,7 +331,7 @@ dixChangeWindowProperty(ClientPtr pClient, WindowPtr pWin, Atom property, /* do nothing */ } else if (mode == PropModeAppend) { - data = xallocarray(pProp->size + len, sizeInBytes); + unsigned char *data = calloc(pProp->size + len, sizeInBytes); if (!data) return BadAlloc; memcpy(data, pProp->data, pProp->size * sizeInBytes); @@ -341,7 +340,7 @@ dixChangeWindowProperty(ClientPtr pClient, WindowPtr pWin, Atom property, pProp->size += len; } else if (mode == PropModePrepend) { - data = xallocarray(len + pProp->size, sizeInBytes); + unsigned char *data = calloc(len + pProp->size, sizeInBytes); if (!data) return BadAlloc; memcpy(data + totalSize, pProp->data, pProp->size * sizeInBytes); diff --git a/dix/region.c b/dix/region.c index f45a56a9e..e7e105953 100644 --- a/dix/region.c +++ b/dix/region.c @@ -235,16 +235,14 @@ InitRegions(void) /***************************************************************** * RegionCreate(rect, size) - * This routine does a simple malloc to make a structure of + * This routine does a simple calloc to make a structure of * REGION of "size" number of rectangles. *****************************************************************/ RegionPtr RegionCreate(BoxPtr rect, int size) { - RegionPtr pReg; - - pReg = (RegionPtr) malloc(sizeof(RegionRec)); + RegionPtr pReg = calloc(1, sizeof(RegionRec)); if (!pReg) return &RegionBrokenRegion; @@ -356,7 +354,7 @@ RegionRectAlloc(RegionPtr pRgn, int n) if (!pRgn->data) { n++; rgnSize = RegionSizeof(n); - pRgn->data = (rgnSize > 0) ? malloc(rgnSize) : NULL; + pRgn->data = (rgnSize > 0) ? calloc(1, rgnSize) : NULL; if (!pRgn->data) return RegionBreak(pRgn); pRgn->data->numRects = 1; @@ -364,7 +362,7 @@ RegionRectAlloc(RegionPtr pRgn, int n) } else if (!pRgn->data->size) { rgnSize = RegionSizeof(n); - pRgn->data = (rgnSize > 0) ? malloc(rgnSize) : NULL; + pRgn->data = (rgnSize > 0) ? calloc(1, rgnSize) : NULL; if (!pRgn->data) return RegionBreak(pRgn); pRgn->data->numRects = 0; @@ -1150,7 +1148,6 @@ RegionValidate(RegionPtr badreg, Bool *pOverlap) } RegionInfo; int numRects; /* Original numRects for badreg */ - RegionInfo *ri; /* Array of current regions */ int numRI; /* Number of entries used in ri */ int sizeRI; /* Number of entries available in ri */ int i; /* Index into rects */ @@ -1193,7 +1190,7 @@ RegionValidate(RegionPtr badreg, Bool *pOverlap) /* Set up the first region to be the first rectangle in badreg */ /* Note that step 2 code will never overflow the ri[0].reg rects array */ - ri = (RegionInfo *) malloc(4 * sizeof(RegionInfo)); + RegionInfo *ri = calloc(4, sizeof(RegionInfo)); if (!ri) return RegionBreak(badreg); sizeRI = 4; @@ -1324,7 +1321,6 @@ RegionFromRects(int nrects, xRectangle *prect, int ctype) RegionPtr pRgn; size_t rgnSize; - RegDataPtr pData; BoxPtr pBox; int i; int x1, y1, x2, y2; @@ -1351,7 +1347,7 @@ RegionFromRects(int nrects, xRectangle *prect, int ctype) return pRgn; } rgnSize = RegionSizeof(nrects); - pData = (rgnSize > 0) ? malloc(rgnSize) : NULL; + RegDataPtr pData = (rgnSize > 0) ? calloc(1, rgnSize) : NULL; if (!pData) { RegionBreak(pRgn); return pRgn; diff --git a/dix/resource.c b/dix/resource.c index 242db7443..3b3453b17 100644 --- a/dix/resource.c +++ b/dix/resource.c @@ -650,13 +650,13 @@ InitClientResources(ClientPtr client) lastResourceClass = RC_LASTPREDEF; TypeMask = RC_LASTPREDEF - 1; free(resourceTypes); - resourceTypes = malloc(sizeof(predefTypes)); + resourceTypes = calloc(1, sizeof(predefTypes)); if (!resourceTypes) return FALSE; memcpy(resourceTypes, predefTypes, sizeof(predefTypes)); } clientTable[i = client->index].resources = - malloc(INITBUCKETS * sizeof(ResourcePtr)); + calloc(INITBUCKETS, sizeof(ResourcePtr)); if (!clientTable[i].resources) return FALSE; clientTable[i].buckets = INITBUCKETS; @@ -809,7 +809,7 @@ AddResource(XID id, RESTYPE type, void *value) { int client; ClientResourceRec *rrec; - ResourcePtr res, *head; + ResourcePtr *head; #ifdef XSERVER_DTRACE XSERVER_RESOURCE_ALLOC(id, type, value, TypeNameString(type)); @@ -824,7 +824,7 @@ AddResource(XID id, RESTYPE type, void *value) if ((rrec->elements >= 4 * rrec->buckets) && (rrec->hashsize < MAXHASHSIZE)) RebuildTable(client); head = &rrec->resources[HashResourceID(id, clientTable[client].hashsize)]; - res = malloc(sizeof(ResourceRec)); + ResourcePtr res = calloc(1, sizeof(ResourceRec)); if (!res) { (*resourceTypes[type & TypeMask].deleteFunc) (value, id); return FALSE; diff --git a/dix/swaprep.c b/dix/swaprep.c index b9afa14a2..2f9c636d3 100644 --- a/dix/swaprep.c +++ b/dix/swaprep.c @@ -94,7 +94,7 @@ CopySwap32Write(ClientPtr pClient, int size, CARD32 *pbuf) CARD32 tmpbuf[1]; /* Allocate as big a buffer as we can... */ - while (!(pbufT = malloc(bufsize))) { + while (!(pbufT = calloc(1, bufsize))) { bufsize >>= 1; if (bufsize == 4) { pbufT = tmpbuf; @@ -141,7 +141,7 @@ CopySwap16Write(ClientPtr pClient, int size, short *pbuf) short tmpbuf[2]; /* Allocate as big a buffer as we can... */ - while (!(pbufT = malloc(bufsize))) { + while (!(pbufT = calloc(1, bufsize))) { bufsize >>= 1; if (bufsize == 4) { pbufT = tmpbuf; @@ -1130,9 +1130,7 @@ SwapConnSetupInfo(char *pInfo, char *pInfoT) void _X_COLD WriteSConnectionInfo(ClientPtr pClient, unsigned long size, char *pInfo) { - char *pInfoTBase; - - pInfoTBase = malloc(size); + char *pInfoTBase = calloc(1, size); if (!pInfoTBase) { pClient->noClientException = -1; return; diff --git a/dix/window.c b/dix/window.c index 4ac0d4cb1..51b616c25 100644 --- a/dix/window.c +++ b/dix/window.c @@ -581,7 +581,7 @@ CreateRootWindow(ScreenPtr pScreen) pWin->parent = NullWindow; SetWindowToDefaults(pWin); - pWin->optional = malloc(sizeof(WindowOptRec)); + pWin->optional = calloc(1, sizeof(WindowOptRec)); if (!pWin->optional) return FALSE; @@ -3197,7 +3197,6 @@ TileScreenSaver(ScreenPtr pScreen, int kind) Mask mask; WindowPtr pWin; CursorMetricRec cm; - unsigned char *srcbits, *mskbits; CursorPtr cursor; XID cursorID = 0; int attri; @@ -3235,8 +3234,8 @@ TileScreenSaver(ScreenPtr pScreen, int kind) cm.height = 16; cm.xhot = 8; cm.yhot = 8; - srcbits = malloc(BitmapBytePad(32) * 16); - mskbits = malloc(BitmapBytePad(32) * 16); + unsigned char *srcbits = calloc(16, BitmapBytePad(32)); + unsigned char *mskbits = calloc(16, BitmapBytePad(32)); if (!srcbits || !mskbits) { free(srcbits); free(mskbits); @@ -3374,12 +3373,12 @@ CheckWindowOptionalNeed(WindowPtr w) Bool MakeWindowOptional(WindowPtr pWin) { - WindowOptPtr optional; WindowOptPtr parentOptional; if (pWin->optional) return TRUE; - optional = malloc(sizeof(WindowOptRec)); + + WindowOptPtr optional = calloc(1, sizeof(WindowOptRec)); if (!optional) return FALSE; optional->dontPropagateMask = DontPropagateMasks[pWin->dontPropagate];