Kaydet (Commit) 8fea252a authored tarafından Victor Stinner's avatar Victor Stinner

Issue #18520: fix reference leak in _PySys_Init()

üst 22da9677
...@@ -1573,6 +1573,17 @@ _PySys_Init(void) ...@@ -1573,6 +1573,17 @@ _PySys_Init(void)
if (m == NULL) if (m == NULL)
return NULL; return NULL;
sysdict = PyModule_GetDict(m); sysdict = PyModule_GetDict(m);
#define SET_SYS_FROM_STRING_BORROW(key, value) \
do { \
int res; \
PyObject *v = (value); \
if (v == NULL) \
return NULL; \
res = PyDict_SetItemString(sysdict, key, v); \
if (res < 0) { \
return NULL; \
} \
} while (0)
#define SET_SYS_FROM_STRING(key, value) \ #define SET_SYS_FROM_STRING(key, value) \
do { \ do { \
int res; \ int res; \
...@@ -1580,8 +1591,8 @@ _PySys_Init(void) ...@@ -1580,8 +1591,8 @@ _PySys_Init(void)
if (v == NULL) \ if (v == NULL) \
return NULL; \ return NULL; \
res = PyDict_SetItemString(sysdict, key, v); \ res = PyDict_SetItemString(sysdict, key, v); \
Py_DECREF(v); \
if (res < 0) { \ if (res < 0) { \
Py_DECREF(v); \
return NULL; \ return NULL; \
} \ } \
} while (0) } while (0)
...@@ -1606,10 +1617,10 @@ _PySys_Init(void) ...@@ -1606,10 +1617,10 @@ _PySys_Init(void)
/* stdin/stdout/stderr are now set by pythonrun.c */ /* stdin/stdout/stderr are now set by pythonrun.c */
SET_SYS_FROM_STRING("__displayhook__", SET_SYS_FROM_STRING_BORROW("__displayhook__",
PyDict_GetItemString(sysdict, "displayhook")); PyDict_GetItemString(sysdict, "displayhook"));
SET_SYS_FROM_STRING("__excepthook__", SET_SYS_FROM_STRING_BORROW("__excepthook__",
PyDict_GetItemString(sysdict, "excepthook")); PyDict_GetItemString(sysdict, "excepthook"));
SET_SYS_FROM_STRING("version", SET_SYS_FROM_STRING("version",
PyUnicode_FromString(Py_GetVersion())); PyUnicode_FromString(Py_GetVersion()));
SET_SYS_FROM_STRING("hexversion", SET_SYS_FROM_STRING("hexversion",
...@@ -1679,9 +1690,9 @@ _PySys_Init(void) ...@@ -1679,9 +1690,9 @@ _PySys_Init(void)
else { else {
Py_INCREF(warnoptions); Py_INCREF(warnoptions);
} }
SET_SYS_FROM_STRING("warnoptions", warnoptions); SET_SYS_FROM_STRING_BORROW("warnoptions", warnoptions);
SET_SYS_FROM_STRING("_xoptions", get_xoptions()); SET_SYS_FROM_STRING_BORROW("_xoptions", get_xoptions());
/* version_info */ /* version_info */
if (VersionInfoType.tp_name == NULL) { if (VersionInfoType.tp_name == NULL) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment