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 <alan.coopersmith@oracle.com> Reviewed-by: Jeremy Huddleston <jeremyhu@apple.com>
This commit is contained in:
parent
dadb0791eb
commit
043c175865
25
dbe/dbe.c
25
dbe/dbe.c
|
@ -690,8 +690,7 @@ ProcDbeGetVisualInfo(ClientPtr client)
|
||||||
}
|
}
|
||||||
|
|
||||||
count = (stuff->n == 0) ? screenInfo.numScreens : stuff->n;
|
count = (stuff->n == 0) ? screenInfo.numScreens : stuff->n;
|
||||||
if (!(pScrVisInfo = (XdbeScreenVisualInfo *)malloc(count *
|
if (!(pScrVisInfo = calloc(count, sizeof(XdbeScreenVisualInfo))))
|
||||||
sizeof(XdbeScreenVisualInfo))))
|
|
||||||
{
|
{
|
||||||
free(pDrawables);
|
free(pDrawables);
|
||||||
|
|
||||||
|
@ -707,21 +706,16 @@ ProcDbeGetVisualInfo(ClientPtr client)
|
||||||
pDbeScreenPriv = DBE_SCREEN_PRIV(pScreen);
|
pDbeScreenPriv = DBE_SCREEN_PRIV(pScreen);
|
||||||
|
|
||||||
rc = XaceHook(XACE_SCREEN_ACCESS, client, pScreen, DixGetAttrAccess);
|
rc = XaceHook(XACE_SCREEN_ACCESS, client, pScreen, DixGetAttrAccess);
|
||||||
if ((rc != Success) ||
|
if (rc != Success)
|
||||||
!(*pDbeScreenPriv->GetVisualInfo)(pScreen, &pScrVisInfo[i]))
|
goto freeScrVisInfo;
|
||||||
|
|
||||||
|
if (!(*pDbeScreenPriv->GetVisualInfo)(pScreen, &pScrVisInfo[i]))
|
||||||
{
|
{
|
||||||
/* We failed to alloc pScrVisInfo[i].visinfo. */
|
/* We failed to alloc pScrVisInfo[i].visinfo. */
|
||||||
|
rc = BadAlloc;
|
||||||
|
|
||||||
/* Free visinfos that we allocated for previous screen infos.*/
|
/* Free visinfos that we allocated for previous screen infos.*/
|
||||||
for (j = 0; j < i; j++)
|
goto freeScrVisInfo;
|
||||||
{
|
|
||||||
free(pScrVisInfo[j].visinfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Free pDrawables if we needed to allocate it above. */
|
|
||||||
free(pDrawables);
|
|
||||||
|
|
||||||
return (rc == Success) ? BadAlloc : rc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Account for n, number of xDbeVisInfo items in list. */
|
/* Account for n, number of xDbeVisInfo items in list. */
|
||||||
|
@ -790,6 +784,9 @@ ProcDbeGetVisualInfo(ClientPtr client)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rc = Success;
|
||||||
|
|
||||||
|
freeScrVisInfo:
|
||||||
/* Clean up memory. */
|
/* Clean up memory. */
|
||||||
for (i = 0; i < count; i++)
|
for (i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
|
@ -799,7 +796,7 @@ ProcDbeGetVisualInfo(ClientPtr client)
|
||||||
|
|
||||||
free(pDrawables);
|
free(pDrawables);
|
||||||
|
|
||||||
return Success;
|
return rc;
|
||||||
|
|
||||||
} /* ProcDbeGetVisualInfo() */
|
} /* ProcDbeGetVisualInfo() */
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user