From 30b10dcdd0c9e6801262ac5de786d86bbb967181 Mon Sep 17 00:00:00 2001 From: Michael D Kinney Date: Mon, 21 Oct 2024 23:28:15 -0700 Subject: [PATCH] UnitTestFrameworkPkg/UnitTestLib: Implement Free*() services Implement FreeUnitTestEntry(), FreeUnitTestSuiteEntry(), and FreeUnitTestFramework() so the UnitTestLib does not introduce any memory leaks. Signed-off-by: Michael D Kinney --- .../Library/UnitTestLib/UnitTestLib.c | 99 +++++++++++++++---- 1 file changed, 80 insertions(+), 19 deletions(-) diff --git a/UnitTestFrameworkPkg/Library/UnitTestLib/UnitTestLib.c b/UnitTestFrameworkPkg/Library/UnitTestLib/UnitTestLib.c index 953f1959bc..b0e0e5c4db 100644 --- a/UnitTestFrameworkPkg/Library/UnitTestLib/UnitTestLib.c +++ b/UnitTestFrameworkPkg/Library/UnitTestLib/UnitTestLib.c @@ -131,6 +131,59 @@ CompareFingerprints ( return (CompareMem (FingerprintA, FingerprintB, UNIT_TEST_FINGERPRINT_SIZE) == 0); } +STATIC +VOID +FreeUnitTestTestEntry ( + IN UNIT_TEST_LIST_ENTRY *TestEntry + ) +{ + if (TestEntry) { + if (TestEntry->UT.Description) { + FreePool (TestEntry->UT.Description); + } + + if (TestEntry->UT.Name) { + FreePool (TestEntry->UT.Name); + } + + FreePool (TestEntry); + } +} + +STATIC +EFI_STATUS +FreeUnitTestSuiteEntry ( + IN UNIT_TEST_SUITE_LIST_ENTRY *SuiteEntry + ) +{ + UNIT_TEST_LIST_ENTRY *TestCase; + UNIT_TEST_LIST_ENTRY *NextTestCase; + LIST_ENTRY *TestCaseList; + + if (SuiteEntry) { + TestCaseList = &(SuiteEntry->UTS.TestCaseList); + TestCase = (UNIT_TEST_LIST_ENTRY *)GetFirstNode (TestCaseList); + while (&TestCase->Entry != TestCaseList) { + NextTestCase = (UNIT_TEST_LIST_ENTRY *)GetNextNode (TestCaseList, &TestCase->Entry); + RemoveEntryList (&TestCase->Entry); + FreeUnitTestTestEntry (TestCase); + TestCase = NextTestCase; + } + + if (SuiteEntry->UTS.Title) { + FreePool (SuiteEntry->UTS.Title); + } + + if (SuiteEntry->UTS.Name) { + FreePool (SuiteEntry->UTS.Name); + } + + FreePool (SuiteEntry); + } + + return EFI_SUCCESS; +} + /** Cleanup a test framework. @@ -151,27 +204,35 @@ FreeUnitTestFramework ( IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle ) { - // TODO: Finish this function. - return EFI_SUCCESS; -} + UNIT_TEST_FRAMEWORK *Framework; + UNIT_TEST_SUITE_LIST_ENTRY *Suite; + UNIT_TEST_SUITE_LIST_ENTRY *NextSuite; -STATIC -EFI_STATUS -FreeUnitTestSuiteEntry ( - IN UNIT_TEST_SUITE_LIST_ENTRY *SuiteEntry - ) -{ - // TODO: Finish this function. - return EFI_SUCCESS; -} + Framework = (UNIT_TEST_FRAMEWORK *)FrameworkHandle; + if (Framework) { + Suite = (UNIT_TEST_SUITE_LIST_ENTRY *)GetFirstNode (&Framework->TestSuiteList); + while ((LIST_ENTRY *)Suite != &Framework->TestSuiteList) { + NextSuite = (UNIT_TEST_SUITE_LIST_ENTRY *)GetNextNode (&Framework->TestSuiteList, (LIST_ENTRY *)Suite); + RemoveEntryList ((LIST_ENTRY *)Suite); + FreeUnitTestSuiteEntry (Suite); + Suite = NextSuite; + } + + if (Framework->Title) { + FreePool (Framework->Title); + } + + if (Framework->ShortTitle) { + FreePool (Framework->ShortTitle); + } + + if (Framework->VersionString) { + FreePool (Framework->VersionString); + } + + FreePool (Framework); + } -STATIC -EFI_STATUS -FreeUnitTestTestEntry ( - IN UNIT_TEST_LIST_ENTRY *TestEntry - ) -{ - // TODO: Finish this function. return EFI_SUCCESS; }