summaryrefslogtreecommitdiffstats
path: root/PC
diff options
context:
space:
mode:
authorErlend E. Aasland <erlend.aasland@protonmail.com>2023-04-09 20:00:22 (GMT)
committerGitHub <noreply@github.com>2023-04-09 20:00:22 (GMT)
commit6d97e521169b07851cbbf7fccbf9bef3f41e3ce0 (patch)
tree6a0b9c79aafbfb4461586b3a3e4fb96f1f070d96 /PC
parentf65fdbb8fdc16d6b1eddaecc92d0dfa131761090 (diff)
downloadcpython-6d97e521169b07851cbbf7fccbf9bef3f41e3ce0.zip
cpython-6d97e521169b07851cbbf7fccbf9bef3f41e3ce0.tar.gz
cpython-6d97e521169b07851cbbf7fccbf9bef3f41e3ce0.tar.bz2
gh-83004: Harden winsound init (#103385)
Diffstat (limited to 'PC')
-rw-r--r--PC/winsound.c30
1 files changed, 14 insertions, 16 deletions
diff --git a/PC/winsound.c b/PC/winsound.c
index 65025dd..bae8e44 100644
--- a/PC/winsound.c
+++ b/PC/winsound.c
@@ -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 = {
@@ -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);
@@ -254,5 +245,12 @@ PyInit_winsound(void)
ADD_DEFINE(MB_ICONEXCLAMATION);
ADD_DEFINE(MB_ICONHAND);
ADD_DEFINE(MB_ICONQUESTION);
+
+#undef ADD_DEFINE
+
return module;
+
+error:
+ Py_DECREF(module);
+ return NULL;
}