aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Coleman <brianfcoleman@gmail.com>2017-03-02 22:21:53 +0000
committerSerhiy Storchaka <storchaka@gmail.com>2017-03-03 00:21:53 +0200
commita6e84933d204f807e0e81b6a2237193b2e8ab89a (patch)
treec8675269eb9fca2e35fbd4864db1e1afbed0da71
parent65bd0bdf3d285e3917d66c600c95cb0842e3b3be (diff)
downloadcpython3-a6e84933d204f807e0e81b6a2237193b2e8ab89a.tar.gz
bpo-29683 - Fixes to _PyCode_SetExtra when co_extra->ce->extras is (#402)
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.
-rw-r--r--Misc/NEWS3
-rw-r--r--Objects/codeobject.c24
2 files changed, 17 insertions, 10 deletions
diff --git a/Misc/NEWS b/Misc/NEWS
index 33559a7abb..a57c476a0b 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.6.1 release candidate 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.
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 0d8a675f9f..df8b9538fe 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -856,16 +856,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;
}
@@ -874,20 +873,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*));
- 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;
+ 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;