Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 288cb25

Browse files
Issue #25961: Disallowed null characters in the type name.
Simplified testing for null characters in __name__ setter.
2 parents 58f8833 + 42bf8fc commit 288cb25

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Release date: tba
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #25961: Disallowed null characters in the type name.
14+
1315
- Issue #25973: Fix segfault when an invalid nonlocal statement binds a name
1416
starting with two underscores.
1517

Objects/typeobject.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ type_qualname(PyTypeObject *type, void *context)
401401
static int
402402
type_set_name(PyTypeObject *type, PyObject *value, void *context)
403403
{
404-
char *tp_name;
405-
PyObject *tmp;
404+
const char *tp_name;
405+
Py_ssize_t name_size;
406406

407407
if (!check_set_special_type_attr(type, value, "__name__"))
408408
return -1;
@@ -413,21 +413,14 @@ type_set_name(PyTypeObject *type, PyObject *value, void *context)
413413
return -1;
414414
}
415415

416-
/* Check absence of null characters */
417-
tmp = PyUnicode_FromStringAndSize("\0", 1);
418-
if (tmp == NULL)
416+
tp_name = PyUnicode_AsUTF8AndSize(value, &name_size);
417+
if (tp_name == NULL)
419418
return -1;
420-
if (PyUnicode_Contains(value, tmp) != 0) {
421-
Py_DECREF(tmp);
422-
PyErr_Format(PyExc_ValueError,
423-
"__name__ must not contain null bytes");
419+
if (strlen(tp_name) != (size_t)name_size) {
420+
PyErr_SetString(PyExc_ValueError,
421+
"type name must not contain null characters");
424422
return -1;
425423
}
426-
Py_DECREF(tmp);
427-
428-
tp_name = _PyUnicode_AsString(value);
429-
if (tp_name == NULL)
430-
return -1;
431424

432425
type->tp_name = tp_name;
433426
Py_INCREF(value);
@@ -2284,8 +2277,8 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
22842277
PyTypeObject *type = NULL, *base, *tmptype, *winner;
22852278
PyHeapTypeObject *et;
22862279
PyMemberDef *mp;
2287-
Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
2288-
int j, may_add_dict, may_add_weak;
2280+
Py_ssize_t i, nbases, nslots, slotoffset, name_size;
2281+
int j, may_add_dict, may_add_weak, add_dict, add_weak;
22892282
_Py_IDENTIFIER(__qualname__);
22902283
_Py_IDENTIFIER(__slots__);
22912284

@@ -2508,9 +2501,14 @@ type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
25082501
type->tp_as_sequence = &et->as_sequence;
25092502
type->tp_as_mapping = &et->as_mapping;
25102503
type->tp_as_buffer = &et->as_buffer;
2511-
type->tp_name = _PyUnicode_AsString(name);
2504+
type->tp_name = PyUnicode_AsUTF8AndSize(name, &name_size);
25122505
if (!type->tp_name)
25132506
goto error;
2507+
if (strlen(type->tp_name) != (size_t)name_size) {
2508+
PyErr_SetString(PyExc_ValueError,
2509+
"type name must not contain null characters");
2510+
goto error;
2511+
}
25142512

25152513
/* Set tp_base and tp_bases */
25162514
type->tp_bases = bases;

0 commit comments

Comments
 (0)