https://github.com/python/cpython/commit/60c415bd531392a239c23c754154a7944695ac99
commit: 60c415bd531392a239c23c754154a7944695ac99
branch: main
author: Steve Dower <[email protected]>
committer: zooba <[email protected]>
date: 2024-10-30T19:59:51Z
summary:

gh-125315: Avoid crashing in _wmimodule due to slow WMI calls on some Windows 
machines (GH-126141)

files:
A Misc/NEWS.d/next/Windows/2024-10-29-19-48-03.gh-issue-125315.jdB9qN.rst
M PC/_wmimodule.cpp

diff --git 
a/Misc/NEWS.d/next/Windows/2024-10-29-19-48-03.gh-issue-125315.jdB9qN.rst 
b/Misc/NEWS.d/next/Windows/2024-10-29-19-48-03.gh-issue-125315.jdB9qN.rst
new file mode 100644
index 00000000000000..3d813248766a5b
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2024-10-29-19-48-03.gh-issue-125315.jdB9qN.rst
@@ -0,0 +1,2 @@
+Avoid crashing in :mod:`platform` due to slow WMI calls on some Windows
+machines.
diff --git a/PC/_wmimodule.cpp b/PC/_wmimodule.cpp
index 48863b90f4cc27..3a59e78f171526 100644
--- a/PC/_wmimodule.cpp
+++ b/PC/_wmimodule.cpp
@@ -55,12 +55,26 @@ _query_thread(LPVOID param)
     IWbemLocator *locator = NULL;
     IWbemServices *services = NULL;
     IEnumWbemClassObject* enumerator = NULL;
+    HRESULT hr = S_OK;
     BSTR bstrQuery = NULL;
     struct _query_data *data = (struct _query_data*)param;
 
-    HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
+    // gh-125315: Copy the query string first, so that if the main thread gives
+    // up on waiting we aren't left with a dangling pointer (and a likely 
crash)
+    bstrQuery = SysAllocString(data->query);
+    if (!bstrQuery) {
+        hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
+    }
+
+    if (SUCCEEDED(hr)) {
+        hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
+    }
+
     if (FAILED(hr)) {
         CloseHandle(data->writePipe);
+        if (bstrQuery) {
+            SysFreeString(bstrQuery);
+        }
         return (DWORD)hr;
     }
 
@@ -101,12 +115,6 @@ _query_thread(LPVOID param)
             NULL, EOAC_NONE
         );
     }
-    if (SUCCEEDED(hr)) {
-        bstrQuery = SysAllocString(data->query);
-        if (!bstrQuery) {
-            hr = HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
-        }
-    }
     if (SUCCEEDED(hr)) {
         hr = services->ExecQuery(
             bstr_t("WQL"),

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to