Skip to content

Commit cfc77c6

Browse files
Mschtmschlicht-tt
andauthored
Move PyWInObject_Free* methods behind PY_INTERFACE_POSTCALL (#2467)
Prevent access violation if no GIL is aquired. --------- Co-authored-by: Mario <[email protected]>
1 parent 8d5af1f commit cfc77c6

15 files changed

+25
-27
lines changed

Pythonwin/win32view.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,8 @@ static PyObject *ui_edit_window_save_file(PyObject *self, PyObject *args)
839839
CDocument *pDocument = pView->GetDocument();
840840
if (pDocument)
841841
pDocument->SetModifiedFlag(FALSE); // start off with unmodified
842-
PyWinObject_FreeTCHAR(fileName);
843842
GUI_END_SAVE;
843+
PyWinObject_FreeTCHAR(fileName);
844844
RETURN_NONE;
845845
}
846846
// @pymethod tuple|PyCEditView|PreCreateWindow|Calls the underlying MFC PreCreateWindow method.

Pythonwin/win32win.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1654,9 +1654,9 @@ static PyObject *ui_window_message_box(PyObject *self, PyObject *args)
16541654
// @pyseemfc CWnd|MessageBox
16551655

16561656
rc = pWnd->MessageBox(message, title, style);
1657+
GUI_END_SAVE;
16571658
PyWinObject_FreeTCHAR(message);
16581659
PyWinObject_FreeTCHAR(title);
1659-
GUI_END_SAVE;
16601660
return Py_BuildValue("i", rc);
16611661
// @rdesc An integer identifying the button pressed to dismiss the dialog.
16621662
}

com/win32com/src/extensions/PyIType.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,8 @@ PyObject *pythoncom_loadtypelib(PyObject *self, PyObject *args)
688688
ITypeLib *ptl;
689689
PY_INTERFACE_PRECALL;
690690
SCODE sc = LoadTypeLib(bstrName, &ptl);
691-
PyWinObject_FreeBstr(bstrName);
692691
PY_INTERFACE_POSTCALL;
692+
PyWinObject_FreeBstr(bstrName);
693693
if (FAILED(sc))
694694
return PyCom_BuildPyException(sc);
695695

com/win32comext/bits/src/PyIBackgroundCopyJob.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -620,9 +620,9 @@ PyObject *PyIBackgroundCopyJob::SetProxySettings(PyObject *self, PyObject *args)
620620
HRESULT hr;
621621
PY_INTERFACE_PRECALL;
622622
hr = pIBCJ->SetProxySettings(ProxyUsage, ProxyList, ProxyBypassList);
623+
PY_INTERFACE_POSTCALL;
623624
PyWinObject_FreeWCHAR(ProxyList);
624625
PyWinObject_FreeWCHAR(ProxyBypassList);
625-
PY_INTERFACE_POSTCALL;
626626
if (FAILED(hr))
627627
return PyCom_BuildPyException(hr, pIBCJ, IID_IBackgroundCopyJob);
628628
Py_INCREF(Py_None);

com/win32comext/internet/src/PyIDocHostUIHandler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ PyObject *PyIDocHostUIHandler::TranslateUrl(PyObject *self, PyObject *args)
376376
HRESULT hr;
377377
PY_INTERFACE_PRECALL;
378378
hr = pIDHUIH->TranslateUrl(dwTranslate, pchURLIn, &pchURLOut);
379-
PyWinObject_FreeWCHAR(pchURLIn);
380379
PY_INTERFACE_POSTCALL;
380+
PyWinObject_FreeWCHAR(pchURLIn);
381381
if (FAILED(hr))
382382
return PyCom_BuildPyException(hr, pIDHUIH, IID_IDocHostUIHandler);
383383
PyObject *pyretval = MakeOLECHARToObj(pchURLOut);

com/win32comext/internet/src/PyIInternetSecurityManager.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ PyObject *PyIInternetSecurityManager::ProcessUrlAction(PyObject *self, PyObject
145145
PY_INTERFACE_PRECALL;
146146
hr = pIISM->ProcessUrlAction(pwszUrl, dwAction, (BYTE *)&dwPolicy, sizeof(dwPolicy), (BYTE *)context, cbcontext,
147147
dwFlags, 0);
148-
PyWinObject_FreeWCHAR(pwszUrl);
149148
PY_INTERFACE_POSTCALL;
149+
PyWinObject_FreeWCHAR(pwszUrl);
150150
if (FAILED(hr))
151151
return PyCom_BuildPyException(hr, pIISM, IID_IInternetSecurityManager);
152152
return Py_BuildValue("ll", hr, dwPolicy);

com/win32comext/shell/src/PyICopyHook.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,10 @@ PyObject *PyICopyHookA::CopyCallback(PyObject *self, PyObject *args)
5454
HRESULT hr;
5555
PY_INTERFACE_PRECALL;
5656
hr = pICH->CopyCallback(hwnd, wFunc, wFlags, srcFile, srcAttribs, destFile, destAttribs);
57+
PY_INTERFACE_POSTCALL;
5758
PyWinObject_FreeChars(srcFile);
5859
PyWinObject_FreeChars(destFile);
5960

60-
PY_INTERFACE_POSTCALL;
61-
6261
if (FAILED(hr))
6362
return PyCom_BuildPyException(hr, pICH, IID_IShellCopyHook);
6463
Py_INCREF(Py_None);
@@ -143,10 +142,10 @@ PyObject *PyICopyHookW::CopyCallback(PyObject *self, PyObject *args)
143142
HRESULT hr;
144143
PY_INTERFACE_PRECALL;
145144
hr = pICH->CopyCallback(hwnd, wFunc, wFlags, srcFile, srcAttribs, destFile, destAttribs);
146-
PyWinObject_FreeWCHAR(srcFile);
147-
PyWinObject_FreeWCHAR(destFile);
148145

149146
PY_INTERFACE_POSTCALL;
147+
PyWinObject_FreeWCHAR(srcFile);
148+
PyWinObject_FreeWCHAR(destFile);
150149

151150
if (FAILED(hr))
152151
return PyCom_BuildPyException(hr, pICH, IID_IShellCopyHook);

com/win32comext/shell/src/PyIExtractIcon.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ PyObject *PyIExtractIcon::Extract(PyObject *self, PyObject *args)
3838
HRESULT hr;
3939
PY_INTERFACE_PRECALL;
4040
hr = pIEI->Extract(pszFile, nIconIndex, &hiconLarge, &hiconSmall, nIconSize);
41-
PyWinObject_FreeChars(pszFile);
4241
PY_INTERFACE_POSTCALL;
42+
PyWinObject_FreeChars(pszFile);
43+
4344
if (FAILED(hr))
4445
return PyCom_BuildPyException(hr, pIEI, IID_IExtractIcon);
4546
if (hr == S_FALSE)

com/win32comext/shell/src/PyIExtractIconW.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ PyObject *PyIExtractIconW::Extract(PyObject *self, PyObject *args)
4242
HRESULT hr;
4343
PY_INTERFACE_PRECALL;
4444
hr = pIEI->Extract(pszFile, nIconIndex, &hiconLarge, &hiconSmall, nIconSize);
45-
PyWinObject_FreeWCHAR(pszFile);
4645
PY_INTERFACE_POSTCALL;
46+
PyWinObject_FreeWCHAR(pszFile);
47+
4748
if (FAILED(hr))
4849
return PyCom_BuildPyException(hr, pIEI, IID_IExtractIconW);
4950
if (hr == S_FALSE)

com/win32comext/shell/src/PyIQueryAssociations.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ PyObject *PyIQueryAssociations::Init(PyObject *self, PyObject *args)
4545
HRESULT hr;
4646
PY_INTERFACE_PRECALL;
4747
hr = pIQA->Init(flags, pszAssoc, hkProgid, hwnd);
48-
PyWinObject_FreeWCHAR(pszAssoc);
4948
PY_INTERFACE_POSTCALL;
49+
PyWinObject_FreeWCHAR(pszAssoc);
50+
5051
if (FAILED(hr))
5152
return PyCom_BuildPyException(hr, pIQA, IID_IQueryAssociations);
5253
Py_INCREF(Py_None);
@@ -77,8 +78,8 @@ PyObject *PyIQueryAssociations::GetKey(PyObject *self, PyObject *args)
7778
HRESULT hr;
7879
PY_INTERFACE_PRECALL;
7980
hr = pIQA->GetKey(flags, (ASSOCKEY)assoc, pszExtra, &ret);
80-
PyWinObject_FreeWCHAR(pszExtra);
8181
PY_INTERFACE_POSTCALL;
82+
PyWinObject_FreeWCHAR(pszExtra);
8283
if (FAILED(hr))
8384
return PyCom_BuildPyException(hr, pIQA, IID_IQueryAssociations);
8485
// observation of the "open handles" count in task-manager indicates
@@ -116,8 +117,8 @@ PyObject *PyIQueryAssociations::GetString(PyObject *self, PyObject *args)
116117
HRESULT hr;
117118
PY_INTERFACE_PRECALL;
118119
hr = pIQA->GetString(flags, (ASSOCSTR)assoc, pszExtra, result_buf, &result_size);
119-
PyWinObject_FreeWCHAR(pszExtra);
120120
PY_INTERFACE_POSTCALL;
121+
PyWinObject_FreeWCHAR(pszExtra);
121122
if (FAILED(hr))
122123
return PyCom_BuildPyException(hr, pIQA, IID_IQueryAssociations);
123124
// docs don't explicitly say if result_size includes NULL. It says:

com/win32comext/shell/src/PyIShellIconOverlayIdentifier.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ PyObject *PyIShellIconOverlayIdentifier::IsMemberOf(PyObject *self, PyObject *ar
4343
HRESULT hr;
4444
PY_INTERFACE_PRECALL;
4545
hr = pISIOI->IsMemberOf(path, attrib);
46-
PyWinObject_FreeWCHAR(path);
4746

4847
PY_INTERFACE_POSTCALL;
48+
PyWinObject_FreeWCHAR(path);
4949

5050
if (FAILED(hr))
5151
return PyCom_BuildPyException(hr, pISIOI, IID_IShellIconOverlayIdentifier);

com/win32comext/shell/src/shell.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -856,11 +856,11 @@ void PyObject_CleanupDEFCONTEXTMENU(DEFCONTEXTMENU *dcm)
856856
dcm->psf->Release();
857857
if (dcm->punkAssociationInfo)
858858
dcm->punkAssociationInfo->Release();
859+
PY_INTERFACE_POSTCALL;
859860
if (dcm->pidlFolder)
860861
PyObject_FreePIDL(dcm->pidlFolder);
861862
if (dcm->apidl)
862863
PyObject_FreePIDLArray(dcm->cidl, dcm->apidl);
863-
PY_INTERFACE_POSTCALL;
864864
}
865865

866866
// @object DEFCONTENTMENU|A tuple representing a DEFCONTEXTMENU structure.

com/win32comext/taskscheduler/src/PyIScheduledWorkItem.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,8 @@ PyObject *PyIScheduledWorkItem::SetComment(PyObject *self, PyObject *args)
389389
HRESULT hr;
390390
PY_INTERFACE_PRECALL;
391391
hr = pISWI->SetComment(pwszComment);
392-
PyWinObject_FreeWCHAR(pwszComment);
393-
394392
PY_INTERFACE_POSTCALL;
393+
PyWinObject_FreeWCHAR(pwszComment);
395394

396395
if (FAILED(hr))
397396
return PyCom_BuildPyException(hr, pISWI, IID_IScheduledWorkItem);
@@ -437,9 +436,8 @@ PyObject *PyIScheduledWorkItem::SetCreator(PyObject *self, PyObject *args)
437436
HRESULT hr;
438437
PY_INTERFACE_PRECALL;
439438
hr = pISWI->SetCreator(pwszCreator);
440-
PyWinObject_FreeWCHAR(pwszCreator);
441-
442439
PY_INTERFACE_POSTCALL;
440+
PyWinObject_FreeWCHAR(pwszCreator);
443441

444442
if (FAILED(hr))
445443
return PyCom_BuildPyException(hr, pISWI, IID_IScheduledWorkItem);
@@ -685,9 +683,9 @@ PyObject *PyIScheduledWorkItem::SetAccountInformation(PyObject *self, PyObject *
685683
HRESULT hr;
686684
PY_INTERFACE_PRECALL;
687685
hr = pISWI->SetAccountInformation(AccountName, Password);
686+
PY_INTERFACE_POSTCALL;
688687
PyWinObject_FreeWCHAR(AccountName);
689688
PyWinObject_FreeWCHAR(Password);
690-
PY_INTERFACE_POSTCALL;
691689

692690
if (FAILED(hr))
693691
return PyCom_BuildPyException(hr, pISWI, IID_IScheduledWorkItem);

com/win32comext/taskscheduler/src/PyITask.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ PyObject *PyITask::SetApplicationName(PyObject *self, PyObject *args)
3030
HRESULT hr;
3131
PY_INTERFACE_PRECALL;
3232
hr = pIT->SetApplicationName(pwszApplicationName);
33-
PyWinObject_FreeWCHAR(pwszApplicationName);
34-
3533
PY_INTERFACE_POSTCALL;
34+
PyWinObject_FreeWCHAR(pwszApplicationName);
3635

3736
if (FAILED(hr))
3837
return PyCom_BuildPyException(hr, pIT, IID_ITask);
@@ -80,9 +79,8 @@ PyObject *PyITask::SetParameters(PyObject *self, PyObject *args)
8079
HRESULT hr;
8180
PY_INTERFACE_PRECALL;
8281
hr = pIT->SetParameters(pwszParameters);
83-
PyWinObject_FreeWCHAR(pwszParameters);
84-
8582
PY_INTERFACE_POSTCALL;
83+
PyWinObject_FreeWCHAR(pwszParameters);
8684

8785
if (FAILED(hr))
8886
return PyCom_BuildPyException(hr, pIT, IID_ITask);

com/win32comext/taskscheduler/src/PyITaskScheduler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ PyObject *PyITaskScheduler::SetTargetComputer(PyObject *self, PyObject *args)
3434
HRESULT hr;
3535
PY_INTERFACE_PRECALL;
3636
hr = pITS->SetTargetComputer(Computer);
37-
PyWinObject_FreeWCHAR(Computer);
3837
PY_INTERFACE_POSTCALL;
38+
PyWinObject_FreeWCHAR(Computer);
3939

4040
if (FAILED(hr))
4141
return PyCom_BuildPyException(hr, pITS, IID_ITaskScheduler);

0 commit comments

Comments
 (0)