Subject: [PATCH] mingw: replace deprecated GetVersion with RtlGetVersion

From: "Rose via GitGitGadget" <[email-masked]>
Date: Thu, 19 Jan 2023 17:07:25 +0000

From: Seija Kijin <[email-masked]>

The previous way is deprecated and returns
the wrong value in Windows 8 and up,
returning the manifest Windows data
as opposed to the actual Windows data.

RtlGetVersion is the correct way to get the Windows version now.

Signed-off-by: Seija Kijin <[email-masked]>
---
    mingw: replace deprecated GetVersion with RtlGetVersion
    
    GetVersion has its behavior changed in Windows 8 and above anyway, so
    this is the right way to do it now.
    
    The previous way returns the wrong value in Windows 8 and up, returning
    the manifest Windows data as opposed to the actual Windows data.
    
    Signed-off-by: Seija Kijin [email-masked]

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1438%2FAtariDreams%2Fmingw-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1438/AtariDreams/mingw-v1
Pull-Request: https://github.com/git/git/pull/1438

 compat/mingw.c | 33 +++++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)

diff --git a/compat/mingw.c b/compat/mingw.c
index af397e68a1d..ebd5850002a 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -3081,15 +3081,36 @@ int wmain(int argc, const wchar_t **wargv)
 	return exit_status;
 }
 
+/*
+ * For RtlGetVersion in uname
+ */
+
+typedef NTSTATUS(WINAPI *RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
+union winprocaddr {
+	FARPROC procaddr;
+	RtlGetVersionPtr procGetVersion;
+};
+
 int uname(struct utsname *buf)
 {
-	unsigned v = (unsigned)GetVersion();
+	union winprocaddr RtlGetVersionInternal;
+	OSVERSIONINFOA version;
+
+	RtlGetVersionInternal.procaddr =
+		GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlGetVersion");
+	if (!RtlGetVersionInternal.procaddr) {
+		die_message_errno(
+			"Could not get handle to RtlGetVersion in ntdll.dll");
+	}
+
+	version.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+	RtlGetVersionInternal.procGetVersion((PRTL_OSVERSIONINFOW)&version);
+
 	memset(buf, 0, sizeof(*buf));
 	xsnprintf(buf->sysname, sizeof(buf->sysname), "Windows");
-	xsnprintf(buf->release, sizeof(buf->release),
-		 "%u.%u", v & 0xff, (v >> 8) & 0xff);
-	/* assuming NT variants only.. */
-	xsnprintf(buf->version, sizeof(buf->version),
-		  "%u", (v >> 16) & 0x7fff);
+	xsnprintf(buf->release, sizeof(buf->release), "%lu.%lu",
+		  version.dwMajorVersion, version.dwMinorVersion);
+	xsnprintf(buf->version, sizeof(buf->version), "%lu",
+		  version.dwBuildNumber);
 	return 0;
 }

base-commit: a7caae2729742fc80147bca1c02ae848cb55921a
-- 
gitgitgadget