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 <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult 2025-04-10 19:45:12 +02:00
parent 8c873c04cb
commit a2d9d2078f
5 changed files with 38 additions and 40 deletions

View File

@ -92,7 +92,6 @@ int
PictureGetFilterId(const char *filter, int len, Bool makeit) PictureGetFilterId(const char *filter, int len, Bool makeit)
{ {
int i; int i;
char *name;
char **names; char **names;
if (len < 0) if (len < 0)
@ -103,7 +102,7 @@ PictureGetFilterId(const char *filter, int len, Bool makeit)
return i; return i;
if (!makeit) if (!makeit)
return -1; return -1;
name = malloc(len + 1); char *name = calloc(1, len + 1);
if (!name) if (!name)
return -1; return -1;
memcpy(name, filter, len); memcpy(name, filter, len);
@ -111,7 +110,7 @@ PictureGetFilterId(const char *filter, int len, Bool makeit)
if (filterNames) if (filterNames)
names = reallocarray(filterNames, nfilterNames + 1, sizeof(char *)); names = reallocarray(filterNames, nfilterNames + 1, sizeof(char *));
else else
names = malloc(sizeof(char *)); names = calloc(1, sizeof(char *));
if (!names) { if (!names) {
free(name); free(name);
return -1; return -1;
@ -189,7 +188,7 @@ PictureAddFilter(ScreenPtr pScreen,
filters = filters =
reallocarray(ps->filters, ps->nfilters + 1, sizeof(PictFilterRec)); reallocarray(ps->filters, ps->nfilters + 1, sizeof(PictFilterRec));
else else
filters = malloc(sizeof(PictFilterRec)); filters = calloc(1, sizeof(PictFilterRec));
if (!filters) if (!filters)
return -1; return -1;
ps->filters = filters; ps->filters = filters;
@ -223,7 +222,7 @@ PictureSetFilterAlias(ScreenPtr pScreen, const char *filter, const char *alias)
ps->nfilterAliases + 1, ps->nfilterAliases + 1,
sizeof(PictFilterAliasRec)); sizeof(PictFilterAliasRec));
else else
aliases = malloc(sizeof(PictFilterAliasRec)); aliases = calloc(1, sizeof(PictFilterAliasRec));
if (!aliases) if (!aliases)
return FALSE; return FALSE;
ps->filterAliases = aliases; ps->filterAliases = aliases;

View File

@ -347,13 +347,12 @@ AllocateGlyph(xGlyphInfo * gi, int fdepth)
{ {
PictureScreenPtr ps; PictureScreenPtr ps;
int size; int size;
GlyphPtr glyph;
int i; int i;
int head_size; int head_size;
head_size = sizeof(GlyphRec) + screenInfo.numScreens * sizeof(PicturePtr); head_size = sizeof(GlyphRec) + screenInfo.numScreens * sizeof(PicturePtr);
size = (head_size + dixPrivatesSize(PRIVATE_GLYPH)); size = (head_size + dixPrivatesSize(PRIVATE_GLYPH));
glyph = (GlyphPtr) malloc(size); GlyphPtr glyph = calloc(1, size);
if (!glyph) if (!glyph)
return 0; return 0;
glyph->refcnt = 1; glyph->refcnt = 1;

View File

@ -226,7 +226,6 @@ miInitIndexed(ScreenPtr pScreen, PictFormatPtr pFormat)
{ {
ColormapPtr pColormap = pFormat->index.pColormap; ColormapPtr pColormap = pFormat->index.pColormap;
VisualPtr pVisual = pColormap->pVisual; VisualPtr pVisual = pColormap->pVisual;
miIndexedPtr pIndexed;
Pixel pixels[MI_MAX_INDEXED]; Pixel pixels[MI_MAX_INDEXED];
xrgb rgb[MI_MAX_INDEXED]; xrgb rgb[MI_MAX_INDEXED];
int num; int num;
@ -246,7 +245,7 @@ miInitIndexed(ScreenPtr pScreen, PictFormatPtr pFormat)
pixels[p] = p; pixels[p] = p;
} }
pIndexed = malloc(sizeof(miIndexedRec)); miIndexedPtr pIndexed = calloc(1, sizeof(miIndexedRec));
if (!pIndexed) if (!pIndexed)
return FALSE; return FALSE;

View File

@ -600,7 +600,6 @@ FreePictFormat(void *pPictFormat, XID pid)
Bool Bool
PictureInit(ScreenPtr pScreen, PictFormatPtr formats, int nformats) PictureInit(ScreenPtr pScreen, PictFormatPtr formats, int nformats)
{ {
PictureScreenPtr ps;
int n; int n;
CARD32 type, a, r, g, b; CARD32 type, a, r, g, b;
@ -663,7 +662,7 @@ PictureInit(ScreenPtr pScreen, PictFormatPtr formats, int nformats)
} }
formats[n].format = PICT_FORMAT(0, type, a, r, g, b); formats[n].format = PICT_FORMAT(0, type, a, r, g, b);
} }
ps = (PictureScreenPtr) malloc(sizeof(PictureScreenRec)); PictureScreenPtr ps = calloc(1, sizeof(PictureScreenRec));
if (!ps) { if (!ps) {
free(formats); free(formats);
return FALSE; return FALSE;
@ -858,7 +857,7 @@ CreateSolidPicture(Picture pid, xRenderColor * color, int *error)
} }
pPicture->id = pid; pPicture->id = pid;
pPicture->pSourcePict = (SourcePictPtr) malloc(sizeof(SourcePict)); pPicture->pSourcePict = calloc(1, sizeof(SourcePict));
if (!pPicture->pSourcePict) { if (!pPicture->pSourcePict) {
*error = BadAlloc; *error = BadAlloc;
free(pPicture); free(pPicture);
@ -889,7 +888,7 @@ CreateLinearGradientPicture(Picture pid, xPointFixed * p1, xPointFixed * p2,
} }
pPicture->id = pid; pPicture->id = pid;
pPicture->pSourcePict = (SourcePictPtr) malloc(sizeof(SourcePict)); pPicture->pSourcePict = calloc(1, sizeof(SourcePict));
if (!pPicture->pSourcePict) { if (!pPicture->pSourcePict) {
*error = BadAlloc; *error = BadAlloc;
free(pPicture); free(pPicture);
@ -929,7 +928,7 @@ CreateRadialGradientPicture(Picture pid, xPointFixed * inner,
} }
pPicture->id = pid; pPicture->id = pid;
pPicture->pSourcePict = (SourcePictPtr) malloc(sizeof(SourcePict)); pPicture->pSourcePict = calloc(1, sizeof(SourcePict));
if (!pPicture->pSourcePict) { if (!pPicture->pSourcePict) {
*error = BadAlloc; *error = BadAlloc;
free(pPicture); free(pPicture);
@ -972,7 +971,7 @@ CreateConicalGradientPicture(Picture pid, xPointFixed * center, xFixed angle,
} }
pPicture->id = pid; pPicture->id = pid;
pPicture->pSourcePict = (SourcePictPtr) malloc(sizeof(SourcePict)); pPicture->pSourcePict = calloc(1, sizeof(SourcePict));
if (!pPicture->pSourcePict) { if (!pPicture->pSourcePict) {
*error = BadAlloc; *error = BadAlloc;
free(pPicture); free(pPicture);
@ -1323,8 +1322,7 @@ SetPictureTransform(PicturePtr pPicture, PictTransform * transform)
if (transform) { if (transform) {
if (!pPicture->transform) { if (!pPicture->transform) {
pPicture->transform = pPicture->transform = calloc(1, sizeof(PictTransform));
(PictTransform *) malloc(sizeof(PictTransform));
if (!pPicture->transform) if (!pPicture->transform)
return BadAlloc; return BadAlloc;
} }

View File

@ -310,7 +310,6 @@ static int
ProcRenderQueryPictFormats(ClientPtr client) ProcRenderQueryPictFormats(ClientPtr client)
{ {
RenderClientPtr pRenderClient = GetRenderClient(client); RenderClientPtr pRenderClient = GetRenderClient(client);
xRenderQueryPictFormatsReply *reply;
xPictScreen *pictScreen; xPictScreen *pictScreen;
xPictDepth *pictDepth; xPictDepth *pictDepth;
xPictVisual *pictVisual; xPictVisual *pictVisual;
@ -370,7 +369,8 @@ ProcRenderQueryPictFormats(ClientPtr client)
numScreens * sizeof(xPictScreen) + numScreens * sizeof(xPictScreen) +
ndepth * sizeof(xPictDepth) + ndepth * sizeof(xPictDepth) +
nvisual * sizeof(xPictVisual) + numSubpixel * sizeof(CARD32)); nvisual * sizeof(xPictVisual) + numSubpixel * sizeof(CARD32));
reply = (xRenderQueryPictFormatsReply *) calloc(1, rlength);
xRenderQueryPictFormatsReply *reply = calloc(1, rlength);
if (!reply) if (!reply)
return BadAlloc; return BadAlloc;
reply->type = X_Reply; reply->type = X_Reply;
@ -506,7 +506,6 @@ ProcRenderQueryPictIndexValues(ClientPtr client)
int i; int i;
REQUEST(xRenderQueryPictIndexValuesReq); REQUEST(xRenderQueryPictIndexValuesReq);
xRenderQueryPictIndexValuesReply *reply;
xIndexValue *values; xIndexValue *values;
REQUEST_AT_LEAST_SIZE(xRenderQueryPictIndexValuesReq); REQUEST_AT_LEAST_SIZE(xRenderQueryPictIndexValuesReq);
@ -523,7 +522,8 @@ ProcRenderQueryPictIndexValues(ClientPtr client)
num = pFormat->index.nvalues; num = pFormat->index.nvalues;
rlength = (sizeof(xRenderQueryPictIndexValuesReply) + rlength = (sizeof(xRenderQueryPictIndexValuesReply) +
num * sizeof(xIndexValue)); num * sizeof(xIndexValue));
reply = (xRenderQueryPictIndexValuesReply *) calloc(1, rlength);
xRenderQueryPictIndexValuesReply *reply = calloc(1, rlength);
if (!reply) if (!reply)
return BadAlloc; return BadAlloc;
@ -1461,9 +1461,9 @@ ProcRenderCreateCursor(ClientPtr client)
PicturePtr pSrc; PicturePtr pSrc;
ScreenPtr pScreen; ScreenPtr pScreen;
unsigned short width, height; unsigned short width, height;
CARD32 *argbbits, *argb; CARD32 *argb;
unsigned char *srcbits, *srcline; unsigned char *srcline;
unsigned char *mskbits, *mskline; unsigned char *mskline;
int stride; int stride;
int x, y; int x, y;
int nbytes_mono; int nbytes_mono;
@ -1485,18 +1485,21 @@ ProcRenderCreateCursor(ClientPtr client)
return BadAlloc; return BadAlloc;
if (stuff->x > width || stuff->y > height) if (stuff->x > width || stuff->y > height)
return BadMatch; return BadMatch;
argbbits = malloc(width * height * sizeof(CARD32));
CARD32 *argbbits = calloc(width * height, sizeof(CARD32));
if (!argbbits) if (!argbbits)
return BadAlloc; return BadAlloc;
stride = BitmapBytePad(width); stride = BitmapBytePad(width);
nbytes_mono = stride * height; nbytes_mono = stride * height;
srcbits = calloc(1, nbytes_mono);
unsigned char *srcbits = calloc(1, nbytes_mono);
if (!srcbits) { if (!srcbits) {
free(argbbits); free(argbbits);
return BadAlloc; return BadAlloc;
} }
mskbits = calloc(1, nbytes_mono);
unsigned char *mskbits = calloc(1, nbytes_mono);
if (!mskbits) { if (!mskbits) {
free(argbbits); free(argbbits);
free(srcbits); free(srcbits);
@ -1664,7 +1667,6 @@ ProcRenderQueryFilters(ClientPtr client)
{ {
REQUEST(xRenderQueryFiltersReq); REQUEST(xRenderQueryFiltersReq);
DrawablePtr pDrawable; DrawablePtr pDrawable;
xRenderQueryFiltersReply *reply;
int nbytesName; int nbytesName;
int nnames; int nnames;
ScreenPtr pScreen; ScreenPtr pScreen;
@ -1692,7 +1694,8 @@ ProcRenderQueryFilters(ClientPtr client)
} }
len = ((nnames + 1) >> 1) + bytes_to_int32(nbytesName); len = ((nnames + 1) >> 1) + bytes_to_int32(nbytesName);
total_bytes = sizeof(xRenderQueryFiltersReply) + (len << 2); total_bytes = sizeof(xRenderQueryFiltersReply) + (len << 2);
reply = (xRenderQueryFiltersReply *) calloc(1, total_bytes);
xRenderQueryFiltersReply *reply = calloc(1, total_bytes);
if (!reply) if (!reply)
return BadAlloc; return BadAlloc;
aliases = (INT16 *) (reply + 1); aliases = (INT16 *) (reply + 1);
@ -2585,7 +2588,7 @@ PanoramiXRenderCreatePicture(ClientPtr client)
XRC_DRAWABLE, client, DixWriteAccess); XRC_DRAWABLE, client, DixWriteAccess);
if (result != Success) if (result != Success)
return (result == BadValue) ? BadDrawable : result; return (result == BadValue) ? BadDrawable : result;
if (!(newPict = (PanoramiXRes *) malloc(sizeof(PanoramiXRes)))) if (!(newPict = calloc(1, sizeof(PanoramiXRes))))
return BadAlloc; return BadAlloc;
newPict->type = XRT_PICTURE; newPict->type = XRT_PICTURE;
panoramix_setup_ids(newPict, client, stuff->pid); panoramix_setup_ids(newPict, client, stuff->pid);
@ -2826,7 +2829,7 @@ PanoramiXRenderFillRectangles(ClientPtr client)
REQUEST_AT_LEAST_SIZE(xRenderFillRectanglesReq); REQUEST_AT_LEAST_SIZE(xRenderFillRectanglesReq);
VERIFY_XIN_PICTURE(dst, stuff->dst, client, DixWriteAccess); VERIFY_XIN_PICTURE(dst, stuff->dst, client, DixWriteAccess);
extra_len = (client->req_len << 2) - sizeof(xRenderFillRectanglesReq); 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); memcpy(extra, stuff + 1, extra_len);
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
if (j) if (j)
@ -2875,7 +2878,7 @@ PanoramiXRenderTrapezoids(ClientPtr client)
extra_len = (client->req_len << 2) - sizeof(xRenderTrapezoidsReq); 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); memcpy(extra, stuff + 1, extra_len);
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -2936,7 +2939,7 @@ PanoramiXRenderTriangles(ClientPtr client)
extra_len = (client->req_len << 2) - sizeof(xRenderTrianglesReq); 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); memcpy(extra, stuff + 1, extra_len);
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -2993,7 +2996,7 @@ PanoramiXRenderTriStrip(ClientPtr client)
extra_len = (client->req_len << 2) - sizeof(xRenderTriStripReq); 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); memcpy(extra, stuff + 1, extra_len);
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -3046,7 +3049,7 @@ PanoramiXRenderTriFan(ClientPtr client)
extra_len = (client->req_len << 2) - sizeof(xRenderTriFanReq); 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); memcpy(extra, stuff + 1, extra_len);
FOR_NSCREENS_FORWARD(j) { FOR_NSCREENS_FORWARD(j) {
@ -3096,7 +3099,7 @@ PanoramiXRenderAddTraps(ClientPtr client)
REQUEST_AT_LEAST_SIZE(xRenderAddTrapsReq); REQUEST_AT_LEAST_SIZE(xRenderAddTrapsReq);
VERIFY_XIN_PICTURE(picture, stuff->picture, client, DixWriteAccess); VERIFY_XIN_PICTURE(picture, stuff->picture, client, DixWriteAccess);
extra_len = (client->req_len << 2) - sizeof(xRenderAddTrapsReq); 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); memcpy(extra, stuff + 1, extra_len);
x_off = stuff->xOff; x_off = stuff->xOff;
y_off = stuff->yOff; y_off = stuff->yOff;
@ -3128,7 +3131,7 @@ PanoramiXRenderCreateSolidFill(ClientPtr client)
REQUEST_AT_LEAST_SIZE(xRenderCreateSolidFillReq); REQUEST_AT_LEAST_SIZE(xRenderCreateSolidFillReq);
if (!(newPict = (PanoramiXRes *) malloc(sizeof(PanoramiXRes)))) if (!(newPict = calloc(1, sizeof(PanoramiXRes))))
return BadAlloc; return BadAlloc;
newPict->type = XRT_PICTURE; newPict->type = XRT_PICTURE;
@ -3159,7 +3162,7 @@ PanoramiXRenderCreateLinearGradient(ClientPtr client)
REQUEST_AT_LEAST_SIZE(xRenderCreateLinearGradientReq); REQUEST_AT_LEAST_SIZE(xRenderCreateLinearGradientReq);
if (!(newPict = (PanoramiXRes *) malloc(sizeof(PanoramiXRes)))) if (!(newPict = calloc(1, sizeof(PanoramiXRes))))
return BadAlloc; return BadAlloc;
newPict->type = XRT_PICTURE; newPict->type = XRT_PICTURE;
@ -3191,7 +3194,7 @@ PanoramiXRenderCreateRadialGradient(ClientPtr client)
REQUEST_AT_LEAST_SIZE(xRenderCreateRadialGradientReq); REQUEST_AT_LEAST_SIZE(xRenderCreateRadialGradientReq);
if (!(newPict = (PanoramiXRes *) malloc(sizeof(PanoramiXRes)))) if (!(newPict = calloc(1, sizeof(PanoramiXRes))))
return BadAlloc; return BadAlloc;
newPict->type = XRT_PICTURE; newPict->type = XRT_PICTURE;
@ -3223,7 +3226,7 @@ PanoramiXRenderCreateConicalGradient(ClientPtr client)
REQUEST_AT_LEAST_SIZE(xRenderCreateConicalGradientReq); REQUEST_AT_LEAST_SIZE(xRenderCreateConicalGradientReq);
if (!(newPict = (PanoramiXRes *) malloc(sizeof(PanoramiXRes)))) if (!(newPict = calloc(1, sizeof(PanoramiXRes))))
return BadAlloc; return BadAlloc;
newPict->type = XRT_PICTURE; newPict->type = XRT_PICTURE;