From 8c05f4db0aedbb51765ed5a7052342a9c2b83939 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Thu, 10 Apr 2025 19:21:40 +0200 Subject: [PATCH] mi: 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 --- mi/miarc.c | 13 +++++-------- mi/micmap.c | 4 ++-- mi/mifillarc.c | 2 +- mi/mioverlay.c | 2 +- mi/mipointer.c | 6 ++---- mi/mipoly.c | 4 ++-- mi/mipushpxl.c | 3 +-- mi/miscrinit.c | 4 +--- mi/misprite.c | 3 +-- 9 files changed, 16 insertions(+), 25 deletions(-) diff --git a/mi/miarc.c b/mi/miarc.c index 3223bcb02..d8e7e325f 100644 --- a/mi/miarc.c +++ b/mi/miarc.c @@ -747,13 +747,12 @@ tailX(double K, static miArcSpanData * miComputeWideEllipse(int lw, xArc * parc) { - miArcSpanData *spdata = NULL; int k; if (!lw) lw = 1; k = (parc->height >> 1) + ((lw - 1) >> 1); - spdata = malloc(sizeof(miArcSpanData) + sizeof(miArcSpan) * (k + 2)); + miArcSpanData *spdata = calloc(1, sizeof(miArcSpanData) + sizeof(miArcSpan) * (k + 2)); if (!spdata) return NULL; spdata->spans = (miArcSpan *) (spdata + 1); @@ -772,7 +771,6 @@ miFillWideEllipse(DrawablePtr pDraw, GCPtr pGC, xArc * parc) { DDXPointPtr points; DDXPointPtr pts; - int *widths; int *wids; miArcSpanData *spdata; miArcSpan *span; @@ -781,7 +779,7 @@ miFillWideEllipse(DrawablePtr pDraw, GCPtr pGC, xArc * parc) yorgu = parc->height + pGC->lineWidth; n = (sizeof(int) * 2) * yorgu; - widths = malloc(n + (sizeof(DDXPointRec) * 2) * yorgu); + int *widths = calloc(1, n + (sizeof(DDXPointRec) * 2) * yorgu); if (!widths) return; points = (DDXPointPtr) ((char *) widths + n); @@ -1395,7 +1393,7 @@ miArcJoin(DrawablePtr pDraw, GCPtr pGC, miArcFacePtr pLeft, arc.height = width; arc.angle1 = -miDatan2(corner.y - center.y, corner.x - center.x); arc.angle2 = a; - pArcPts = malloc(3 * sizeof(SppPointRec)); + pArcPts = calloc(3, sizeof(SppPointRec)); if (!pArcPts) return; pArcPts[0].x = otherCorner.x; @@ -1637,7 +1635,7 @@ miDatan2(double dy, double dx) * This procedure allocates the space necessary to fit the arc points. * Sometimes it's convenient for those points to be at the end of an existing * array. (For example, if we want to leave a spare point to make sectors - * instead of segments.) So we pass in the malloc()ed chunk that contains the + * instead of segments.) So we pass in the calloc()ed chunk that contains the * array and an index saying where we should start stashing the points. * If there isn't an array already, we just pass in a null pointer and * count on realloc() to handle the null pointer correctly. @@ -3037,11 +3035,10 @@ static struct finalSpanChunk *chunks; static struct finalSpan * realAllocSpan(void) { - struct finalSpanChunk *newChunk; struct finalSpan *span; int i; - newChunk = malloc(sizeof(struct finalSpanChunk)); + struct finalSpanChunk *newChunk = calloc(1, sizeof(struct finalSpanChunk)); if (!newChunk) return (struct finalSpan *) NULL; newChunk->next = chunks; diff --git a/mi/micmap.c b/mi/micmap.c index 055a2f910..655151035 100644 --- a/mi/micmap.c +++ b/mi/micmap.c @@ -330,9 +330,9 @@ miSetVisualTypesAndMasks(int depth, int visuals, int bitsPerRGB, int preferredCVC, Pixel redMask, Pixel greenMask, Pixel blueMask) { - miVisualsPtr new, *prev, v; + miVisualsPtr *prev, v; - new = malloc(sizeof *new); + miVisualsPtr new = calloc(1, sizeof *new); if (!new) return FALSE; if (!redMask || !greenMask || !blueMask) { diff --git a/mi/mifillarc.c b/mi/mifillarc.c index 1f467db28..3c3be268f 100644 --- a/mi/mifillarc.c +++ b/mi/mifillarc.c @@ -665,7 +665,7 @@ miPolyFillArc(DrawablePtr pDraw, GCPtr pGC, int narcs_all, xArc * parcs) nspans += (arc->height + 1) >> 1; } - pts = points = malloc (sizeof (DDXPointRec) * nspans + + pts = points = calloc(1, sizeof (DDXPointRec) * nspans + sizeof(int) * nspans); if (points) { wids = widths = (int *) (points + nspans); diff --git a/mi/mioverlay.c b/mi/mioverlay.c index 5d3df3f1e..b762e8a98 100644 --- a/mi/mioverlay.c +++ b/mi/mioverlay.c @@ -124,7 +124,7 @@ miInitOverlay(ScreenPtr pScreen, if (!dixRegisterPrivateKey(&miOverlayScreenKeyRec, PRIVATE_SCREEN, 0)) return FALSE; - if (!(pScreenPriv = malloc(sizeof(miOverlayScreenRec)))) + if (!(pScreenPriv = calloc(1, sizeof(miOverlayScreenRec)))) return FALSE; dixSetPrivate(&pScreen->devPrivates, miOverlayScreenKey, pScreenPriv); diff --git a/mi/mipointer.c b/mi/mipointer.c index 19bcde810..7d51f8659 100644 --- a/mi/mipointer.c +++ b/mi/mipointer.c @@ -134,7 +134,7 @@ miPointerInitialize(ScreenPtr pScreen, if (!dixRegisterPrivateKey(&miPointerPrivKeyRec, PRIVATE_DEVICE, 0)) return FALSE; - pScreenPriv = malloc(sizeof(miPointerScreenRec)); + pScreenPriv = calloc(1, sizeof(miPointerScreenRec)); if (!pScreenPriv) return FALSE; pScreenPriv->spriteFuncs = spriteFuncs; @@ -323,11 +323,9 @@ miRecolorCursor(DeviceIntPtr pDev, ScreenPtr pScr, static Bool miPointerDeviceInitialize(DeviceIntPtr pDev, ScreenPtr pScreen) { - miPointerPtr pPointer; - SetupScreen(pScreen); - pPointer = malloc(sizeof(miPointerRec)); + miPointerPtr pPointer = calloc(1, sizeof(miPointerRec)); if (!pPointer) return FALSE; diff --git a/mi/mipoly.c b/mi/mipoly.c index 4b068e7e2..559c6b607 100644 --- a/mi/mipoly.c +++ b/mi/mipoly.c @@ -87,7 +87,7 @@ miInsertEdgeInET(EdgeTable * ET, EdgeTableEntry * ETE, int scanline, */ if ((!pSLL) || (pSLL->scanline > scanline)) { if (*iSLLBlock > SLLSPERBLOCK - 1) { - tmpSLLBlock = malloc(sizeof(ScanLineListBlock)); + tmpSLLBlock = calloc(1, sizeof(ScanLineListBlock)); if (!tmpSLLBlock) return FALSE; (*SLLBlock)->next = tmpSLLBlock; @@ -542,7 +542,7 @@ miFillGeneralPoly(DrawablePtr dst, GCPtr pgc, int count, DDXPointPtr ptsIn) if (count < 3) return TRUE; - if (!(pETEs = malloc(sizeof(EdgeTableEntry) * count))) + if (!(pETEs = calloc(count, sizeof(EdgeTableEntry)))) return FALSE; ptsOut = FirstPoint; width = FirstWidth; diff --git a/mi/mipushpxl.c b/mi/mipushpxl.c index 028b8633e..ea8f8ae44 100644 --- a/mi/mipushpxl.c +++ b/mi/mipushpxl.c @@ -93,7 +93,6 @@ miPushPixels(GCPtr pGC, PixmapPtr pBitMap, DrawablePtr pDrawable, int dx, int dy, int xOrg, int yOrg) { int h, dxDivPPW, ibEnd; - MiBits *pwLineStart; MiBits *pw, *pwEnd; MiBits msk; int ib, w; @@ -116,7 +115,7 @@ miPushPixels(GCPtr pGC, PixmapPtr pBitMap, DrawablePtr pDrawable, startmask = (MiBits) (-1) ^ LONG2CHARSDIFFORDER((MiBits) (-1) >> 1); #endif - pwLineStart = malloc(BitmapBytePad(dx)); + MiBits *pwLineStart = calloc(1, BitmapBytePad(dx)); if (!pwLineStart) return; ipt = 0; diff --git a/mi/miscrinit.c b/mi/miscrinit.c index 1372cf754..02d6ab3b7 100644 --- a/mi/miscrinit.c +++ b/mi/miscrinit.c @@ -192,13 +192,11 @@ miCreateScreenResources(ScreenPtr pScreen) static Bool miScreenDevPrivateInit(ScreenPtr pScreen, int width, void *pbits, int xsize, int ysize) { - miScreenInitParmsPtr pScrInitParms; - /* Stash pbits and width in a short-lived miScreenInitParmsRec attached * to the screen, until CreateScreenResources can put them in the * screen pixmap. */ - pScrInitParms = malloc(sizeof(miScreenInitParmsRec)); + miScreenInitParmsPtr pScrInitParms = calloc(1, sizeof(miScreenInitParmsRec)); if (!pScrInitParms) return FALSE; pScrInitParms->pbits = pbits; diff --git a/mi/misprite.c b/mi/misprite.c index 2e1d73491..cee97bcef 100644 --- a/mi/misprite.c +++ b/mi/misprite.c @@ -280,7 +280,6 @@ miSpriteReportDamage(DamagePtr pDamage, RegionPtr pRegion, void *closure) Bool miSpriteInitialize(ScreenPtr pScreen, miPointerScreenFuncPtr screenFuncs) { - miSpriteScreenPtr pScreenPriv; VisualPtr pVisual; if (!DamageSetup(pScreen)) @@ -293,7 +292,7 @@ miSpriteInitialize(ScreenPtr pScreen, miPointerScreenFuncPtr screenFuncs) (&miSpriteDevPrivatesKeyRec, PRIVATE_DEVICE, sizeof(miCursorInfoRec))) return FALSE; - pScreenPriv = malloc(sizeof(miSpriteScreenRec)); + miSpriteScreenPtr pScreenPriv = calloc(1, sizeof(miSpriteScreenRec)); if (!pScreenPriv) return FALSE;