bpo-1635741: Fix potential refleaks in binascii module (GH-18613)

This commit is contained in:
Hai Shi 2020-03-12 00:50:52 +08:00 committed by GitHub
parent 41fbf865a3
commit aa0c0808ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 45 additions and 7 deletions

View File

@ -66,6 +66,12 @@ typedef struct binascii_state {
PyObject *Incomplete; PyObject *Incomplete;
} binascii_state; } binascii_state;
static binascii_state *
get_binascii_state(PyObject *module)
{
return (binascii_state *)PyModule_GetState(module);
}
/* /*
** hqx lookup table, ascii->binary. ** hqx lookup table, ascii->binary.
*/ */
@ -1606,9 +1612,9 @@ static struct PyMethodDef binascii_module_methods[] = {
PyDoc_STRVAR(doc_binascii, "Conversion between binary data and ASCII"); PyDoc_STRVAR(doc_binascii, "Conversion between binary data and ASCII");
static int static int
binascii_exec(PyObject *m) { binascii_exec(PyObject *module) {
int result; int result;
binascii_state *state = PyModule_GetState(m); binascii_state *state = PyModule_GetState(module);
if (state == NULL) { if (state == NULL) {
return -1; return -1;
} }
@ -1617,8 +1623,10 @@ binascii_exec(PyObject *m) {
if (state->Error == NULL) { if (state->Error == NULL) {
return -1; return -1;
} }
result = PyModule_AddObject(m, "Error", state->Error); Py_INCREF(state->Error);
result = PyModule_AddObject(module, "Error", state->Error);
if (result == -1) { if (result == -1) {
Py_DECREF(state->Error);
return -1; return -1;
} }
@ -1626,8 +1634,10 @@ binascii_exec(PyObject *m) {
if (state->Incomplete == NULL) { if (state->Incomplete == NULL) {
return -1; return -1;
} }
result = PyModule_AddObject(m, "Incomplete", state->Incomplete); Py_INCREF(state->Incomplete);
result = PyModule_AddObject(module, "Incomplete", state->Incomplete);
if (result == -1) { if (result == -1) {
Py_DECREF(state->Incomplete);
return -1; return -1;
} }
@ -1639,6 +1649,34 @@ static PyModuleDef_Slot binascii_slots[] = {
{0, NULL} {0, NULL}
}; };
static int
binascii_traverse(PyObject *module, visitproc visit, void *arg)
{
binascii_state *state = get_binascii_state(module);
if (state) {
Py_VISIT(state->Error);
Py_VISIT(state->Incomplete);
}
return 0;
}
static int
binascii_clear(PyObject *module)
{
binascii_state *state = get_binascii_state(module);
if (state) {
Py_CLEAR(state->Error);
Py_CLEAR(state->Incomplete);
}
return 0;
}
static void
binascii_free(void *module)
{
binascii_clear((PyObject *)module);
}
static struct PyModuleDef binasciimodule = { static struct PyModuleDef binasciimodule = {
PyModuleDef_HEAD_INIT, PyModuleDef_HEAD_INIT,
"binascii", "binascii",
@ -1646,9 +1684,9 @@ static struct PyModuleDef binasciimodule = {
sizeof(binascii_state), sizeof(binascii_state),
binascii_module_methods, binascii_module_methods,
binascii_slots, binascii_slots,
NULL, binascii_traverse,
NULL, binascii_clear,
NULL binascii_free
}; };
PyMODINIT_FUNC PyMODINIT_FUNC