File tree Expand file tree Collapse file tree 2 files changed +17
-7
lines changed Expand file tree Collapse file tree 2 files changed +17
-7
lines changed Original file line number Diff line number Diff line change 1+ Avoid crashing in :mod: `platform ` due to slow WMI calls on some Windows
2+ machines.
Original file line number Diff line number Diff line change @@ -55,12 +55,26 @@ _query_thread(LPVOID param)
5555 IWbemLocator *locator = NULL ;
5656 IWbemServices *services = NULL ;
5757 IEnumWbemClassObject* enumerator = NULL ;
58+ HRESULT hr = S_OK;
5859 BSTR bstrQuery = NULL ;
5960 struct _query_data *data = (struct _query_data *)param;
6061
61- HRESULT hr = CoInitializeEx (nullptr , COINIT_APARTMENTTHREADED);
62+ // gh-125315: Copy the query string first, so that if the main thread gives
63+ // up on waiting we aren't left with a dangling pointer (and a likely crash)
64+ bstrQuery = SysAllocString (data->query );
65+ if (!bstrQuery) {
66+ hr = HRESULT_FROM_WIN32 (ERROR_NOT_ENOUGH_MEMORY);
67+ }
68+
69+ if (SUCCEEDED (hr)) {
70+ hr = CoInitializeEx (nullptr , COINIT_APARTMENTTHREADED);
71+ }
72+
6273 if (FAILED (hr)) {
6374 CloseHandle (data->writePipe );
75+ if (bstrQuery) {
76+ SysFreeString (bstrQuery);
77+ }
6478 return (DWORD)hr;
6579 }
6680
@@ -101,12 +115,6 @@ _query_thread(LPVOID param)
101115 NULL , EOAC_NONE
102116 );
103117 }
104- if (SUCCEEDED (hr)) {
105- bstrQuery = SysAllocString (data->query );
106- if (!bstrQuery) {
107- hr = HRESULT_FROM_WIN32 (ERROR_NOT_ENOUGH_MEMORY);
108- }
109- }
110118 if (SUCCEEDED (hr)) {
111119 hr = services->ExecQuery (
112120 bstr_t (" WQL" ),
You can’t perform that action at this time.
0 commit comments