-
-
Notifications
You must be signed in to change notification settings - Fork 32k
bpo-1635741: Fix potential refleaks in binascii module #18613
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,12 @@ typedef struct binascii_state { | |
PyObject *Incomplete; | ||
} binascii_state; | ||
|
||
static binascii_state * | ||
get_binascii_state(PyObject *module) | ||
{ | ||
return (binascii_state *)PyModule_GetState(module); | ||
} | ||
|
||
/* | ||
** hqx lookup table, ascii->binary. | ||
*/ | ||
|
@@ -1617,17 +1623,21 @@ binascii_exec(PyObject *m) { | |
if (state->Error == NULL) { | ||
return -1; | ||
} | ||
Py_INCREF(state->Error); | ||
result = PyModule_AddObject(m, "Error", state->Error); | ||
if (result == -1) { | ||
Py_DECREF(state->Error); | ||
return -1; | ||
} | ||
|
||
state->Incomplete = PyErr_NewException("binascii.Incomplete", NULL, NULL); | ||
if (state->Incomplete == NULL) { | ||
return -1; | ||
} | ||
Py_INCREF(state->Incomplete); | ||
result = PyModule_AddObject(m, "Incomplete", state->Incomplete); | ||
if (result == -1) { | ||
Py_DECREF(state->Incomplete); | ||
return -1; | ||
} | ||
|
||
|
@@ -1639,16 +1649,46 @@ static PyModuleDef_Slot binascii_slots[] = { | |
{0, NULL} | ||
}; | ||
|
||
static int | ||
binascii_traverse(PyObject *m, visitproc visit, void *arg) | ||
{ | ||
binascii_state *state = get_binascii_state(m); | ||
if (state == NULL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will update this behavior soon. |
||
return -1; | ||
} | ||
Py_VISIT(state->Error); | ||
Py_VISIT(state->Incomplete); | ||
return 0; | ||
} | ||
|
||
static int | ||
binascii_clear(PyObject *m) | ||
{ | ||
binascii_state *state = get_binascii_state(m); | ||
if (state == NULL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that state can be NULL. If it's NULL, it's a bug in Python. No? Same remark in binascii_traverse(). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do some debug in my vm:
What's the reason? |
||
return -1; | ||
} | ||
Py_CLEAR(state->Error); | ||
Py_CLEAR(state->Incomplete); | ||
return 0; | ||
} | ||
|
||
static void | ||
binascii_free(void *m) | ||
{ | ||
binascii_clear((PyObject *)m); | ||
} | ||
|
||
static struct PyModuleDef binasciimodule = { | ||
PyModuleDef_HEAD_INIT, | ||
"binascii", | ||
doc_binascii, | ||
sizeof(binascii_state), | ||
binascii_module_methods, | ||
binascii_slots, | ||
NULL, | ||
NULL, | ||
NULL | ||
binascii_traverse, | ||
binascii_clear, | ||
binascii_free | ||
}; | ||
|
||
PyMODINIT_FUNC | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: I would prefer to use longer variable name than just "m". Rename "m" to "mod" or "module". Same remark if following functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, no probleam, i will update it.