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

Skip to content

Commit e845c0f

Browse files
committed
Fixes for issue 1752184, ensuring type objects are always created
with a PyUnicode name.
1 parent 15c9746 commit e845c0f

4 files changed

Lines changed: 15 additions & 6 deletions

File tree

Objects/typeobject.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type_set_name(PyTypeObject *type, PyObject *value, void *context)
4545
{
4646
PyHeapTypeObject* et;
4747
char *tp_name;
48+
PyObject *tmp;
4849

4950
if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
5051
PyErr_Format(PyExc_TypeError,
@@ -62,14 +63,22 @@ type_set_name(PyTypeObject *type, PyObject *value, void *context)
6263
type->tp_name, Py_Type(value)->tp_name);
6364
return -1;
6465
}
65-
tp_name = PyUnicode_AsString(value);
66-
if (tp_name == NULL)
66+
67+
/* Check absence of null characters */
68+
tmp = PyUnicode_FromStringAndSize("\0", 1);
69+
if (tmp == NULL)
6770
return -1;
68-
if (strlen(tp_name) != (size_t)PyUnicode_GET_SIZE(value)) {
71+
if (PyUnicode_Contains(value, tmp) != 0) {
72+
Py_DECREF(tmp);
6973
PyErr_Format(PyExc_ValueError,
7074
"__name__ must not contain null bytes");
7175
return -1;
7276
}
77+
Py_DECREF(tmp);
78+
79+
tp_name = PyUnicode_AsString(value);
80+
if (tp_name == NULL)
81+
return -1;
7382

7483
et = (PyHeapTypeObject*)type;
7584

Parser/asdl_c.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ def visitModule(self, mod):
415415
}
416416
PyTuple_SET_ITEM(fnames, i, field);
417417
}
418-
result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
418+
result = PyObject_CallFunction((PyObject*)&PyType_Type, "U(O){sOss}",
419419
type, base, "_fields", fnames, "__module__", "_ast");
420420
Py_DECREF(fnames);
421421
return (PyTypeObject*)result;

Python/Python-ast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int
410410
}
411411
PyTuple_SET_ITEM(fnames, i, field);
412412
}
413-
result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}",
413+
result = PyObject_CallFunction((PyObject*)&PyType_Type, "U(O){sOss}",
414414
type, base, "_fields", fnames, "__module__", "_ast");
415415
Py_DECREF(fnames);
416416
return (PyTypeObject*)result;

Python/errors.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
608608
goto failure;
609609
}
610610
/* Create a real new-style class. */
611-
result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
611+
result = PyObject_CallFunction((PyObject *)&PyType_Type, "UOO",
612612
dot+1, bases, dict);
613613
failure:
614614
Py_XDECREF(bases);

0 commit comments

Comments
 (0)