Skip to content

bpo-29683 - Fixes to _PyCode_SetExtra when co_extra->ce->extras is allocated #376

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

Merged
merged 1 commit into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1?
Core and Builtins
-----------------

- bpo-29683: Fixes to memory allocation in _PyCode_SetExtra. Patch by
Brian Coleman.

- bpo-29684: Fix minor regression of PyEval_CallObjectWithKeywords.
It should raise TypeError when kwargs is not a dict. But it might
cause segv when args=NULL and kwargs is not a dict.
Expand Down
24 changes: 14 additions & 10 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -861,16 +861,15 @@ _PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
_PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;

if (co_extra == NULL) {
o->co_extra = (_PyCodeObjectExtra*) PyMem_Malloc(
sizeof(_PyCodeObjectExtra));
if (o->co_extra == NULL) {
co_extra = PyMem_Malloc(sizeof(_PyCodeObjectExtra));
if (co_extra == NULL) {
return -1;
}
co_extra = (_PyCodeObjectExtra *) o->co_extra;

co_extra->ce_extras = PyMem_Malloc(
tstate->co_extra_user_count * sizeof(void*));
if (co_extra->ce_extras == NULL) {
PyMem_Free(co_extra);
return -1;
}

Expand All @@ -879,20 +878,25 @@ _PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
co_extra->ce_extras[i] = NULL;
}

o->co_extra = co_extra;
}
else if (co_extra->ce_size <= index) {
co_extra->ce_extras = PyMem_Realloc(
void** ce_extras = PyMem_Realloc(
co_extra->ce_extras, tstate->co_extra_user_count * sizeof(void*));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously allocated co_extra->ce_extras is leaked if PyMem_Realloc() returns NULL. It would be better to save the result of PyMem_Realloc() to the variable and check it for NULL first than assign it to co_extra->ce_extras.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made this change


if (co_extra->ce_extras == NULL) {
if (ce_extras == NULL) {
return -1;
}

co_extra->ce_size = tstate->co_extra_user_count;

for (Py_ssize_t i = co_extra->ce_size; i < co_extra->ce_size; i++) {
co_extra->ce_extras[i] = NULL;
for (Py_ssize_t i = co_extra->ce_size;
i < tstate->co_extra_user_count;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch.

i++) {
ce_extras[i] = NULL;
}

co_extra->ce_extras = ce_extras;
co_extra->ce_size = tstate->co_extra_user_count;
}

co_extra->ce_extras[index] = extra;
Expand Down