Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-83004: Harden winreg init #103386

Merged
merged 1 commit into from
Apr 10, 2023
Merged
Changes from all commits
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
65 changes: 37 additions & 28 deletions PC/winreg.c
Original file line number Diff line number Diff line change
Expand Up @@ -2059,27 +2059,29 @@ static struct PyMethodDef winreg_methods[] = {
NULL,
};

static void
insint(PyObject * d, char * name, long value)
{
PyObject *v = PyLong_FromLong(value);
if (!v || PyDict_SetItemString(d, name, v))
PyErr_Clear();
Py_XDECREF(v);
}

#define ADD_INT(val) insint(d, #val, val)
#define ADD_INT(VAL) do { \
if (PyModule_AddIntConstant(m, #VAL, VAL) < 0) { \
goto error; \
} \
} while (0)

static void
inskey(PyObject * d, char * name, HKEY key)
static int
inskey(PyObject *mod, char *name, HKEY key)
{
PyObject *v = PyLong_FromVoidPtr(key);
if (!v || PyDict_SetItemString(d, name, v))
PyErr_Clear();
Py_XDECREF(v);
if (v == NULL) {
return -1;
}
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 = {
Expand All @@ -2096,20 +2098,20 @@ static struct PyModuleDef winregmodule = {

PyMODINIT_FUNC PyInit_winreg(void)
{
PyObject *m, *d;
m = PyModule_Create(&winregmodule);
if (m == NULL)
PyObject *m = PyModule_Create(&winregmodule);
if (m == NULL) {
return NULL;
d = PyModule_GetDict(m);
}
PyHKEY_Type.tp_doc = PyHKEY_doc;
if (PyType_Ready(&PyHKEY_Type) < 0)
return NULL;
if (PyDict_SetItemString(d, "HKEYType",
(PyObject *)&PyHKEY_Type) != 0)
return NULL;
if (PyDict_SetItemString(d, "error",
PyExc_OSError) != 0)
return NULL;
if (PyType_Ready(&PyHKEY_Type) < 0) {
goto error;
}
if (PyModule_AddObjectRef(m, "HKEYType", (PyObject *)&PyHKEY_Type) < 0) {
goto error;
}
if (PyModule_AddObjectRef(m, "error", PyExc_OSError) < 0) {
goto error;
}

/* Add the relevant constants */
ADD_KEY(HKEY_CLASSES_ROOT);
Expand Down Expand Up @@ -2170,7 +2172,14 @@ PyMODINIT_FUNC PyInit_winreg(void)
ADD_INT(REG_RESOURCE_LIST);
ADD_INT(REG_FULL_RESOURCE_DESCRIPTOR);
ADD_INT(REG_RESOURCE_REQUIREMENTS_LIST);

#undef ADD_INT
Copy link
Member

Choose a reason for hiding this comment

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

Should we undef the ADD_KEY here also?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes ideally, but not worth a separate PR, IMO.

Copy link
Member

Choose a reason for hiding this comment

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

Trying to merge this to #103250 , I'll add it is this PR.


return m;

error:
Py_DECREF(m);
return NULL;
}

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