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 <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2025-04-10 19:21:40 +02:00
parent 55544ff85f
commit 8c05f4db0a
9 changed files with 16 additions and 25 deletions

View File

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

View File

@ -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) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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