From 2ad73189b4e22adb256f67952f67954e49db09fa Mon Sep 17 00:00:00 2001 From: "Enrico Weigelt, metux IT consult" Date: Thu, 10 Apr 2025 20:04:52 +0200 Subject: [PATCH] xfree86: 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 --- hw/xfree86/common/xf86AutoConfig.c | 6 ++--- hw/xfree86/common/xf86Configure.c | 11 +++----- hw/xfree86/common/xf86Cursor.c | 4 +-- hw/xfree86/common/xf86DGA.c | 10 +++---- hw/xfree86/common/xf86Option.c | 4 +-- hw/xfree86/common/xf86RandR.c | 3 +-- hw/xfree86/common/xf86VGAarbiter.c | 2 +- hw/xfree86/common/xf86cmap.c | 7 +++-- hw/xfree86/common/xf86fbman.c | 14 +++++----- hw/xfree86/common/xf86pciBus.c | 2 +- hw/xfree86/common/xf86sbusBus.c | 2 +- hw/xfree86/common/xf86xv.c | 2 +- hw/xfree86/common/xf86xvmc.c | 2 +- hw/xfree86/common/xisb.c | 6 ++--- hw/xfree86/ddc/ddc.c | 10 +++---- hw/xfree86/dri/dri.c | 2 +- hw/xfree86/dri2/dri2.c | 7 ++--- hw/xfree86/drivers/modesetting/dri2.c | 3 +-- hw/xfree86/drivers/modesetting/driver.c | 2 +- .../drivers/modesetting/drmmode_display.c | 4 +-- hw/xfree86/int10/helper_mem.c | 2 +- hw/xfree86/int10/vbe.c | 9 +++---- hw/xfree86/loader/loadmod.c | 9 +++---- hw/xfree86/modes/xf86Cursors.c | 2 +- hw/xfree86/modes/xf86RandR12.c | 3 +-- hw/xfree86/modes/xf86Rotate.c | 2 +- hw/xfree86/os-support/bus/Sbus.c | 10 +++---- hw/xfree86/parser/Files.c | 2 +- hw/xfree86/parser/InputClass.c | 4 +-- hw/xfree86/parser/OutputClass.c | 4 +-- hw/xfree86/parser/scan.c | 26 +++++++++---------- hw/xfree86/shadowfb/shadowfb.c | 2 +- hw/xfree86/utils/gtf/gtf.c | 2 +- hw/xfree86/vgahw/vgaHW.c | 6 ++--- 34 files changed, 77 insertions(+), 109 deletions(-) diff --git a/hw/xfree86/common/xf86AutoConfig.c b/hw/xfree86/common/xf86AutoConfig.c index 5bbf876d9..41e93f50b 100644 --- a/hw/xfree86/common/xf86AutoConfig.c +++ b/hw/xfree86/common/xf86AutoConfig.c @@ -324,16 +324,14 @@ listPossibleVideoDrivers(XF86MatchedDrivers *md) static Bool copyScreen(confScreenPtr oscreen, GDevPtr odev, int i, char *driver) { - confScreenPtr nscreen; - GDevPtr cptr = NULL; char *identifier; - nscreen = malloc(sizeof(confScreenRec)); + confScreenPtr nscreen = calloc(1, sizeof(confScreenRec)); if (!nscreen) return FALSE; memcpy(nscreen, oscreen, sizeof(confScreenRec)); - cptr = malloc(sizeof(GDevRec)); + GDevPtr cptr = calloc(1, sizeof(GDevRec)); if (!cptr) { free(nscreen); return FALSE; diff --git a/hw/xfree86/common/xf86Configure.c b/hw/xfree86/common/xf86Configure.c index 74936a5bd..96fe64d85 100644 --- a/hw/xfree86/common/xf86Configure.c +++ b/hw/xfree86/common/xf86Configure.c @@ -339,9 +339,7 @@ configureLayoutSection(void) ptr->lay_identifier = "X.org Configured"; { - XF86ConfInputrefPtr iptr; - - iptr = malloc(sizeof(XF86ConfInputrefRec)); + XF86ConfInputrefPtr iptr = calloc(1, sizeof(XF86ConfInputrefRec)); iptr->list.next = NULL; iptr->iref_option_lst = NULL; iptr->iref_inputdev_str = XNFstrdup("Mouse0"); @@ -353,9 +351,7 @@ configureLayoutSection(void) } { - XF86ConfInputrefPtr iptr; - - iptr = malloc(sizeof(XF86ConfInputrefRec)); + XF86ConfInputrefPtr iptr = calloc(1, sizeof(XF86ConfInputrefRec)); iptr->list.next = NULL; iptr->iref_option_lst = NULL; iptr->iref_inputdev_str = XNFstrdup("Keyboard0"); @@ -367,10 +363,9 @@ configureLayoutSection(void) } for (scrnum = 0; scrnum < nDevToConfig; scrnum++) { - XF86ConfAdjacencyPtr aptr; char *tmp; - aptr = malloc(sizeof(XF86ConfAdjacencyRec)); + XF86ConfAdjacencyPtr aptr = calloc(1, sizeof(XF86ConfAdjacencyRec)); aptr->list.next = NULL; aptr->adj_x = 0; aptr->adj_y = 0; diff --git a/hw/xfree86/common/xf86Cursor.c b/hw/xfree86/common/xf86Cursor.c index 025e175fa..d8346bc77 100644 --- a/hw/xfree86/common/xf86Cursor.c +++ b/hw/xfree86/common/xf86Cursor.c @@ -482,7 +482,7 @@ AddEdge(xf86EdgePtr edge, } if (!pEdge) { - if (!(pNew = malloc(sizeof(xf86EdgeRec)))) + if (!(pNew = calloc(1, sizeof(xf86EdgeRec)))) break; pNew->screen = screen; @@ -500,7 +500,7 @@ AddEdge(xf86EdgePtr edge, break; } else if (min < pEdge->start) { - if (!(pNew = malloc(sizeof(xf86EdgeRec)))) + if (!(pNew = calloc(1, sizeof(xf86EdgeRec)))) break; pNew->screen = screen; diff --git a/hw/xfree86/common/xf86DGA.c b/hw/xfree86/common/xf86DGA.c index 2482530a1..de712451f 100644 --- a/hw/xfree86/common/xf86DGA.c +++ b/hw/xfree86/common/xf86DGA.c @@ -146,7 +146,7 @@ DGAInit(ScreenPtr pScreen, DGAFunctionPtr funcs, DGAModePtr modes, int num) pScreenPriv = DGA_GET_SCREEN_PRIV(pScreen); if (!pScreenPriv) { - if (!(pScreenPriv = (DGAScreenPtr) malloc(sizeof(DGAScreenRec)))) + if (!(pScreenPriv = calloc(1, sizeof(DGAScreenRec)))) return FALSE; dixSetPrivate(&pScreen->devPrivates, &DGAScreenKeyRec, pScreenPriv); pScreenPriv->CloseScreen = pScreen->CloseScreen; @@ -400,7 +400,7 @@ xf86SetDGAMode(ScrnInfoPtr pScrn, int num, DGADevicePtr devRet) else return BadValue; - if (!(device = (DGADevicePtr) malloc(sizeof(DGADeviceRec)))) + if (!(device = calloc(1, sizeof(DGADeviceRec)))) return BadAlloc; if (!pScreenPriv->current) { @@ -664,7 +664,7 @@ DGACreateColormap(int index, ClientPtr client, int id, int mode, int alloc) pMode = &(pScreenPriv->modes[mode - 1]); - if (!(pVisual = malloc(sizeof(VisualRec)))) + if (!(pVisual = calloc(1, sizeof(VisualRec)))) return BadAlloc; pVisual->vid = FakeClientID(0); @@ -698,7 +698,7 @@ DGACreateColormap(int index, ClientPtr client, int id, int mode, int alloc) pVisual->offsetBlue = BitsClear(pVisual->blueMask); } - if (!(fvlp = malloc(sizeof(FakedVisualList)))) { + if (!(fvlp = calloc(1, sizeof(FakedVisualList)))) { free(pVisual); return BadAlloc; } @@ -1682,7 +1682,7 @@ ProcXDGASetClientVersion(ClientPtr client) REQUEST_SIZE_MATCH(xXDGASetClientVersionReq); if ((pPriv = DGA_GETPRIV(client)) == NULL) { - pPriv = malloc(sizeof(DGAPrivRec)); + pPriv = calloc(1, sizeof(DGAPrivRec)); /* XXX Need to look into freeing this */ if (!pPriv) return BadAlloc; diff --git a/hw/xfree86/common/xf86Option.c b/hw/xfree86/common/xf86Option.c index b8bab3617..35a2683d9 100644 --- a/hw/xfree86/common/xf86Option.c +++ b/hw/xfree86/common/xf86Option.c @@ -898,13 +898,13 @@ xf86NameCmp(const char *s1, const char *s2) char * xf86NormalizeName(const char *s) { - char *ret, *q; + char *q; const char *p; if (s == NULL) return NULL; - ret = malloc(strlen(s) + 1); + char *ret = calloc(1, strlen(s) + 1); for (p = s, q = ret; *p != 0; p++) { switch (*p) { case '_': diff --git a/hw/xfree86/common/xf86RandR.c b/hw/xfree86/common/xf86RandR.c index 1bd5f8ea7..88dbcd197 100644 --- a/hw/xfree86/common/xf86RandR.c +++ b/hw/xfree86/common/xf86RandR.c @@ -406,7 +406,6 @@ Bool xf86RandRInit(ScreenPtr pScreen) { rrScrPrivPtr rp; - XF86RandRInfoPtr randrp; ScrnInfoPtr scrp = xf86ScreenToScrn(pScreen); #ifdef XINERAMA @@ -420,7 +419,7 @@ xf86RandRInit(ScreenPtr pScreen) if (!dixRegisterPrivateKey(&xf86RandRKeyRec, PRIVATE_SCREEN, 0)) return FALSE; - randrp = malloc(sizeof(XF86RandRInfoRec)); + XF86RandRInfoPtr randrp = calloc(1, sizeof(XF86RandRInfoRec)); if (!randrp) return FALSE; diff --git a/hw/xfree86/common/xf86VGAarbiter.c b/hw/xfree86/common/xf86VGAarbiter.c index 47c561b26..0eb3dc577 100644 --- a/hw/xfree86/common/xf86VGAarbiter.c +++ b/hw/xfree86/common/xf86VGAarbiter.c @@ -184,7 +184,7 @@ xf86VGAarbiterWrapFunctions(void) if (!dixRegisterPrivateKey(&VGAarbiterScreenKeyRec, PRIVATE_SCREEN, 0)) return FALSE; - if (!(pScreenPriv = malloc(sizeof(VGAarbiterScreenRec)))) + if (!(pScreenPriv = calloc(1, sizeof(VGAarbiterScreenRec)))) return FALSE; dixSetPrivate(&pScreen->devPrivates, VGAarbiterScreenKey, pScreenPriv); diff --git a/hw/xfree86/common/xf86cmap.c b/hw/xfree86/common/xf86cmap.c index 6d5f9ac90..5ac8a49d7 100644 --- a/hw/xfree86/common/xf86cmap.c +++ b/hw/xfree86/common/xf86cmap.c @@ -168,7 +168,7 @@ xf86HandleColormaps(ScreenPtr pScreen, return FALSE; } - if (!(pScreenPriv = malloc(sizeof(CMapScreenRec)))) { + if (!(pScreenPriv = calloc(1, sizeof(CMapScreenRec)))) { free(gamma); free(indices); return FALSE; @@ -264,7 +264,6 @@ CMapAllocateColormapPrivate(ColormapPtr pmap) (CMapScreenPtr) dixLookupPrivate(&pmap->pScreen->devPrivates, CMapScreenKey); CMapColormapPtr pColPriv; - CMapLinkPtr pLink; int numColors; LOCO *colors; @@ -276,7 +275,7 @@ CMapAllocateColormapPrivate(ColormapPtr pmap) if (!(colors = xallocarray(numColors, sizeof(LOCO)))) return FALSE; - if (!(pColPriv = malloc(sizeof(CMapColormapRec)))) { + if (!(pColPriv = calloc(1, sizeof(CMapColormapRec)))) { free(colors); return FALSE; } @@ -289,7 +288,7 @@ CMapAllocateColormapPrivate(ColormapPtr pmap) pColPriv->overscan = -1; /* add map to list */ - pLink = malloc(sizeof(CMapLink)); + CMapLinkPtr pLink = calloc(1, sizeof(CMapLink)); if (pLink) { pLink->cmap = pmap; pLink->next = pScreenPriv->maps; diff --git a/hw/xfree86/common/xf86fbman.c b/hw/xfree86/common/xf86fbman.c index 25779e510..5e13dfc24 100644 --- a/hw/xfree86/common/xf86fbman.c +++ b/hw/xfree86/common/xf86fbman.c @@ -371,7 +371,7 @@ AllocateArea(FBManagerPtr offman, if (((boxp->y2 - boxp->y1) < h) || ((boxp->x2 - x) < w)) continue; - link = malloc(sizeof(FBLink)); + link = calloc(1, sizeof(FBLink)); if (!link) return NULL; @@ -829,7 +829,6 @@ AllocateLinear(FBManagerPtr offman, int size, int granularity, void *privData) { ScreenPtr pScreen = offman->pScreen; FBLinearLinkPtr linear = NULL; - FBLinearLinkPtr newlink = NULL; int offset, end; if (size <= 0) @@ -857,7 +856,7 @@ AllocateLinear(FBManagerPtr offman, int size, int granularity, void *privData) /* break left */ if (offset > linear->linear.offset) { - newlink = malloc(sizeof(FBLinearLink)); + FBLinearLinkPtr newlink = calloc(1, sizeof(FBLinearLink)); if (!newlink) return NULL; newlink->area = NULL; @@ -873,7 +872,7 @@ AllocateLinear(FBManagerPtr offman, int size, int granularity, void *privData) /* break right */ if (size < linear->linear.size) { - newlink = malloc(sizeof(FBLinearLink)); + FBLinearLinkPtr newlink = calloc(1, sizeof(FBLinearLink)); if (!newlink) return NULL; newlink->area = NULL; @@ -923,7 +922,7 @@ localAllocateOffscreenLinear(ScreenPtr pScreen, DebugF("NOPE, ALLOCATING AREA\n"); - if (!(link = malloc(sizeof(FBLinearLink)))) + if (!(link = calloc(1, sizeof(FBLinearLink)))) return NULL; /* No linear available, so try and pinch some from the XY areas */ @@ -1309,7 +1308,6 @@ xf86InitFBManagerArea(ScreenPtr pScreen, int PixelArea, int Verbosity) Bool xf86InitFBManagerRegion(ScreenPtr pScreen, RegionPtr FullRegion) { - FBManagerPtr offman; if (RegionNil(FullRegion)) return FALSE; @@ -1320,7 +1318,7 @@ xf86InitFBManagerRegion(ScreenPtr pScreen, RegionPtr FullRegion) if (!xf86RegisterOffscreenManager(pScreen, &xf86FBManFuncs)) return FALSE; - offman = malloc(sizeof(FBManager)); + FBManagerPtr offman = calloc(1, sizeof(FBManager)); if (!offman) return FALSE; @@ -1362,7 +1360,7 @@ xf86InitFBManagerLinear(ScreenPtr pScreen, int offset, int size) offman = (FBManagerPtr) dixLookupPrivate(&pScreen->devPrivates, xf86FBScreenKey); - offman->LinearAreas = malloc(sizeof(FBLinearLink)); + offman->LinearAreas = calloc(1, sizeof(FBLinearLink)); if (!offman->LinearAreas) return FALSE; diff --git a/hw/xfree86/common/xf86pciBus.c b/hw/xfree86/common/xf86pciBus.c index 35fb8b0a9..896224e96 100644 --- a/hw/xfree86/common/xf86pciBus.c +++ b/hw/xfree86/common/xf86pciBus.c @@ -1358,7 +1358,7 @@ xf86MatchDriverFromFiles(uint16_t match_vendor, uint16_t match_chip, } if (vendor == match_vendor && chip == match_chip) { tmpMatch = - (char *) malloc(sizeof(char) * + (char *) calloc(1, sizeof(char) * strlen(direntry->d_name) - 3); if (!tmpMatch) { LogMessageVerb(X_ERROR, 1, diff --git a/hw/xfree86/common/xf86sbusBus.c b/hw/xfree86/common/xf86sbusBus.c index 3beb17dfc..c68c79c38 100644 --- a/hw/xfree86/common/xf86sbusBus.c +++ b/hw/xfree86/common/xf86sbusBus.c @@ -86,7 +86,7 @@ xf86SbusProbe(void) char fbDevName[32]; sbusDevicePtr psdp, *psdpp; - xf86SbusInfo = malloc(sizeof(psdp)); + xf86SbusInfo = calloc(1, sizeof(psdp)); *xf86SbusInfo = NULL; for (i = 0; i < 32; i++) { snprintf(fbDevName, sizeof(fbDevName), "/dev/fb%d", i); diff --git a/hw/xfree86/common/xf86xv.c b/hw/xfree86/common/xf86xv.c index 35eab722c..a6df4e920 100644 --- a/hw/xfree86/common/xf86xv.c +++ b/hw/xfree86/common/xf86xv.c @@ -245,7 +245,7 @@ xf86XVScreenInit(ScreenPtr pScreen, XF86VideoAdaptorPtr * adaptors, int num) PortResource = XvGetRTPort(); - ScreenPriv = malloc(sizeof(XF86XVScreenRec)); + ScreenPriv = calloc(1, sizeof(XF86XVScreenRec)); dixSetPrivate(&pScreen->devPrivates, &XF86XVScreenPrivateKey, ScreenPriv); if (!ScreenPriv) diff --git a/hw/xfree86/common/xf86xvmc.c b/hw/xfree86/common/xf86xvmc.c index c67418bf4..d22011a61 100644 --- a/hw/xfree86/common/xf86xvmc.c +++ b/hw/xfree86/common/xf86xvmc.c @@ -163,7 +163,7 @@ xf86XvMCScreenInit(ScreenPtr pScreen, return FALSE; } - if (!(pScreenPriv = malloc(sizeof(xf86XvMCScreenRec)))) { + if (!(pScreenPriv = calloc(1, sizeof(xf86XvMCScreenRec)))) { free(pAdapt); return FALSE; } diff --git a/hw/xfree86/common/xisb.c b/hw/xfree86/common/xisb.c index f35a6825a..78a31f8ef 100644 --- a/hw/xfree86/common/xisb.c +++ b/hw/xfree86/common/xisb.c @@ -64,12 +64,10 @@ XISBuffer * XisbNew(int fd, ssize_t size) { - XISBuffer *b; - - b = malloc(sizeof(XISBuffer)); + XISBuffer *b = calloc(1, sizeof(XISBuffer)); if (!b) return NULL; - b->buf = malloc((sizeof(unsigned char) * size)); + b->buf = calloc(sizeof(unsigned char), size); if (!b->buf) { free(b); return NULL; diff --git a/hw/xfree86/ddc/ddc.c b/hw/xfree86/ddc/ddc.c index effeff6fc..1a6f25f3a 100644 --- a/hw/xfree86/ddc/ddc.c +++ b/hw/xfree86/ddc/ddc.c @@ -90,7 +90,7 @@ find_header(unsigned char *block) static unsigned char * resort(unsigned char *s_block) { - unsigned char *d_new, *d_ptr, *d_end, *s_ptr, *s_end; + unsigned char *d_ptr, *d_end, *s_ptr, *s_end; unsigned char tmp; s_ptr = find_header(s_block); @@ -98,7 +98,7 @@ resort(unsigned char *s_block) return NULL; s_end = s_block + EDID1_LEN; - d_new = malloc(EDID1_LEN); + unsigned char *d_new = calloc(1, EDID1_LEN); if (!d_new) return NULL; d_end = d_new + EDID1_LEN; @@ -186,7 +186,7 @@ FetchEDID_DDC1(register ScrnInfoPtr pScrn, int count = NUM; unsigned int *ptr, *xp; - ptr = xp = malloc(sizeof(int) * NUM); + ptr = xp = calloc(NUM, sizeof(int)); if (!ptr) return NULL; @@ -413,9 +413,7 @@ xf86DoEEDID(ScrnInfoPtr pScrn, I2CBusPtr pBus, Bool complete) /* Default DDC and DDC2 to enabled. */ Bool noddc = FALSE, noddc2 = FALSE; - OptionInfoPtr options; - - options = malloc(sizeof(DDCOptions)); + OptionInfoPtr options = calloc(1, sizeof(DDCOptions)); if (!options) return NULL; memcpy(options, DDCOptions, sizeof(DDCOptions)); diff --git a/hw/xfree86/dri/dri.c b/hw/xfree86/dri/dri.c index 2f584a654..0015a24b9 100644 --- a/hw/xfree86/dri/dri.c +++ b/hw/xfree86/dri/dri.c @@ -1285,7 +1285,7 @@ DRICreateDrawable(ScreenPtr pScreen, ClientPtr client, DrawablePtr pDrawable, } else { /* allocate a DRI Window Private record */ - if (!(pDRIDrawablePriv = malloc(sizeof(DRIDrawablePrivRec)))) { + if (!(pDRIDrawablePriv = calloc(1, sizeof(DRIDrawablePrivRec)))) { return FALSE; } diff --git a/hw/xfree86/dri2/dri2.c b/hw/xfree86/dri2/dri2.c index 4ac0e144e..fb10f9fd9 100644 --- a/hw/xfree86/dri2/dri2.c +++ b/hw/xfree86/dri2/dri2.c @@ -235,12 +235,11 @@ static DRI2DrawablePtr DRI2AllocateDrawable(DrawablePtr pDraw) { DRI2ScreenPtr ds = DRI2GetScreen(pDraw->pScreen); - DRI2DrawablePtr pPriv; CARD64 ust; WindowPtr pWin; PixmapPtr pPixmap; - pPriv = malloc(sizeof *pPriv); + DRI2DrawablePtr pPriv = calloc(1, sizeof *pPriv); if (pPriv == NULL) return NULL; @@ -327,9 +326,7 @@ static int DRI2AddDrawableRef(DRI2DrawablePtr pPriv, XID id, XID dri2_id, DRI2InvalidateProcPtr invalidate, void *priv) { - DRI2DrawableRefPtr ref; - - ref = malloc(sizeof *ref); + DRI2DrawableRefPtr ref = calloc(1, sizeof *ref); if (ref == NULL) return BadAlloc; diff --git a/hw/xfree86/drivers/modesetting/dri2.c b/hw/xfree86/drivers/modesetting/dri2.c index 68765cad8..60fe73a15 100644 --- a/hw/xfree86/drivers/modesetting/dri2.c +++ b/hw/xfree86/drivers/modesetting/dri2.c @@ -88,7 +88,6 @@ struct ms_dri2_resource { static struct ms_dri2_resource * ms_get_resource(XID id, RESTYPE type) { - struct ms_dri2_resource *resource; void *ptr; ptr = NULL; @@ -96,7 +95,7 @@ ms_get_resource(XID id, RESTYPE type) if (ptr) return ptr; - resource = malloc(sizeof(*resource)); + struct ms_dri2_resource *resource = calloc(1, sizeof(*resource)); if (resource == NULL) return NULL; diff --git a/hw/xfree86/drivers/modesetting/driver.c b/hw/xfree86/drivers/modesetting/driver.c index fe78111e8..e07c6cd74 100644 --- a/hw/xfree86/drivers/modesetting/driver.c +++ b/hw/xfree86/drivers/modesetting/driver.c @@ -1327,7 +1327,7 @@ PreInit(ScrnInfoPtr pScrn, int flags) /* Process the options */ xf86CollectOptions(pScrn, NULL); - if (!(ms->drmmode.Options = malloc(sizeof(Options)))) + if (!(ms->drmmode.Options = calloc(1, sizeof(Options)))) return FALSE; memcpy(ms->drmmode.Options, Options, sizeof(Options)); xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, ms->drmmode.Options); diff --git a/hw/xfree86/drivers/modesetting/drmmode_display.c b/hw/xfree86/drivers/modesetting/drmmode_display.c index cf4269403..c6dd5ad78 100644 --- a/hw/xfree86/drivers/modesetting/drmmode_display.c +++ b/hw/xfree86/drivers/modesetting/drmmode_display.c @@ -410,7 +410,7 @@ drmmode_prop_info_copy(drmmode_prop_info_ptr dst, continue; dst[i].enum_values = - malloc(src[i].num_enum_values * + calloc(src[i].num_enum_values, sizeof(*dst[i].enum_values)); if (!dst[i].enum_values) goto err; @@ -4121,7 +4121,7 @@ drmmode_crtc_upgrade_lut(xf86CrtcPtr crtc, int num) if (size != crtc->gamma_size) { ScrnInfoPtr pScrn = crtc->scrn; - uint16_t *gamma = malloc(3 * size * sizeof(uint16_t)); + uint16_t *gamma = calloc(3 * size, sizeof(uint16_t)); if (gamma) { free(crtc->gamma_red); diff --git a/hw/xfree86/int10/helper_mem.c b/hw/xfree86/int10/helper_mem.c index 7bd27a3c1..78d377acb 100644 --- a/hw/xfree86/int10/helper_mem.c +++ b/hw/xfree86/int10/helper_mem.c @@ -207,7 +207,7 @@ xf86HandleInt10Options(ScrnInfoPtr pScrn, int entityIndex) configOptions = pEnt->device->options; if (configOptions) { - if (!(options = (OptionInfoPtr) malloc(sizeof(INT10Options)))) + if (!(options = (OptionInfoPtr) calloc(1, sizeof(INT10Options)))) return NULL; (void) memcpy(options, INT10Options, sizeof(INT10Options)); diff --git a/hw/xfree86/int10/vbe.c b/hw/xfree86/int10/vbe.c index a4f3af58c..86e0d15ea 100644 --- a/hw/xfree86/int10/vbe.c +++ b/hw/xfree86/int10/vbe.c @@ -505,8 +505,6 @@ VBEGetVBEMode(vbeInfoPtr pVbe, int *mode) VbeModeInfoBlock * VBEGetModeInfo(vbeInfoPtr pVbe, int mode) { - VbeModeInfoBlock *block = NULL; - memset(pVbe->memory, 0, sizeof(VbeModeInfoBlock)); /* @@ -530,7 +528,7 @@ VBEGetModeInfo(vbeInfoPtr pVbe, int mode) if (R16(pVbe->pInt10->ax) != 0x4f) return NULL; - block = malloc(sizeof(VbeModeInfoBlock)); + VbeModeInfoBlock *block = calloc(1, sizeof(VbeModeInfoBlock)); if (block) memcpy(block, pVbe->memory, sizeof(*block)); @@ -834,7 +832,6 @@ VBESetGetPaletteData(vbeInfoPtr pVbe, Bool set, int first, int num, VBEpmi * VBEGetVBEpmi(vbeInfoPtr pVbe) { - VBEpmi *pmi; /* Input: @@ -859,7 +856,7 @@ VBEGetVBEpmi(vbeInfoPtr pVbe) if (R16(pVbe->pInt10->ax) != 0x4f) return NULL; - pmi = malloc(sizeof(VBEpmi)); + VBEpmi *pmi = calloc(1, sizeof(VBEpmi)); pmi->seg_tbl = R16(pVbe->pInt10->es); pmi->tbl_off = R16(pVbe->pInt10->di); pmi->tbl_len = R16(pVbe->pInt10->cx); @@ -936,7 +933,7 @@ VBEVesaSaveRestore(vbeInfoPtr pVbe, vbeSaveRestorePtr vbe_sr, vbe_sr->stateMode = -1; /* invalidate */ /* don't rely on the memory not being touched */ if (vbe_sr->pstate == NULL) - vbe_sr->pstate = malloc(vbe_sr->stateSize); + vbe_sr->pstate = calloc(1, vbe_sr->stateSize); memcpy(vbe_sr->pstate, vbe_sr->state, vbe_sr->stateSize); } ErrorF("VBESaveRestore done with success\n"); diff --git a/hw/xfree86/loader/loadmod.c b/hw/xfree86/loader/loadmod.c index 2edc14ac7..ebc4deaa7 100644 --- a/hw/xfree86/loader/loadmod.c +++ b/hw/xfree86/loader/loadmod.c @@ -142,7 +142,7 @@ InitPathList(const char *path) free(fullpath); return NULL; } - list[n] = malloc(len + 1); + list[n] = calloc(1, len + 1); if (!list[n]) { FreeStringList(list); free(fullpath); @@ -398,7 +398,7 @@ LoaderListDir(const char *subdir, const char **patternlist) closedir(d); goto bail; } - listing[n] = malloc(len + 1); + listing[n] = calloc(1, len + 1); if (!listing[n]) { FreeStringList(listing); closedir(d); @@ -723,7 +723,7 @@ LoadModule(const char *module, void *options, const XF86ModReqInfo *modreq, pathlist = defaultPathList; if (!pathlist) { - /* This could be a malloc failure too */ + /* This could be a calloc failure too */ if (errmaj) *errmaj = LDR_BADUSAGE; goto LoadModule_fail; @@ -962,7 +962,6 @@ LoaderErrorMsg(const char *name, const char *modname, int errmaj, int errmin) static char * LoaderGetCanonicalName(const char *modname, PatternPtr patterns) { - char *str; const char *s; int len; PatternPtr p; @@ -979,7 +978,7 @@ LoaderGetCanonicalName(const char *modname, PatternPtr patterns) for (p = patterns; p->pattern; p++) if (regexec(&p->rex, s, 2, match, 0) == 0 && match[1].rm_so != -1) { len = match[1].rm_eo - match[1].rm_so; - str = malloc(len + 1); + char *str = calloc(1, len + 1); if (!str) return NULL; strncpy(str, s + match[1].rm_so, len); diff --git a/hw/xfree86/modes/xf86Cursors.c b/hw/xfree86/modes/xf86Cursors.c index adeecec06..8202ad5f7 100644 --- a/hw/xfree86/modes/xf86Cursors.c +++ b/hw/xfree86/modes/xf86Cursors.c @@ -649,7 +649,7 @@ xf86_cursors_init(ScreenPtr screen, int max_width, int max_height, int flags) if (!cursor_info) return FALSE; - xf86_config->cursor_image = malloc(max_width * max_height * 4); + xf86_config->cursor_image = calloc(max_width * max_height, 4); if (!xf86_config->cursor_image) { xf86DestroyCursorInfoRec(cursor_info); diff --git a/hw/xfree86/modes/xf86RandR12.c b/hw/xfree86/modes/xf86RandR12.c index ddcf5e748..6fa47af8c 100644 --- a/hw/xfree86/modes/xf86RandR12.c +++ b/hw/xfree86/modes/xf86RandR12.c @@ -847,7 +847,6 @@ Bool xf86RandR12Init(ScreenPtr pScreen) { rrScrPrivPtr rp; - XF86RandRInfoPtr randrp; #ifdef XINERAMA /* XXX disable RandR when using Xinerama */ @@ -865,7 +864,7 @@ xf86RandR12Init(ScreenPtr pScreen) if (!dixRegisterPrivateKey(&xf86RandR12KeyRec, PRIVATE_SCREEN, 0)) return FALSE; - randrp = malloc(sizeof(XF86RandRInfoRec)); + XF86RandRInfoPtr randrp = calloc(1, sizeof(XF86RandRInfoRec)); if (!randrp) return FALSE; diff --git a/hw/xfree86/modes/xf86Rotate.c b/hw/xfree86/modes/xf86Rotate.c index da0aa7635..2dc7a8d7d 100644 --- a/hw/xfree86/modes/xf86Rotate.c +++ b/hw/xfree86/modes/xf86Rotate.c @@ -457,7 +457,7 @@ xf86CrtcRotate(xf86CrtcPtr crtc) #ifdef RANDR_12_INTERFACE if (transform) { if (transform->nparams) { - new_params = malloc(transform->nparams * sizeof(xFixed)); + new_params = calloc(transform->nparams, sizeof(xFixed)); if (new_params) { memcpy(new_params, transform->params, transform->nparams * sizeof(xFixed)); diff --git a/hw/xfree86/os-support/bus/Sbus.c b/hw/xfree86/os-support/bus/Sbus.c index e27a13dfa..596d0ac75 100644 --- a/hw/xfree86/os-support/bus/Sbus.c +++ b/hw/xfree86/os-support/bus/Sbus.c @@ -213,7 +213,7 @@ sparcPromInit(void) promFd = open("/dev/openprom", O_RDONLY, 0); if (promFd == -1) return -1; - promOpio = (struct openpromio *) malloc(4096); + promOpio = (struct openpromio *) calloc(1, 4096); if (!promOpio) { sparcPromClose(); return -1; @@ -535,11 +535,9 @@ promWalkNode2Pathname(char *path, int parent, int node, int searchNode, char * sparcPromNode2Pathname(sbusPromNodePtr pnode) { - char *ret; - if (!pnode->node) return NULL; - ret = malloc(4096); + char *ret = calloc(1, 4096); if (!ret) return NULL; if (promWalkNode2Pathname @@ -609,10 +607,10 @@ int sparcPromPathname2Node(const char *pathName) { int i; - char *name, *regstr, *p; + char *regstr, *p; i = strlen(pathName); - name = malloc(i + 2); + char *name = calloc(1, i + 2); if (!name) return 0; strcpy(name, pathName); diff --git a/hw/xfree86/parser/Files.c b/hw/xfree86/parser/Files.c index fba99a864..606244675 100644 --- a/hw/xfree86/parser/Files.c +++ b/hw/xfree86/parser/Files.c @@ -121,7 +121,7 @@ xf86parseFilesSection(void) l = FALSE; str = xf86_lex_val.str; if (ptr->file_modulepath == NULL) { - ptr->file_modulepath = malloc(1); + ptr->file_modulepath = calloc(1, 1); ptr->file_modulepath[0] = '\0'; k = strlen(str) + 1; } diff --git a/hw/xfree86/parser/InputClass.c b/hw/xfree86/parser/InputClass.c index 8b9510c0b..004b91a1d 100644 --- a/hw/xfree86/parser/InputClass.c +++ b/hw/xfree86/parser/InputClass.c @@ -155,9 +155,7 @@ enum MatchType { static void add_group_entry(struct xorg_list *head, char **values, enum MatchType type) { - xf86MatchGroup *group; - - group = malloc(sizeof(*group)); + xf86MatchGroup *group = calloc(1, sizeof(xf86MatchGroup)); if (group) { group->is_negated = (type == MATCH_NEGATED); group->values = values; diff --git a/hw/xfree86/parser/OutputClass.c b/hw/xfree86/parser/OutputClass.c index 4c5340a03..e771baf34 100644 --- a/hw/xfree86/parser/OutputClass.c +++ b/hw/xfree86/parser/OutputClass.c @@ -78,9 +78,7 @@ xf86freeOutputClassList(XF86ConfOutputClassPtr ptr) static void add_group_entry(struct xorg_list *head, char **values) { - xf86MatchGroup *group; - - group = malloc(sizeof(*group)); + xf86MatchGroup *group = calloc(1, sizeof(xf86MatchGroup)); if (group) { group->values = values; xorg_list_add(&group->entry, head); diff --git a/hw/xfree86/parser/scan.c b/hw/xfree86/parser/scan.c index 471e3adb3..648b9db0e 100644 --- a/hw/xfree86/parser/scan.c +++ b/hw/xfree86/parser/scan.c @@ -118,20 +118,20 @@ xf86getNextLine(void) /* * reallocate the string if it was grown last time (i.e., is no - * longer CONFIG_BUF_LEN); we malloc the new strings first, so - * that if either of the mallocs fail, we can fall back on the + * longer CONFIG_BUF_LEN); we calloc the new strings first, so + * that if either of the callocs fail, we can fall back on the * existing buffer allocations */ if (configBufLen != CONFIG_BUF_LEN) { - tmpConfigBuf = malloc(CONFIG_BUF_LEN); - tmpConfigRBuf = malloc(CONFIG_BUF_LEN); + tmpConfigBuf = calloc(1, CONFIG_BUF_LEN); + tmpConfigRBuf = calloc(1, CONFIG_BUF_LEN); if (!tmpConfigBuf || !tmpConfigRBuf) { /* - * at least one of the mallocs failed; keep the old buffers + * at least one of the callocs failed; keep the old buffers * and free any partial allocations */ @@ -142,7 +142,7 @@ xf86getNextLine(void) else { /* - * malloc succeeded; free the old buffers and use the new + * calloc succeeded; free the old buffers and use the new * buffers */ @@ -393,7 +393,7 @@ xf86getToken(const xf86ConfigSymTabRec * tab) } while ((c != '\"') && (c != '\n') && (c != '\r') && (c != '\0')); configRBuf[i] = '\0'; - xf86_lex_val.str = malloc(strlen(configRBuf) + 1); + xf86_lex_val.str = calloc(1, strlen(configRBuf) + 1); strcpy(xf86_lex_val.str, configRBuf); /* private copy ! */ return STRING; } @@ -569,7 +569,6 @@ static char * DoSubstitution(const char *template, const char *cmdline, const char *projroot, int *cmdlineUsed, int *envUsed, const char *XConfigFile) { - char *result; int i, l; static const char *env = NULL; static char *hostname = NULL; @@ -582,7 +581,7 @@ DoSubstitution(const char *template, const char *cmdline, const char *projroot, if (envUsed) *envUsed = 0; - result = malloc(PATH_MAX + 1); + char *result = calloc(1, PATH_MAX + 1); l = 0; for (i = 0; template[i]; i++) { if (template[i] != '%') { @@ -623,7 +622,7 @@ DoSubstitution(const char *template, const char *cmdline, const char *projroot, break; case 'H': if (!hostname) { - if ((hostname = malloc(MAXHOSTNAMELEN + 1))) { + if ((hostname = calloc(1, MAXHOSTNAMELEN + 1))) { if (gethostname(hostname, MAXHOSTNAMELEN) == 0) { hostname[MAXHOSTNAMELEN] = '\0'; } @@ -769,7 +768,6 @@ AddConfigDirFiles(const char *dirpath, struct dirent **list, int num) Bool warnOnce = FALSE; for (i = 0; i < num; i++) { - char *path; FILE *file; if (numFiles >= CONFIG_MAX_FILES) { @@ -780,7 +778,7 @@ AddConfigDirFiles(const char *dirpath, struct dirent **list, int num) continue; } - path = malloc(PATH_MAX + 1); + char *path = calloc(1, PATH_MAX + 1); snprintf(path, PATH_MAX + 1, "%s/%s", dirpath, list[i]->d_name); file = fopen(path, "r"); if (!file) { @@ -857,8 +855,8 @@ xf86initConfigFiles(void) configLineNo = 0; pushToken = LOCK_TOKEN; - configBuf = malloc(CONFIG_BUF_LEN); - configRBuf = malloc(CONFIG_BUF_LEN); + configBuf = calloc(1, CONFIG_BUF_LEN); + configRBuf = calloc(1, CONFIG_BUF_LEN); configBuf[0] = '\0'; /* sanity ... */ } diff --git a/hw/xfree86/shadowfb/shadowfb.c b/hw/xfree86/shadowfb/shadowfb.c index d2481ed8a..96ed0b82d 100644 --- a/hw/xfree86/shadowfb/shadowfb.c +++ b/hw/xfree86/shadowfb/shadowfb.c @@ -61,7 +61,7 @@ ShadowFBInit2(ScreenPtr pScreen, if (!dixRegisterPrivateKey(&ShadowScreenKeyRec, PRIVATE_SCREEN, 0)) return FALSE; - if (!(pPriv = (ShadowScreenPtr) malloc(sizeof(ShadowScreenRec)))) + if (!(pPriv = (ShadowScreenPtr) calloc(1, sizeof(ShadowScreenRec)))) return FALSE; dixSetPrivate(&pScreen->devPrivates, &ShadowScreenKeyRec, pPriv); diff --git a/hw/xfree86/utils/gtf/gtf.c b/hw/xfree86/utils/gtf/gtf.c index 818ee0fd7..7830f1abd 100644 --- a/hw/xfree86/utils/gtf/gtf.c +++ b/hw/xfree86/utils/gtf/gtf.c @@ -294,7 +294,7 @@ vert_refresh(int h_pixels, int v_lines, float freq, int interlaced, int margins) float h_front_porch; float v_odd_front_porch_lines; - mode *m = (mode *) malloc(sizeof(mode)); + mode *m = (mode *) calloc(1, sizeof(mode)); /* 1. In order to give correct results, the number of horizontal * pixels requested is first processed to ensure that it is divisible diff --git a/hw/xfree86/vgahw/vgaHW.c b/hw/xfree86/vgahw/vgaHW.c index 215bc93f9..ec5676332 100644 --- a/hw/xfree86/vgahw/vgaHW.c +++ b/hw/xfree86/vgahw/vgaHW.c @@ -952,21 +952,21 @@ vgaHWSaveFonts(ScrnInfoPtr scrninfp, vgaRegPtr save) hwp->writeGr(hwp, 0x06, 0x05); /* set graphics */ #if SAVE_FONT1 - if (hwp->FontInfo1 || (hwp->FontInfo1 = malloc(FONT_AMOUNT))) { + if (hwp->FontInfo1 || (hwp->FontInfo1 = calloc(1, FONT_AMOUNT))) { hwp->writeSeq(hwp, 0x02, 0x04); /* write to plane 2 */ hwp->writeGr(hwp, 0x04, 0x02); /* read plane 2 */ slowbcopy_frombus(hwp->Base, hwp->FontInfo1, FONT_AMOUNT); } #endif /* SAVE_FONT1 */ #if SAVE_FONT2 - if (hwp->FontInfo2 || (hwp->FontInfo2 = malloc(FONT_AMOUNT))) { + if (hwp->FontInfo2 || (hwp->FontInfo2 = calloc(1, FONT_AMOUNT))) { hwp->writeSeq(hwp, 0x02, 0x08); /* write to plane 3 */ hwp->writeGr(hwp, 0x04, 0x03); /* read plane 3 */ slowbcopy_frombus(hwp->Base, hwp->FontInfo2, FONT_AMOUNT); } #endif /* SAVE_FONT2 */ #if SAVE_TEXT - if (hwp->TextInfo || (hwp->TextInfo = malloc(2 * TEXT_AMOUNT))) { + if (hwp->TextInfo || (hwp->TextInfo = calloc(2, TEXT_AMOUNT))) { hwp->writeSeq(hwp, 0x02, 0x01); /* write to plane 0 */ hwp->writeGr(hwp, 0x04, 0x00); /* read plane 0 */ slowbcopy_frombus(hwp->Base, hwp->TextInfo, TEXT_AMOUNT);