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;
}
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"),