From 67701a8ff7ceb10e5edc0c75d5e4b6fdbf66d4d9 Mon Sep 17 00:00:00 2001 From: dmex Date: Sun, 27 Jun 2021 02:43:07 +1000 Subject: [PATCH] Fix GetAppIdForProcess memory leak --- phlib/appresolver.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/phlib/appresolver.c b/phlib/appresolver.c index 1eb6cca79894..3e3ae1b814ae 100644 --- a/phlib/appresolver.c +++ b/phlib/appresolver.c @@ -69,6 +69,23 @@ static PVOID PhpQueryStartMenuCacheInterface( return startMenuInterface; } +static LPMALLOC PhpQueryStartMenuMallocInterface( + VOID + ) +{ + static PH_INITONCE initOnce = PH_INITONCE_INIT; + static LPMALLOC allocInterface = NULL; + + if (PhBeginInitOnce(&initOnce)) + { + CoGetMalloc(MEMCTX_TASK, &allocInterface); + + PhEndInitOnce(&initOnce); + } + + return allocInterface; +} + static BOOLEAN PhpKernelAppCoreInitialized( VOID ) @@ -142,8 +159,12 @@ BOOLEAN PhAppResolverGetAppIdForProcess( if (appIdText) { + //SIZE_T appIdTextLength = IMalloc_GetSize(PhpQueryStartMenuMallocInterface(), appIdText); + //*ApplicationUserModelId = PhCreateStringEx(appIdText, appIdTextLength - sizeof(UNICODE_NULL)); + //IMalloc_Free(PhpQueryStartMenuMallocInterface(), appIdText); + *ApplicationUserModelId = PhCreateString(appIdText); - RtlFreeHeap(RtlProcessHeap(), 0, appIdText); + CoTaskMemFree(appIdText); return TRUE; } @@ -187,8 +208,12 @@ BOOLEAN PhAppResolverGetAppIdForWindow( if (appIdText) { + //SIZE_T appIdTextLength = IMalloc_GetSize(PhpQueryStartMenuMallocInterface(), appIdText); + //*ApplicationUserModelId = PhCreateStringEx(appIdText, appIdTextLength - sizeof(UNICODE_NULL)); + //IMalloc_Free(PhpQueryStartMenuMallocInterface(), appIdText); + *ApplicationUserModelId = PhCreateString(appIdText); - RtlFreeHeap(RtlProcessHeap(), 0, appIdText); + CoTaskMemFree(appIdText); return TRUE; } @@ -240,7 +265,6 @@ HRESULT PhAppResolverEnablePackageDebug( ) { HRESULT status; - PPH_LIST packageTasks = NULL; IPackageDebugSettings* packageDebugSettings; status = PhGetClassObject( @@ -270,7 +294,6 @@ HRESULT PhAppResolverDisablePackageDebug( ) { HRESULT status; - PPH_LIST packageTasks = NULL; IPackageDebugSettings* packageDebugSettings; status = PhGetClassObject( @@ -555,6 +578,7 @@ PPH_STRING PhGetPackageAppDataPath( attributeValue = PhCreateStringFromUnicodeString(&attribute->Values.pString[2]); + // TODO: Lookup user specific environment path from process. (dmex) if (attributePath = PhExpandEnvironmentStrings(&appdataPackages)) { packageAppDataPath = PhConcatStringRef2(&attributePath->sr, &attributeValue->sr);