cpython (merge 3.5 -> default): Issue #25961: Disallowed null characters in the type name.

https://hg.python.org/cpython/rev/1ab7bcd4e176 changeset: 99728:1ab7bcd4e176 parent: 99725:965f0bddac6d parent: 99727:d2417971c934 user: Serhiy Storchaka <storchaka@gmail.com> date: Wed Dec 30 21:41:53 2015 +0200 summary: Issue #25961: Disallowed null characters in the type name. Simplified testing for null characters in __name__ setter. files: Misc/NEWS | 2 + Objects/typeobject.c | 34 +++++++++++++++---------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Misc/NEWS b/Misc/NEWS --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,8 @@ Core and Builtins ----------------- +- Issue #25961: Disallowed null characters in the type name. + - Issue #25973: Fix segfault when an invalid nonlocal statement binds a name starting with two underscores. diff --git a/Objects/typeobject.c b/Objects/typeobject.c --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -401,8 +401,8 @@ static int type_set_name(PyTypeObject *type, PyObject *value, void *context) { - char *tp_name; - PyObject *tmp; + const char *tp_name; + Py_ssize_t name_size; if (!check_set_special_type_attr(type, value, "__name__")) return -1; @@ -413,21 +413,14 @@ return -1; } - /* Check absence of null characters */ - tmp = PyUnicode_FromStringAndSize("\0", 1); - if (tmp == NULL) - return -1; - if (PyUnicode_Contains(value, tmp) != 0) { - Py_DECREF(tmp); - PyErr_Format(PyExc_ValueError, - "__name__ must not contain null bytes"); - return -1; - } - Py_DECREF(tmp); - - tp_name = _PyUnicode_AsString(value); + tp_name = PyUnicode_AsUTF8AndSize(value, &name_size); if (tp_name == NULL) return -1; + if (strlen(tp_name) != (size_t)name_size) { + PyErr_SetString(PyExc_ValueError, + "type name must not contain null characters"); + return -1; + } type->tp_name = tp_name; Py_INCREF(value); @@ -2284,8 +2277,8 @@ PyTypeObject *type = NULL, *base, *tmptype, *winner; PyHeapTypeObject *et; PyMemberDef *mp; - Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak; - int j, may_add_dict, may_add_weak; + Py_ssize_t i, nbases, nslots, slotoffset, name_size; + int j, may_add_dict, may_add_weak, add_dict, add_weak; _Py_IDENTIFIER(__qualname__); _Py_IDENTIFIER(__slots__); @@ -2508,9 +2501,14 @@ type->tp_as_sequence = &et->as_sequence; type->tp_as_mapping = &et->as_mapping; type->tp_as_buffer = &et->as_buffer; - type->tp_name = _PyUnicode_AsString(name); + type->tp_name = PyUnicode_AsUTF8AndSize(name, &name_size); if (!type->tp_name) goto error; + if (strlen(type->tp_name) != (size_t)name_size) { + PyErr_SetString(PyExc_ValueError, + "type name must not contain null characters"); + goto error; + } /* Set tp_base and tp_bases */ type->tp_bases = bases; -- Repository URL: https://hg.python.org/cpython
participants (1)
-
serhiy.storchaka