Skip to content

Commit 6a9122c

Browse files
brianfcolemanvstinner
authored andcommitted
bpo-29683 - Fixes to _PyCode_SetExtra when co_extra->ce->extras is (#376)
allocated. On PyMem_Realloc failure, _PyCode_SetExtra should free co_extra if co_extra->ce_extras could not be allocated. On PyMem_Realloc success, _PyCode_SetExtra should set all unused slots in co_extra->ce_extras to NULL.
1 parent 275104e commit 6a9122c

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- bpo-29683: Fixes to memory allocation in _PyCode_SetExtra. Patch by
14+
Brian Coleman.
15+
1316
- bpo-29684: Fix minor regression of PyEval_CallObjectWithKeywords.
1417
It should raise TypeError when kwargs is not a dict. But it might
1518
cause segv when args=NULL and kwargs is not a dict.

Objects/codeobject.c

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -861,16 +861,15 @@ _PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
861861
_PyCodeObjectExtra *co_extra = (_PyCodeObjectExtra *) o->co_extra;
862862

863863
if (co_extra == NULL) {
864-
o->co_extra = (_PyCodeObjectExtra*) PyMem_Malloc(
865-
sizeof(_PyCodeObjectExtra));
866-
if (o->co_extra == NULL) {
864+
co_extra = PyMem_Malloc(sizeof(_PyCodeObjectExtra));
865+
if (co_extra == NULL) {
867866
return -1;
868867
}
869-
co_extra = (_PyCodeObjectExtra *) o->co_extra;
870868

871869
co_extra->ce_extras = PyMem_Malloc(
872870
tstate->co_extra_user_count * sizeof(void*));
873871
if (co_extra->ce_extras == NULL) {
872+
PyMem_Free(co_extra);
874873
return -1;
875874
}
876875

@@ -879,20 +878,25 @@ _PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
879878
for (Py_ssize_t i = 0; i < co_extra->ce_size; i++) {
880879
co_extra->ce_extras[i] = NULL;
881880
}
881+
882+
o->co_extra = co_extra;
882883
}
883884
else if (co_extra->ce_size <= index) {
884-
co_extra->ce_extras = PyMem_Realloc(
885+
void** ce_extras = PyMem_Realloc(
885886
co_extra->ce_extras, tstate->co_extra_user_count * sizeof(void*));
886887

887-
if (co_extra->ce_extras == NULL) {
888+
if (ce_extras == NULL) {
888889
return -1;
889890
}
890891

891-
co_extra->ce_size = tstate->co_extra_user_count;
892-
893-
for (Py_ssize_t i = co_extra->ce_size; i < co_extra->ce_size; i++) {
894-
co_extra->ce_extras[i] = NULL;
892+
for (Py_ssize_t i = co_extra->ce_size;
893+
i < tstate->co_extra_user_count;
894+
i++) {
895+
ce_extras[i] = NULL;
895896
}
897+
898+
co_extra->ce_extras = ce_extras;
899+
co_extra->ce_size = tstate->co_extra_user_count;
896900
}
897901

898902
co_extra->ce_extras[index] = extra;

0 commit comments

Comments
 (0)