Skip to content

Commit b516598

Browse files
nir9chrisbra
authored andcommitted
patch 9.1.0780: MS-Windows: incorrect Win32 error checking
Problem: MS-Windows: incorrect Win32 error checking Solution: fix wrong order of error handling and perform some minor refactoring (Nir Lichtman) In the function that adjusts the process privileges there is a mistake in which GetLastError is called after CloseHandle, though clearly the last error check is meant for the privileges related call before hand and the current state appears like a mistake. So fix this problem, and also perform the following: - Remove the static variable done since the PlatformId is only called during initialization - Fix incorrect parameter passed to the Win32 API privileges function closes: #15845 Signed-off-by: Nir Lichtman <nir@lichtman.org> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent a2aa921 commit b516598

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

src/os_win32.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -918,9 +918,8 @@ null_libintl_wputenv(const wchar_t *envstring UNUSED)
918918
* Enables or disables the specified privilege.
919919
*/
920920
static BOOL
921-
win32_enable_privilege(LPTSTR lpszPrivilege, BOOL bEnable)
921+
win32_enable_privilege(LPTSTR lpszPrivilege)
922922
{
923-
BOOL bResult;
924923
LUID luid;
925924
HANDLE hToken;
926925
TOKEN_PRIVILEGES tokenPrivileges;
@@ -937,15 +936,22 @@ win32_enable_privilege(LPTSTR lpszPrivilege, BOOL bEnable)
937936

938937
tokenPrivileges.PrivilegeCount = 1;
939938
tokenPrivileges.Privileges[0].Luid = luid;
940-
tokenPrivileges.Privileges[0].Attributes = bEnable ?
941-
SE_PRIVILEGE_ENABLED : 0;
939+
tokenPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
942940

943-
bResult = AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges,
944-
sizeof(TOKEN_PRIVILEGES), NULL, NULL);
941+
if (!AdjustTokenPrivileges(hToken, FALSE, &tokenPrivileges, 0, NULL, NULL))
942+
{
943+
CloseHandle(hToken);
944+
return FALSE;
945+
}
945946

946-
CloseHandle(hToken);
947+
if (GetLastError() != ERROR_SUCCESS)
948+
{
949+
CloseHandle(hToken);
950+
return FALSE;
951+
}
947952

948-
return bResult && GetLastError() == ERROR_SUCCESS;
953+
CloseHandle(hToken);
954+
return TRUE;
949955
}
950956
#endif
951957

@@ -961,15 +967,11 @@ win32_enable_privilege(LPTSTR lpszPrivilege, BOOL bEnable)
961967
void
962968
PlatformId(void)
963969
{
964-
static int done = FALSE;
965-
966-
if (done)
967-
return;
968-
969970
OSVERSIONINFO ovi;
970971

971972
ovi.dwOSVersionInfoSize = sizeof(ovi);
972-
GetVersionEx(&ovi);
973+
if (!GetVersionEx(&ovi))
974+
return;
973975

974976
#ifdef FEAT_EVAL
975977
vim_snprintf(windowsVersion, sizeof(windowsVersion), "%d.%d",
@@ -985,9 +987,9 @@ PlatformId(void)
985987

986988
#ifdef HAVE_ACL
987989
// Enable privilege for getting or setting SACLs.
988-
win32_enable_privilege(SE_SECURITY_NAME, TRUE);
990+
if (!win32_enable_privilege(SE_SECURITY_NAME))
991+
return;
989992
#endif
990-
done = TRUE;
991993
}
992994
#ifdef _MSC_VER
993995
# pragma warning(pop)

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
780,
707709
/**/
708710
779,
709711
/**/

0 commit comments

Comments
 (0)