From 043c1758652259fd12b88ae37720fe6e93eda76b Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Tue, 19 Apr 2011 19:02:54 -0700 Subject: Clean up memory better when GetVisualInfo fails in ProcDbeGetVisualInfo Use calloc to initialize pScrVisInfo array so we don't have to check which ones were already initialized when freeing them all. On failure, set rc if necessary, and jump to code at end that already frees all the necessary allocations and return rc. Fixes parfait reported error: Error: Memory leak (CWE 401) Memory leak of pointer 'pScrVisInfo' allocated with malloc((count * 16)) at line 724 of dbe/dbe.c in function 'ProcDbeGetVisualInfo'. 'pScrVisInfo' allocated at line 693 with malloc((count * 16)). pScrVisInfo leaks when rc != 0 at line 710 and j >= i at line 716. [ This bug was found by the Parfait 0.3.7 bug checking tool. For more information see http://labs.oracle.com/projects/parfait/ ] Signed-off-by: Alan Coopersmith Reviewed-by: Jeremy Huddleston --- dbe/dbe.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to 'dbe') diff --git a/dbe/dbe.c b/dbe/dbe.c index 77b616b6a..51bbdc6c9 100644 --- a/dbe/dbe.c +++ b/dbe/dbe.c @@ -690,8 +690,7 @@ ProcDbeGetVisualInfo(ClientPtr client) } count = (stuff->n == 0) ? screenInfo.numScreens : stuff->n; - if (!(pScrVisInfo = (XdbeScreenVisualInfo *)malloc(count * - sizeof(XdbeScreenVisualInfo)))) + if (!(pScrVisInfo = calloc(count, sizeof(XdbeScreenVisualInfo)))) { free(pDrawables); @@ -707,21 +706,16 @@ ProcDbeGetVisualInfo(ClientPtr client) pDbeScreenPriv = DBE_SCREEN_PRIV(pScreen); rc = XaceHook(XACE_SCREEN_ACCESS, client, pScreen, DixGetAttrAccess); - if ((rc != Success) || - !(*pDbeScreenPriv->GetVisualInfo)(pScreen, &pScrVisInfo[i])) + if (rc != Success) + goto freeScrVisInfo; + + if (!(*pDbeScreenPriv->GetVisualInfo)(pScreen, &pScrVisInfo[i])) { /* We failed to alloc pScrVisInfo[i].visinfo. */ + rc = BadAlloc; /* Free visinfos that we allocated for previous screen infos.*/ - for (j = 0; j < i; j++) - { - free(pScrVisInfo[j].visinfo); - } - - /* Free pDrawables if we needed to allocate it above. */ - free(pDrawables); - - return (rc == Success) ? BadAlloc : rc; + goto freeScrVisInfo; } /* Account for n, number of xDbeVisInfo items in list. */ @@ -790,6 +784,9 @@ ProcDbeGetVisualInfo(ClientPtr client) } } + rc = Success; + + freeScrVisInfo: /* Clean up memory. */ for (i = 0; i < count; i++) { @@ -799,7 +796,7 @@ ProcDbeGetVisualInfo(ClientPtr client) free(pDrawables); - return Success; + return rc; } /* ProcDbeGetVisualInfo() */ -- cgit v1.2.3