Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5ed2f19

Browse filesBrowse files
pythongh-83004: Harden winreg init (python#103386)
1 parent c3cd3d1 commit 5ed2f19
Copy full SHA for 5ed2f19

File tree

1 file changed

+37
-28
lines changed
Filter options

1 file changed

+37
-28
lines changed

‎PC/winreg.c

Copy file name to clipboardExpand all lines: PC/winreg.c
+37-28Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,27 +2059,29 @@ static struct PyMethodDef winreg_methods[] = {
20592059
NULL,
20602060
};
20612061

2062-
static void
2063-
insint(PyObject * d, char * name, long value)
2064-
{
2065-
PyObject *v = PyLong_FromLong(value);
2066-
if (!v || PyDict_SetItemString(d, name, v))
2067-
PyErr_Clear();
2068-
Py_XDECREF(v);
2069-
}
2070-
2071-
#define ADD_INT(val) insint(d, #val, val)
2062+
#define ADD_INT(VAL) do { \
2063+
if (PyModule_AddIntConstant(m, #VAL, VAL) < 0) { \
2064+
goto error; \
2065+
} \
2066+
} while (0)
20722067

2073-
static void
2074-
inskey(PyObject * d, char * name, HKEY key)
2068+
static int
2069+
inskey(PyObject *mod, char *name, HKEY key)
20752070
{
20762071
PyObject *v = PyLong_FromVoidPtr(key);
2077-
if (!v || PyDict_SetItemString(d, name, v))
2078-
PyErr_Clear();
2079-
Py_XDECREF(v);
2072+
if (v == NULL) {
2073+
return -1;
2074+
}
2075+
int rc = PyModule_AddObjectRef(mod, name, v);
2076+
Py_DECREF(v);
2077+
return rc;
20802078
}
20812079

2082-
#define ADD_KEY(val) inskey(d, #val, val)
2080+
#define ADD_KEY(VAL) do { \
2081+
if (inskey(m, #VAL, VAL) < 0) { \
2082+
goto error; \
2083+
} \
2084+
} while (0)
20832085

20842086

20852087
static struct PyModuleDef winregmodule = {
@@ -2096,20 +2098,20 @@ static struct PyModuleDef winregmodule = {
20962098

20972099
PyMODINIT_FUNC PyInit_winreg(void)
20982100
{
2099-
PyObject *m, *d;
2100-
m = PyModule_Create(&winregmodule);
2101-
if (m == NULL)
2101+
PyObject *m = PyModule_Create(&winregmodule);
2102+
if (m == NULL) {
21022103
return NULL;
2103-
d = PyModule_GetDict(m);
2104+
}
21042105
PyHKEY_Type.tp_doc = PyHKEY_doc;
2105-
if (PyType_Ready(&PyHKEY_Type) < 0)
2106-
return NULL;
2107-
if (PyDict_SetItemString(d, "HKEYType",
2108-
(PyObject *)&PyHKEY_Type) != 0)
2109-
return NULL;
2110-
if (PyDict_SetItemString(d, "error",
2111-
PyExc_OSError) != 0)
2112-
return NULL;
2106+
if (PyType_Ready(&PyHKEY_Type) < 0) {
2107+
goto error;
2108+
}
2109+
if (PyModule_AddObjectRef(m, "HKEYType", (PyObject *)&PyHKEY_Type) < 0) {
2110+
goto error;
2111+
}
2112+
if (PyModule_AddObjectRef(m, "error", PyExc_OSError) < 0) {
2113+
goto error;
2114+
}
21132115

21142116
/* Add the relevant constants */
21152117
ADD_KEY(HKEY_CLASSES_ROOT);
@@ -2170,7 +2172,14 @@ PyMODINIT_FUNC PyInit_winreg(void)
21702172
ADD_INT(REG_RESOURCE_LIST);
21712173
ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
21722174
ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
2175+
2176+
#undef ADD_INT
2177+
21732178
return m;
2179+
2180+
error:
2181+
Py_DECREF(m);
2182+
return NULL;
21742183
}
21752184

21762185
#endif /* MS_WINDOWS_DESKTOP || MS_WINDOWS_SYSTEM || MS_WINDOWS_GAMES */

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.