diff options
author | Erlend E. Aasland <erlend.aasland@protonmail.com> | 2023-04-10 17:09:33 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-10 17:09:33 (GMT) |
commit | c3cd3d10788769558a4668e9e5cfff42fe377852 (patch) | |
tree | 829ed9d1e75f3c536cb4dd492dc8cd928458fd77 /PC | |
parent | 8b1b17134e2241a8cdff9e0c869013a7ff3ca2fe (diff) | |
download | cpython-c3cd3d10788769558a4668e9e5cfff42fe377852.zip cpython-c3cd3d10788769558a4668e9e5cfff42fe377852.tar.gz cpython-c3cd3d10788769558a4668e9e5cfff42fe377852.tar.bz2 |
gh-83004: Harden `msvcrt` init (#103383)
Diffstat (limited to 'PC')
-rw-r--r-- | PC/msvcrtmodule.c | 41 |
1 files changed, 29 insertions, 12 deletions
diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c index face4d0..6e8b423 100644 --- a/PC/msvcrtmodule.c +++ b/PC/msvcrtmodule.c @@ -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); @@ -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); + 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; } |