From a87fa30ed5f0de1f5750d34a91c19592a2002f00 Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Thu, 10 Apr 2025 19:45:12 +0200 Subject: [PATCH] render: 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 --- render/filter.c | 9 ++++----- render/glyph.c | 3 +-- render/miindex.c | 3 +-- render/picture.c | 14 ++++++-------- render/render.c | 49 +++++++++++++++++++++++++----------------------- 5 files changed, 38 insertions(+), 40 deletions(-) diff --git a/render/filter.c b/render/filter.c index 546998a92..ac142d2f6 100644 --- a/render/filter.c +++ b/render/filter.c @@ -92,7 +92,6 @@ int PictureGetFilterId(const char *filter, int len, Bool makeit) { int i; - char *name; char **names; if (len < 0) @@ -103,7 +102,7 @@ PictureGetFilterId(const char *filter, int len, Bool makeit) return i; if (!makeit) return -1; - name = malloc(len + 1); + char *name = calloc(1, len + 1); if (!name) return -1; memcpy(name, filter, len); @@ -111,7 +110,7 @@ PictureGetFilterId(const char *filter, int len, Bool makeit) if (filterNames) names = reallocarray(filterNames, nfilterNames + 1, sizeof(char *)); else - names = malloc(sizeof(char *)); + names = calloc(1, sizeof(char *)); if (!names) { free(name); return -1; @@ -189,7 +188,7 @@ PictureAddFilter(ScreenPtr pScreen, filters = reallocarray(ps->filters, ps->nfilters + 1, sizeof(PictFilterRec)); else - filters = malloc(sizeof(PictFilterRec)); + filters = calloc(1, sizeof(PictFilterRec)); if (!filters) return -1; ps->filters = filters; @@ -223,7 +222,7 @@ PictureSetFilterAlias(ScreenPtr pScreen, const char *filter, const char *alias) ps->nfilterAliases + 1, sizeof(PictFilterAliasRec)); else - aliases = malloc(sizeof(PictFilterAliasRec)); + aliases = calloc(1, sizeof(PictFilterAliasRec)); if (!aliases) return FALSE; ps->filterAliases = aliases; diff --git a/render/glyph.c b/render/glyph.c index cc982a038..5a5e79714 100644 --- a/render/glyph.c +++ b/render/glyph.c @@ -343,13 +343,12 @@ AllocateGlyph(xGlyphInfo * gi, int fdepth) { PictureScreenPtr ps; int size; - GlyphPtr glyph; int i; int head_size; head_size = sizeof(GlyphRec) + screenInfo.numScreens * sizeof(PicturePtr); size = (head_size + dixPrivatesSize(PRIVATE_GLYPH)); - glyph = (GlyphPtr) malloc(size); + GlyphPtr glyph = calloc(1, size); if (!glyph) return 0; glyph->refcnt = 1; diff --git a/render/miindex.c b/render/miindex.c index 6b0bb2c9b..df9cc110d 100644 --- a/render/miindex.c +++ b/render/miindex.c @@ -226,7 +226,6 @@ miInitIndexed(ScreenPtr pScreen, PictFormatPtr pFormat) { ColormapPtr pColormap = pFormat->index.pColormap; VisualPtr pVisual = pColormap->pVisual; - miIndexedPtr pIndexed; Pixel pixels[MI_MAX_INDEXED]; xrgb rgb[MI_MAX_INDEXED]; int num; @@ -246,7 +245,7 @@ miInitIndexed(ScreenPtr pScreen, PictFormatPtr pFormat) pixels[p] = p; } - pIndexed = malloc(sizeof(miIndexedRec)); + miIndexedPtr pIndexed = calloc(1, sizeof(miIndexedRec)); if (!pIndexed) return FALSE; diff --git a/render/picture.c b/render/picture.c index feb29a7d7..d1eb471b1 100644 --- a/render/picture.c +++ b/render/picture.c @@ -611,7 +611,6 @@ FreePictFormat(void *pPictFormat, XID pid) Bool PictureInit(ScreenPtr pScreen, PictFormatPtr formats, int nformats) { - PictureScreenPtr ps; int n; CARD32 type, a, r, g, b; @@ -674,7 +673,7 @@ PictureInit(ScreenPtr pScreen, PictFormatPtr formats, int nformats) } formats[n].format = PICT_FORMAT(0, type, a, r, g, b); } - ps = (PictureScreenPtr) malloc(sizeof(PictureScreenRec)); + PictureScreenPtr ps = calloc(1, sizeof(PictureScreenRec)); if (!ps) { free(formats); return FALSE; @@ -870,7 +869,7 @@ CreateSolidPicture(Picture pid, xRenderColor * color, int *error) } pPicture->id = pid; - pPicture->pSourcePict = (SourcePictPtr) malloc(sizeof(SourcePict)); + pPicture->pSourcePict = calloc(1, sizeof(SourcePict)); if (!pPicture->pSourcePict) { *error = BadAlloc; free(pPicture); @@ -901,7 +900,7 @@ CreateLinearGradientPicture(Picture pid, xPointFixed * p1, xPointFixed * p2, } pPicture->id = pid; - pPicture->pSourcePict = (SourcePictPtr) malloc(sizeof(SourcePict)); + pPicture->pSourcePict = calloc(1, sizeof(SourcePict)); if (!pPicture->pSourcePict) { *error = BadAlloc; free(pPicture); @@ -941,7 +940,7 @@ CreateRadialGradientPicture(Picture pid, xPointFixed * inner, } pPicture->id = pid; - pPicture->pSourcePict = (SourcePictPtr) malloc(sizeof(SourcePict)); + pPicture->pSourcePict = calloc(1, sizeof(SourcePict)); if (!pPicture->pSourcePict) { *error = BadAlloc; free(pPicture); @@ -984,7 +983,7 @@ CreateConicalGradientPicture(Picture pid, xPointFixed * center, xFixed angle, } pPicture->id = pid; - pPicture->pSourcePict = (SourcePictPtr) malloc(sizeof(SourcePict)); + pPicture->pSourcePict = calloc(1, sizeof(SourcePict)); if (!pPicture->pSourcePict) { *error = BadAlloc; free(pPicture); @@ -1332,8 +1331,7 @@ SetPictureTransform(PicturePtr pPicture, PictTransform * transform) if (transform) { if (!pPicture->transform) { - pPicture->transform = - (PictTransform *) malloc(sizeof(PictTransform)); + pPicture->transform = calloc(1, sizeof(PictTransform)); if (!pPicture->transform) return BadAlloc; } diff --git a/render/render.c b/render/render.c index 113f6e0c5..357f33d4b 100644 --- a/render/render.c +++ b/render/render.c @@ -310,7 +310,6 @@ static int ProcRenderQueryPictFormats(ClientPtr client) { RenderClientPtr pRenderClient = GetRenderClient(client); - xRenderQueryPictFormatsReply *reply; xPictScreen *pictScreen; xPictDepth *pictDepth; xPictVisual *pictVisual; @@ -370,7 +369,8 @@ ProcRenderQueryPictFormats(ClientPtr client) numScreens * sizeof(xPictScreen) + ndepth * sizeof(xPictDepth) + nvisual * sizeof(xPictVisual) + numSubpixel * sizeof(CARD32)); - reply = (xRenderQueryPictFormatsReply *) calloc(1, rlength); + + xRenderQueryPictFormatsReply *reply = calloc(1, rlength); if (!reply) return BadAlloc; reply->type = X_Reply; @@ -506,7 +506,6 @@ ProcRenderQueryPictIndexValues(ClientPtr client) int i; REQUEST(xRenderQueryPictIndexValuesReq); - xRenderQueryPictIndexValuesReply *reply; xIndexValue *values; REQUEST_AT_LEAST_SIZE(xRenderQueryPictIndexValuesReq); @@ -523,7 +522,8 @@ ProcRenderQueryPictIndexValues(ClientPtr client) num = pFormat->index.nvalues; rlength = (sizeof(xRenderQueryPictIndexValuesReply) + num * sizeof(xIndexValue)); - reply = (xRenderQueryPictIndexValuesReply *) calloc(1, rlength); + + xRenderQueryPictIndexValuesReply *reply = calloc(1, rlength); if (!reply) return BadAlloc; @@ -1461,9 +1461,9 @@ ProcRenderCreateCursor(ClientPtr client) PicturePtr pSrc; ScreenPtr pScreen; unsigned short width, height; - CARD32 *argbbits, *argb; - unsigned char *srcbits, *srcline; - unsigned char *mskbits, *mskline; + CARD32 *argb; + unsigned char *srcline; + unsigned char *mskline; int stride; int x, y; int nbytes_mono; @@ -1485,18 +1485,21 @@ ProcRenderCreateCursor(ClientPtr client) return BadAlloc; if (stuff->x > width || stuff->y > height) return BadMatch; - argbbits = malloc(width * height * sizeof(CARD32)); + + CARD32 *argbbits = calloc(width * height, sizeof(CARD32)); if (!argbbits) return BadAlloc; stride = BitmapBytePad(width); nbytes_mono = stride * height; - srcbits = calloc(1, nbytes_mono); + + unsigned char *srcbits = calloc(1, nbytes_mono); if (!srcbits) { free(argbbits); return BadAlloc; } - mskbits = calloc(1, nbytes_mono); + + unsigned char *mskbits = calloc(1, nbytes_mono); if (!mskbits) { free(argbbits); free(srcbits); @@ -1664,7 +1667,6 @@ ProcRenderQueryFilters(ClientPtr client) { REQUEST(xRenderQueryFiltersReq); DrawablePtr pDrawable; - xRenderQueryFiltersReply *reply; int nbytesName; int nnames; ScreenPtr pScreen; @@ -1692,7 +1694,8 @@ ProcRenderQueryFilters(ClientPtr client) } len = ((nnames + 1) >> 1) + bytes_to_int32(nbytesName); total_bytes = sizeof(xRenderQueryFiltersReply) + (len << 2); - reply = (xRenderQueryFiltersReply *) calloc(1, total_bytes); + + xRenderQueryFiltersReply *reply = calloc(1, total_bytes); if (!reply) return BadAlloc; aliases = (INT16 *) (reply + 1); @@ -2585,7 +2588,7 @@ PanoramiXRenderCreatePicture(ClientPtr client) XRC_DRAWABLE, client, DixWriteAccess); if (result != Success) return (result == BadValue) ? BadDrawable : result; - if (!(newPict = (PanoramiXRes *) malloc(sizeof(PanoramiXRes)))) + if (!(newPict = calloc(1, sizeof(PanoramiXRes)))) return BadAlloc; newPict->type = XRT_PICTURE; panoramix_setup_ids(newPict, client, stuff->pid); @@ -2826,7 +2829,7 @@ PanoramiXRenderFillRectangles(ClientPtr client) REQUEST_AT_LEAST_SIZE(xRenderFillRectanglesReq); VERIFY_XIN_PICTURE(dst, stuff->dst, client, DixWriteAccess); extra_len = (client->req_len << 2) - sizeof(xRenderFillRectanglesReq); - if (extra_len && (extra = (char *) malloc(extra_len))) { + if (extra_len && (extra = calloc(1, extra_len))) { memcpy(extra, stuff + 1, extra_len); FOR_NSCREENS_FORWARD(j) { if (j) @@ -2875,7 +2878,7 @@ PanoramiXRenderTrapezoids(ClientPtr client) extra_len = (client->req_len << 2) - sizeof(xRenderTrapezoidsReq); - if (extra_len && (extra = (char *) malloc(extra_len))) { + if (extra_len && (extra = calloc(1, extra_len))) { memcpy(extra, stuff + 1, extra_len); FOR_NSCREENS_FORWARD(j) { @@ -2936,7 +2939,7 @@ PanoramiXRenderTriangles(ClientPtr client) extra_len = (client->req_len << 2) - sizeof(xRenderTrianglesReq); - if (extra_len && (extra = (char *) malloc(extra_len))) { + if (extra_len && (extra = calloc(1, extra_len))) { memcpy(extra, stuff + 1, extra_len); FOR_NSCREENS_FORWARD(j) { @@ -2993,7 +2996,7 @@ PanoramiXRenderTriStrip(ClientPtr client) extra_len = (client->req_len << 2) - sizeof(xRenderTriStripReq); - if (extra_len && (extra = (char *) malloc(extra_len))) { + if (extra_len && (extra = calloc(1, extra_len))) { memcpy(extra, stuff + 1, extra_len); FOR_NSCREENS_FORWARD(j) { @@ -3046,7 +3049,7 @@ PanoramiXRenderTriFan(ClientPtr client) extra_len = (client->req_len << 2) - sizeof(xRenderTriFanReq); - if (extra_len && (extra = (char *) malloc(extra_len))) { + if (extra_len && (extra = calloc(1, extra_len))) { memcpy(extra, stuff + 1, extra_len); FOR_NSCREENS_FORWARD(j) { @@ -3096,7 +3099,7 @@ PanoramiXRenderAddTraps(ClientPtr client) REQUEST_AT_LEAST_SIZE(xRenderAddTrapsReq); VERIFY_XIN_PICTURE(picture, stuff->picture, client, DixWriteAccess); extra_len = (client->req_len << 2) - sizeof(xRenderAddTrapsReq); - if (extra_len && (extra = (char *) malloc(extra_len))) { + if (extra_len && (extra = calloc(1, extra_len))) { memcpy(extra, stuff + 1, extra_len); x_off = stuff->xOff; y_off = stuff->yOff; @@ -3128,7 +3131,7 @@ PanoramiXRenderCreateSolidFill(ClientPtr client) REQUEST_AT_LEAST_SIZE(xRenderCreateSolidFillReq); - if (!(newPict = (PanoramiXRes *) malloc(sizeof(PanoramiXRes)))) + if (!(newPict = calloc(1, sizeof(PanoramiXRes)))) return BadAlloc; newPict->type = XRT_PICTURE; @@ -3159,7 +3162,7 @@ PanoramiXRenderCreateLinearGradient(ClientPtr client) REQUEST_AT_LEAST_SIZE(xRenderCreateLinearGradientReq); - if (!(newPict = (PanoramiXRes *) malloc(sizeof(PanoramiXRes)))) + if (!(newPict = calloc(1, sizeof(PanoramiXRes)))) return BadAlloc; newPict->type = XRT_PICTURE; @@ -3191,7 +3194,7 @@ PanoramiXRenderCreateRadialGradient(ClientPtr client) REQUEST_AT_LEAST_SIZE(xRenderCreateRadialGradientReq); - if (!(newPict = (PanoramiXRes *) malloc(sizeof(PanoramiXRes)))) + if (!(newPict = calloc(1, sizeof(PanoramiXRes)))) return BadAlloc; newPict->type = XRT_PICTURE; @@ -3223,7 +3226,7 @@ PanoramiXRenderCreateConicalGradient(ClientPtr client) REQUEST_AT_LEAST_SIZE(xRenderCreateConicalGradientReq); - if (!(newPict = (PanoramiXRes *) malloc(sizeof(PanoramiXRes)))) + if (!(newPict = calloc(1, sizeof(PanoramiXRes)))) return BadAlloc; newPict->type = XRT_PICTURE;