FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-83004: Harden msvcrt init by erlend-aasland · Pull Request #103383 · python/cpython · GitHub

/ cpython Public
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
41 changes: 29 additions & 12 deletions PC/msvcrtmodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -609,11 +609,11 @@ PyMODINIT_FUNC
PyInit_msvcrt(void)
{
int st;
PyObject *d, *version;
PyObject *m = PyModule_Create(&msvcrtmodule);
if (m == NULL)
if (m == NULL) {
return NULL;
d = PyModule_GetDict(m);
}
PyObject *d = PyModule_GetDict(m); // Borrowed ref.

/* constants for the locking() function's mode argument */
insertint(d, "LK_LOCK", _LK_LOCK);
Expand Down Expand Up @@ -644,30 +644,47 @@ PyInit_msvcrt(void)
#ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
_VC_ASSEMBLY_PUBLICKEYTOKEN);
if (st < 0) return NULL;
if (st < 0) {
goto error;
}
#endif
#ifdef _CRT_ASSEMBLY_VERSION
st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
_CRT_ASSEMBLY_VERSION);
if (st < 0) return NULL;
if (st < 0) {
goto error;
}
#endif
#ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
__LIBRARIES_ASSEMBLY_NAME_PREFIX);
if (st < 0) return NULL;
if (st < 0) {
goto error;
}
#endif

/* 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);
if (st < 0) return NULL;
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) {
goto error;
}
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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Sorry I should have been more careful in this.

Py_DECREF(version);
if (st < 0) {
goto error;
}
#endif
/* make compiler warning quiet if st is unused */
(void)st;

return m;

error:
Py_DECREF(m);
return NULL;
}

Back | FazBrowse Home | New Git URL