bpo-31393: Fix the use of PyUnicode_READY(). (#3451)

This commit is contained in:
Serhiy Storchaka 2017-09-08 09:58:51 +03:00 committed by GitHub
parent 70c2dd306f
commit e3b2b4b8d9
5 changed files with 30 additions and 14 deletions

View File

@ -1467,7 +1467,10 @@ idna_converter(PyObject *obj, struct maybe_idna *data)
len = PyByteArray_Size(obj); len = PyByteArray_Size(obj);
} }
else if (PyUnicode_Check(obj)) { else if (PyUnicode_Check(obj)) {
if (PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) { if (PyUnicode_READY(obj) == -1) {
return 0;
}
if (PyUnicode_IS_COMPACT_ASCII(obj)) {
data->buf = PyUnicode_DATA(obj); data->buf = PyUnicode_DATA(obj);
len = PyUnicode_GET_LENGTH(obj); len = PyUnicode_GET_LENGTH(obj);
} }

View File

@ -27,7 +27,7 @@ all_name_chars(PyObject *o)
}; };
const unsigned char *s, *e; const unsigned char *s, *e;
if (PyUnicode_READY(o) == -1 || !PyUnicode_IS_ASCII(o)) if (!PyUnicode_IS_ASCII(o))
return 0; return 0;
s = PyUnicode_1BYTE_DATA(o); s = PyUnicode_1BYTE_DATA(o);
@ -63,6 +63,10 @@ intern_string_constants(PyObject *tuple)
for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
PyObject *v = PyTuple_GET_ITEM(tuple, i); PyObject *v = PyTuple_GET_ITEM(tuple, i);
if (PyUnicode_CheckExact(v)) { if (PyUnicode_CheckExact(v)) {
if (PyUnicode_READY(v) == -1) {
PyErr_Clear();
continue;
}
if (all_name_chars(v)) { if (all_name_chars(v)) {
PyObject *w = v; PyObject *w = v;
PyUnicode_InternInPlace(&v); PyUnicode_InternInPlace(&v);

View File

@ -30,9 +30,9 @@ class object "PyObject *" "&PyBaseObject_Type"
#define MCACHE_HASH_METHOD(type, name) \ #define MCACHE_HASH_METHOD(type, name) \
MCACHE_HASH((type)->tp_version_tag, \ MCACHE_HASH((type)->tp_version_tag, \
((PyASCIIObject *)(name))->hash) ((PyASCIIObject *)(name))->hash)
#define MCACHE_CACHEABLE_NAME(name) \ #define MCACHE_CACHEABLE_NAME(name) \
PyUnicode_CheckExact(name) && \ PyUnicode_CheckExact(name) && \
PyUnicode_READY(name) != -1 && \ PyUnicode_IS_READY(name) && \
PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE
struct method_cache_entry { struct method_cache_entry {

View File

@ -4185,10 +4185,13 @@ PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index)
void *data; void *data;
int kind; int kind;
if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) { if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument(); PyErr_BadArgument();
return (Py_UCS4)-1; return (Py_UCS4)-1;
} }
if (PyUnicode_READY(unicode) == -1) {
return (Py_UCS4)-1;
}
if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) { if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) {
PyErr_SetString(PyExc_IndexError, "string index out of range"); PyErr_SetString(PyExc_IndexError, "string index out of range");
return (Py_UCS4)-1; return (Py_UCS4)-1;
@ -11668,10 +11671,13 @@ unicode_getitem(PyObject *self, Py_ssize_t index)
enum PyUnicode_Kind kind; enum PyUnicode_Kind kind;
Py_UCS4 ch; Py_UCS4 ch;
if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) { if (!PyUnicode_Check(self)) {
PyErr_BadArgument(); PyErr_BadArgument();
return NULL; return NULL;
} }
if (PyUnicode_READY(self) == -1) {
return NULL;
}
if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) { if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) {
PyErr_SetString(PyExc_IndexError, "string index out of range"); PyErr_SetString(PyExc_IndexError, "string index out of range");
return NULL; return NULL;

View File

@ -5017,13 +5017,16 @@ import_all_from(PyObject *locals, PyObject *v)
PyErr_Clear(); PyErr_Clear();
break; break;
} }
if (skip_leading_underscores && if (skip_leading_underscores && PyUnicode_Check(name)) {
PyUnicode_Check(name) && if (PyUnicode_READY(name) == -1) {
PyUnicode_READY(name) != -1 && Py_DECREF(name);
PyUnicode_READ_CHAR(name, 0) == '_') err = -1;
{ break;
Py_DECREF(name); }
continue; if (PyUnicode_READ_CHAR(name, 0) == '_') {
Py_DECREF(name);
continue;
}
} }
value = PyObject_GetAttr(v, name); value = PyObject_GetAttr(v, name);
if (value == NULL) if (value == NULL)