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 winsound init #103385

Merged
merged 3 commits into from
Apr 9, 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
30 changes: 14 additions & 16 deletions PC/winsound.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,11 @@ static struct PyMethodDef sound_methods[] =
{NULL, NULL}
};

static void
add_define(PyObject *dict, const char *key, long value)
{
PyObject *k = PyUnicode_FromString(key);
PyObject *v = PyLong_FromLong(value);
if (v && k) {
PyDict_SetItem(dict, k, v);
}
Py_XDECREF(k);
Py_XDECREF(v);
}

#define ADD_DEFINE(tok) add_define(dict,#tok,tok)
#define ADD_DEFINE(CONST) do { \
if (PyModule_AddIntConstant(module, #CONST, CONST) < 0) { \
goto error; \
} \
} while (0)


static struct PyModuleDef winsoundmodule = {
Expand All @@ -232,11 +224,10 @@ static struct PyModuleDef winsoundmodule = {
PyMODINIT_FUNC
PyInit_winsound(void)
{
PyObject *dict;
PyObject *module = PyModule_Create(&winsoundmodule);
if (module == NULL)
if (module == NULL) {
return NULL;
dict = PyModule_GetDict(module);
}

ADD_DEFINE(SND_ASYNC);
ADD_DEFINE(SND_NODEFAULT);
Expand All @@ -254,5 +245,12 @@ PyInit_winsound(void)
ADD_DEFINE(MB_ICONEXCLAMATION);
ADD_DEFINE(MB_ICONHAND);
ADD_DEFINE(MB_ICONQUESTION);

kumaraditya303 marked this conversation as resolved.
Show resolved Hide resolved
#undef ADD_DEFINE

return module;

error:
Py_DECREF(module);
return NULL;
}