Skip to content

Commit 043c175

Browse files
committed
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 <[email protected]> Reviewed-by: Jeremy Huddleston <[email protected]>
1 parent dadb079 commit 043c175

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

dbe/dbe.c

+11-14
Original file line numberDiff line numberDiff line change
@@ -690,8 +690,7 @@ ProcDbeGetVisualInfo(ClientPtr client)
690690
}
691691

692692
count = (stuff->n == 0) ? screenInfo.numScreens : stuff->n;
693-
if (!(pScrVisInfo = (XdbeScreenVisualInfo *)malloc(count *
694-
sizeof(XdbeScreenVisualInfo))))
693+
if (!(pScrVisInfo = calloc(count, sizeof(XdbeScreenVisualInfo))))
695694
{
696695
free(pDrawables);
697696

@@ -707,21 +706,16 @@ ProcDbeGetVisualInfo(ClientPtr client)
707706
pDbeScreenPriv = DBE_SCREEN_PRIV(pScreen);
708707

709708
rc = XaceHook(XACE_SCREEN_ACCESS, client, pScreen, DixGetAttrAccess);
710-
if ((rc != Success) ||
711-
!(*pDbeScreenPriv->GetVisualInfo)(pScreen, &pScrVisInfo[i]))
709+
if (rc != Success)
710+
goto freeScrVisInfo;
711+
712+
if (!(*pDbeScreenPriv->GetVisualInfo)(pScreen, &pScrVisInfo[i]))
712713
{
713714
/* We failed to alloc pScrVisInfo[i].visinfo. */
715+
rc = BadAlloc;
714716

715717
/* Free visinfos that we allocated for previous screen infos.*/
716-
for (j = 0; j < i; j++)
717-
{
718-
free(pScrVisInfo[j].visinfo);
719-
}
720-
721-
/* Free pDrawables if we needed to allocate it above. */
722-
free(pDrawables);
723-
724-
return (rc == Success) ? BadAlloc : rc;
718+
goto freeScrVisInfo;
725719
}
726720

727721
/* Account for n, number of xDbeVisInfo items in list. */
@@ -790,6 +784,9 @@ ProcDbeGetVisualInfo(ClientPtr client)
790784
}
791785
}
792786

787+
rc = Success;
788+
789+
freeScrVisInfo:
793790
/* Clean up memory. */
794791
for (i = 0; i < count; i++)
795792
{
@@ -799,7 +796,7 @@ ProcDbeGetVisualInfo(ClientPtr client)
799796

800797
free(pDrawables);
801798

802-
return Success;
799+
return rc;
803800

804801
} /* ProcDbeGetVisualInfo() */
805802

0 commit comments

Comments
 (0)