mirror of
https://github.com/python/cpython.git
synced 2025-12-18 23:02:07 +00:00
gh-83004: Harden winreg init (#103386)
This commit is contained in:
parent
c3cd3d1078
commit
5ed2f19b39
1 changed files with 37 additions and 28 deletions
65
PC/winreg.c
65
PC/winreg.c
|
|
@ -2059,27 +2059,29 @@ static struct PyMethodDef winreg_methods[] = {
|
||||||
NULL,
|
NULL,
|
||||||
};
|
};
|
||||||
|
|
||||||
static void
|
#define ADD_INT(VAL) do { \
|
||||||
insint(PyObject * d, char * name, long value)
|
if (PyModule_AddIntConstant(m, #VAL, VAL) < 0) { \
|
||||||
{
|
goto error; \
|
||||||
PyObject *v = PyLong_FromLong(value);
|
} \
|
||||||
if (!v || PyDict_SetItemString(d, name, v))
|
} while (0)
|
||||||
PyErr_Clear();
|
|
||||||
Py_XDECREF(v);
|
|
||||||
}
|
|
||||||
|
|
||||||
#define ADD_INT(val) insint(d, #val, val)
|
static int
|
||||||
|
inskey(PyObject *mod, char *name, HKEY key)
|
||||||
static void
|
|
||||||
inskey(PyObject * d, char * name, HKEY key)
|
|
||||||
{
|
{
|
||||||
PyObject *v = PyLong_FromVoidPtr(key);
|
PyObject *v = PyLong_FromVoidPtr(key);
|
||||||
if (!v || PyDict_SetItemString(d, name, v))
|
if (v == NULL) {
|
||||||
PyErr_Clear();
|
return -1;
|
||||||
Py_XDECREF(v);
|
}
|
||||||
|
int rc = PyModule_AddObjectRef(mod, name, v);
|
||||||
|
Py_DECREF(v);
|
||||||
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define ADD_KEY(val) inskey(d, #val, val)
|
#define ADD_KEY(VAL) do { \
|
||||||
|
if (inskey(m, #VAL, VAL) < 0) { \
|
||||||
|
goto error; \
|
||||||
|
} \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
|
|
||||||
static struct PyModuleDef winregmodule = {
|
static struct PyModuleDef winregmodule = {
|
||||||
|
|
@ -2096,20 +2098,20 @@ static struct PyModuleDef winregmodule = {
|
||||||
|
|
||||||
PyMODINIT_FUNC PyInit_winreg(void)
|
PyMODINIT_FUNC PyInit_winreg(void)
|
||||||
{
|
{
|
||||||
PyObject *m, *d;
|
PyObject *m = PyModule_Create(&winregmodule);
|
||||||
m = PyModule_Create(&winregmodule);
|
if (m == NULL) {
|
||||||
if (m == NULL)
|
|
||||||
return NULL;
|
return NULL;
|
||||||
d = PyModule_GetDict(m);
|
}
|
||||||
PyHKEY_Type.tp_doc = PyHKEY_doc;
|
PyHKEY_Type.tp_doc = PyHKEY_doc;
|
||||||
if (PyType_Ready(&PyHKEY_Type) < 0)
|
if (PyType_Ready(&PyHKEY_Type) < 0) {
|
||||||
return NULL;
|
goto error;
|
||||||
if (PyDict_SetItemString(d, "HKEYType",
|
}
|
||||||
(PyObject *)&PyHKEY_Type) != 0)
|
if (PyModule_AddObjectRef(m, "HKEYType", (PyObject *)&PyHKEY_Type) < 0) {
|
||||||
return NULL;
|
goto error;
|
||||||
if (PyDict_SetItemString(d, "error",
|
}
|
||||||
PyExc_OSError) != 0)
|
if (PyModule_AddObjectRef(m, "error", PyExc_OSError) < 0) {
|
||||||
return NULL;
|
goto error;
|
||||||
|
}
|
||||||
|
|
||||||
/* Add the relevant constants */
|
/* Add the relevant constants */
|
||||||
ADD_KEY(HKEY_CLASSES_ROOT);
|
ADD_KEY(HKEY_CLASSES_ROOT);
|
||||||
|
|
@ -2170,7 +2172,14 @@ PyMODINIT_FUNC PyInit_winreg(void)
|
||||||
ADD_INT(REG_RESOURCE_LIST);
|
ADD_INT(REG_RESOURCE_LIST);
|
||||||
ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
|
ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
|
||||||
ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
|
ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);
|
||||||
|
|
||||||
|
#undef ADD_INT
|
||||||
|
|
||||||
return m;
|
return m;
|
||||||
|
|
||||||
|
error:
|
||||||
|
Py_DECREF(m);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* MS_WINDOWS_DESKTOP || MS_WINDOWS_SYSTEM || MS_WINDOWS_GAMES */
|
#endif /* MS_WINDOWS_DESKTOP || MS_WINDOWS_SYSTEM || MS_WINDOWS_GAMES */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue