From 36c7ca02911cda3b5cf161ce03c96187be0b3256 Mon Sep 17 00:00:00 2001 From: Daniel Kurtz Date: Wed, 29 Jan 2014 16:26:31 +0100 Subject: Fix malloc/calloc/free usage . Use calloc when allocating objects. . Use malloc when allocating a buffer that we are about to completely fill. . In both cases, NULL check the result, unless using X built-in "kill server on alloc fail" versions (e.g., xnfcalloc()) . The canonical way to call calloc is: ptr = calloc(nelem, sizeof *ptr); /* or sizeof(*ptr) */ if (ptr) { /* Handle OOM */ } . Don't NULL check before calling free(). BUG=none TEST=compiles clean; sanity check ui on device (cherry picked from commit 79795728e366a56013aa4affa2d42128441a5eb9) From: https://chromium.googlesource.com/chromiumos/third_party/xf86-video-armsoc Change-Id: I18b6a6c7c173c0120ec6da948b1e55cbe96e1e1d --- src/armsoc_dri2.c | 6 ++++-- src/armsoc_driver.c | 4 ++-- src/armsoc_dumb.c | 2 +- src/armsoc_exa.c | 3 +-- src/armsoc_exa_null.c | 21 ++++++++++----------- src/drmmode_display.c | 15 +++++++-------- 6 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/armsoc_dri2.c b/src/armsoc_dri2.c index a9700d7..f79ac51 100644 --- a/src/armsoc_dri2.c +++ b/src/armsoc_dri2.c @@ -164,7 +164,7 @@ ARMSOCDRI2CreateBuffer(DrawablePtr pDraw, unsigned int attachment, { ScreenPtr pScreen = pDraw->pScreen; ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen); - struct ARMSOCDRI2BufferRec *buf = calloc(1, sizeof(*buf)); + struct ARMSOCDRI2BufferRec *buf; struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn); PixmapPtr pPixmap = NULL; struct armsoc_bo *bo; @@ -173,6 +173,7 @@ ARMSOCDRI2CreateBuffer(DrawablePtr pDraw, unsigned int attachment, DEBUG_MSG("pDraw=%p, attachment=%d, format=%08x", pDraw, attachment, format); + buf = calloc(1, sizeof *buf); if (!buf) { ERROR_MSG("Couldn't allocate internal buffer structure"); return NULL; @@ -602,11 +603,12 @@ ARMSOCDRI2ScheduleSwap(ClientPtr client, DrawablePtr pDraw, struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn); struct ARMSOCDRI2BufferRec *src = ARMSOCBUF(pSrcBuffer); struct ARMSOCDRI2BufferRec *dst = ARMSOCBUF(pDstBuffer); - struct ARMSOCDRISwapCmd *cmd = calloc(1, sizeof(*cmd)); + struct ARMSOCDRISwapCmd *cmd; struct armsoc_bo *src_bo, *dst_bo; int src_fb_id, dst_fb_id; int new_canflip, ret, do_flip; + cmd = calloc(1, sizeof(*cmd)); if (!cmd) return FALSE; diff --git a/src/armsoc_driver.c b/src/armsoc_driver.c index 329e11d..590352e 100644 --- a/src/armsoc_driver.c +++ b/src/armsoc_driver.c @@ -663,7 +663,7 @@ ARMSOCProbe(DriverPtr drv, int flags) * driverPrivate field. */ pScrn->driverPrivate = - calloc(1, sizeof(struct ARMSOCRec)); + calloc(1, sizeof *pARMSOC); if (!pScrn->driverPrivate) return FALSE; @@ -823,7 +823,7 @@ ARMSOCPreInit(ScrnInfoPtr pScrn, int flags) * Process the "xorg.conf" file options: */ xf86CollectOptions(pScrn, NULL); - pARMSOC->pOptionInfo = calloc(1, sizeof(ARMSOCOptions)); + pARMSOC->pOptionInfo = malloc(sizeof(ARMSOCOptions)); if (!pARMSOC->pOptionInfo) goto fail2; diff --git a/src/armsoc_dumb.c b/src/armsoc_dumb.c index c232251..f27728c 100644 --- a/src/armsoc_dumb.c +++ b/src/armsoc_dumb.c @@ -68,7 +68,7 @@ struct armsoc_device *armsoc_device_new(int fd, int (*create_custom_gem)(int fd, struct armsoc_create_gem *create_gem)) { - struct armsoc_device *new_dev = malloc(sizeof(*new_dev)); + struct armsoc_device *new_dev = calloc(1, sizeof *new_dev); if (!new_dev) return NULL; diff --git a/src/armsoc_exa.c b/src/armsoc_exa.c index 57e2a6d..79c8970 100644 --- a/src/armsoc_exa.c +++ b/src/armsoc_exa.c @@ -78,8 +78,7 @@ ARMSOCCreatePixmap2(ScreenPtr pScreen, int width, int height, int depth, int usage_hint, int bitsPerPixel, int *new_fb_pitch) { - struct ARMSOCPixmapPrivRec *priv = - calloc(sizeof(struct ARMSOCPixmapPrivRec), 1); + struct ARMSOCPixmapPrivRec *priv = calloc(1, sizeof *priv); ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen); struct ARMSOCRec *pARMSOC = ARMSOCPTR(pScrn); enum armsoc_buf_type buf_type = ARMSOC_BO_NON_SCANOUT; diff --git a/src/armsoc_exa_null.c b/src/armsoc_exa_null.c index 782694a..17f5dd0 100644 --- a/src/armsoc_exa_null.c +++ b/src/armsoc_exa_null.c @@ -107,20 +107,21 @@ FreeScreen(FREE_SCREEN_ARGS_DECL) struct ARMSOCEXARec * InitNullEXA(ScreenPtr pScreen, ScrnInfoPtr pScrn, int fd) { - struct ARMSOCNullEXARec *null_exa = calloc(sizeof(*null_exa), 1); + struct ARMSOCNullEXARec *null_exa; struct ARMSOCEXARec *armsoc_exa; ExaDriverPtr exa; INFO_MSG("Soft EXA mode"); + null_exa = calloc(1, sizeof(*null_exa)); if (!null_exa) - return NULL; + goto out; armsoc_exa = (struct ARMSOCEXARec *)null_exa; exa = exaDriverAlloc(); if (!exa) - goto fail; + goto free_null_exa; null_exa->exa = exa; @@ -152,7 +153,7 @@ InitNullEXA(ScreenPtr pScreen, ScrnInfoPtr pScrn, int fd) if (!exaDriverInit(pScreen, exa)) { ERROR_MSG("exaDriverInit failed"); - goto fail; + goto free_exa; } armsoc_exa->CloseScreen = CloseScreen; @@ -160,13 +161,11 @@ InitNullEXA(ScreenPtr pScreen, ScrnInfoPtr pScrn, int fd) return armsoc_exa; -fail: - if (exa) - free(exa); - - if (null_exa) - free(null_exa); - +free_exa: + free(exa); +free_null_exa: + free(null_exa); +out: return NULL; } diff --git a/src/drmmode_display.c b/src/drmmode_display.c index a5f844d..f7b0dc6 100644 --- a/src/drmmode_display.c +++ b/src/drmmode_display.c @@ -267,7 +267,7 @@ drmmode_set_mode_major(xf86CrtcPtr crtc, DisplayModePtr mode, crtc->y = y; crtc->rotation = rotation; - output_ids = calloc(sizeof(uint32_t), xf86_config->num_output); + output_ids = calloc(xf86_config->num_output, sizeof *output_ids); if (!output_ids) { ERROR_MSG( "memory allocation failed in drmmode_set_mode_major()"); @@ -839,7 +839,7 @@ drmmode_crtc_init(ScrnInfoPtr pScrn, struct drmmode_rec *drmmode, int num) if (crtc == NULL) return; - drmmode_crtc = xnfcalloc(sizeof(struct drmmode_crtc_private_rec), 1); + drmmode_crtc = xnfcalloc(1, sizeof *drmmode_crtc); drmmode_crtc->crtc_id = drmmode->mode_res->crtcs[num]; drmmode_crtc->drmmode = drmmode; drmmode_crtc->last_good_mode = NULL; @@ -1021,9 +1021,8 @@ drmmode_output_create_resources(xf86OutputPtr output) uint32_t value; int i, j, err; - drmmode_output->props = - calloc(connector->count_props, - sizeof(struct drmmode_prop_rec)); + drmmode_output->props = calloc(connector->count_props, + sizeof *connector->props); if (!drmmode_output->props) return; @@ -1051,7 +1050,7 @@ drmmode_output_create_resources(xf86OutputPtr output) INT32 range[2]; p->num_atoms = 1; - p->atoms = calloc(p->num_atoms, sizeof(Atom)); + p->atoms = calloc(p->num_atoms, sizeof *p->atoms); if (!p->atoms) continue; p->atoms[0] = MakeAtom(drmmode_prop->name, @@ -1083,7 +1082,7 @@ drmmode_output_create_resources(xf86OutputPtr output) } else if (drmmode_prop->flags & DRM_MODE_PROP_ENUM) { p->num_atoms = drmmode_prop->count_enums + 1; - p->atoms = calloc(p->num_atoms, sizeof(Atom)); + p->atoms = calloc(p->num_atoms, sizeof *p->atoms); if (!p->atoms) continue; p->atoms[0] = MakeAtom(drmmode_prop->name, @@ -1312,7 +1311,7 @@ drmmode_output_init(ScrnInfoPtr pScrn, struct drmmode_rec *drmmode, int num) if (!output) goto free_encoders_exit; - drmmode_output = calloc(sizeof(struct drmmode_output_priv), 1); + drmmode_output = calloc(1, sizeof *drmmode_output); if (!drmmode_output) { xf86OutputDestroy(output); goto free_encoders_exit; -- cgit v1.2.3