Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-83004: Harden msvcrt init
  • Loading branch information
erlend-aasland committed Apr 8, 2023
commit 18820c1f534438a717efab37964ef7156694bb1d
17 changes: 11 additions & 6 deletions PC/msvcrtmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ PyMODINIT_FUNC
PyInit_msvcrt(void)
{
int st;
PyObject *d, *version;
PyObject *d;
PyObject *m = PyModule_Create(&msvcrtmodule);
if (m == NULL)
return NULL;
Expand Down Expand Up @@ -659,11 +659,16 @@ PyInit_msvcrt(void)

/* constants for the 2010 crt versions */
#if defined(_VC_CRT_MAJOR_VERSION) && defined (_VC_CRT_MINOR_VERSION) && defined(_VC_CRT_BUILD_VERSION) && defined(_VC_CRT_RBUILD_VERSION)
version = PyUnicode_FromFormat("%d.%d.%d.%d", _VC_CRT_MAJOR_VERSION,
_VC_CRT_MINOR_VERSION,
_VC_CRT_BUILD_VERSION,
_VC_CRT_RBUILD_VERSION);
st = PyModule_AddObject(m, "CRT_ASSEMBLY_VERSION", version);
PyObject *version = PyUnicode_FromFormat("%d.%d.%d.%d",
_VC_CRT_MAJOR_VERSION,
_VC_CRT_MINOR_VERSION,
_VC_CRT_BUILD_VERSION,
_VC_CRT_RBUILD_VERSION);
if (version == NULL) {
return NULL;
}
st = PyModule_AddObjectRef(m, "CRT_ASSEMBLY_VERSION", version);
Comment on lines +673 to +676
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@erlend-aasland Just FYI, the modsupport functions allow passing in NULL provided an error has already occurred, and they just let it chain. So the code here was fine before.

No need to revert now though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL! Sorry for not waiting on your review.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, there was a leak if PyModule_AddObject failed, since that API only eat refs if successful.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I should have been more careful in this.

Py_DECREF(version);
if (st < 0) return NULL;
#endif
/* make compiler warning quiet if st is unused */
Expand Down