Fix #266: CloseHandle close an invalid handle

This commit is contained in:
yuanyuanxiang
2025-12-26 15:57:27 +01:00
parent 02d86f6ce7
commit 34e7cdb663
33 changed files with 171 additions and 161 deletions

View File

@@ -68,20 +68,20 @@ inline HKEY GetCurrentUserRegistryKey()
DWORD dwSize = 0;
GetTokenInformation(hUserToken, TokenUser, NULL, 0, &dwSize);
if (dwSize == 0) {
CloseHandle(hUserToken);
SAFE_CLOSE_HANDLE(hUserToken);
return HKEY_CURRENT_USER;
}
// 分配内存并获取用户信息
TOKEN_USER* pTokenUser = (TOKEN_USER*)malloc(dwSize);
if (!pTokenUser) {
CloseHandle(hUserToken);
SAFE_CLOSE_HANDLE(hUserToken);
return HKEY_CURRENT_USER;
}
if (!GetTokenInformation(hUserToken, TokenUser, pTokenUser, dwSize, &dwSize)) {
free(pTokenUser);
CloseHandle(hUserToken);
SAFE_CLOSE_HANDLE(hUserToken);
return HKEY_CURRENT_USER;
}
@@ -89,7 +89,7 @@ inline HKEY GetCurrentUserRegistryKey()
LPSTR szSid = NULL;
if (!ConvertSidToStringSidA(pTokenUser->User.Sid, &szSid)) {
free(pTokenUser);
CloseHandle(hUserToken);
SAFE_CLOSE_HANDLE(hUserToken);
return HKEY_CURRENT_USER;
}
@@ -103,7 +103,7 @@ inline HKEY GetCurrentUserRegistryKey()
LocalFree(szSid);
free(pTokenUser);
CloseHandle(hUserToken);
SAFE_CLOSE_HANDLE(hUserToken);
return hUserKey ? hUserKey : HKEY_CURRENT_USER;
}