]> granicus.if.org Git - python/commitdiff
[3.6] bpo-31393: Fix the use of PyUnicode_READY(). (GH-3451). (#3453)
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 8 Sep 2017 07:43:54 +0000 (10:43 +0300)
committerGitHub <noreply@github.com>
Fri, 8 Sep 2017 07:43:54 +0000 (10:43 +0300)
(cherry picked from commit e3b2b4b8d9e751b49e3550cb83ba39b54fdc377c)

Modules/socketmodule.c
Objects/codeobject.c
Objects/typeobject.c
Objects/unicodeobject.c
Python/ceval.c

index d006bd4def3ce4009b3ca64c673042c4a387b108..a351faa7f9fabfffd30b1f0116d8198c68bbbb93 100644 (file)
@@ -1464,7 +1464,10 @@ idna_converter(PyObject *obj, struct maybe_idna *data)
         len = PyByteArray_Size(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);
             len = PyUnicode_GET_LENGTH(obj);
         }
index 6de697ae3fd31c7ed41a5fb4666a71b85066af99..d45be4c96796e6de7547cbfffdb8a61f7b10a018 100644 (file)
@@ -22,8 +22,7 @@ all_name_chars(PyObject *o)
     static const unsigned char *name_chars = (unsigned char *)NAME_CHARS;
     const unsigned char *s, *e;
 
-    if (!PyUnicode_Check(o) || PyUnicode_READY(o) == -1 ||
-        !PyUnicode_IS_ASCII(o))
+    if (!PyUnicode_IS_ASCII(o))
         return 0;
 
     if (ok_name_char[*name_chars] == 0) {
@@ -64,6 +63,10 @@ intern_string_constants(PyObject *tuple)
     for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
         PyObject *v = PyTuple_GET_ITEM(tuple, i);
         if (PyUnicode_CheckExact(v)) {
+            if (PyUnicode_READY(v) == -1) {
+                PyErr_Clear();
+                continue;
+            }
             if (all_name_chars(v)) {
                 PyObject *w = v;
                 PyUnicode_InternInPlace(&v);
index 271b93575c6dcf429e27d7e52a2f52b09c84bd29..cb5e235628dd74007ca24adcbc6f3b095423f852 100644 (file)
@@ -22,9 +22,9 @@
 #define MCACHE_HASH_METHOD(type, name)                                  \
         MCACHE_HASH((type)->tp_version_tag,                     \
                     ((PyASCIIObject *)(name))->hash)
-#define MCACHE_CACHEABLE_NAME(name)                                     \
-        PyUnicode_CheckExact(name) &&                            \
-        PyUnicode_READY(name) != -1 &&                      \
+#define MCACHE_CACHEABLE_NAME(name)                             \
+        PyUnicode_CheckExact(name) &&                           \
+        PyUnicode_IS_READY(name) &&                             \
         PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE
 
 struct method_cache_entry {
index 1650370baccae9ea65413e775a3ef5a4781f6f25..1f221aff67c0d780aae360d8cdaeffca651209a1 100644 (file)
@@ -4210,10 +4210,13 @@ PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index)
     void *data;
     int kind;
 
-    if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) {
+    if (!PyUnicode_Check(unicode)) {
         PyErr_BadArgument();
         return (Py_UCS4)-1;
     }
+    if (PyUnicode_READY(unicode) == -1) {
+        return (Py_UCS4)-1;
+    }
     if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) {
         PyErr_SetString(PyExc_IndexError, "string index out of range");
         return (Py_UCS4)-1;
@@ -11706,10 +11709,13 @@ unicode_getitem(PyObject *self, Py_ssize_t index)
     enum PyUnicode_Kind kind;
     Py_UCS4 ch;
 
-    if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) {
+    if (!PyUnicode_Check(self)) {
         PyErr_BadArgument();
         return NULL;
     }
+    if (PyUnicode_READY(self) == -1) {
+        return NULL;
+    }
     if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) {
         PyErr_SetString(PyExc_IndexError, "string index out of range");
         return NULL;
index 4aa3250cd0bad0016fb6580e9470030f88c55700..2b74d0e5730f427787e79ea91448f27f9e624415 100644 (file)
@@ -5307,13 +5307,16 @@ import_all_from(PyObject *locals, PyObject *v)
                 PyErr_Clear();
             break;
         }
-        if (skip_leading_underscores &&
-            PyUnicode_Check(name) &&
-            PyUnicode_READY(name) != -1 &&
-            PyUnicode_READ_CHAR(name, 0) == '_')
-        {
-            Py_DECREF(name);
-            continue;
+        if (skip_leading_underscores && PyUnicode_Check(name)) {
+            if (PyUnicode_READY(name) == -1) {
+                Py_DECREF(name);
+                err = -1;
+                break;
+            }
+            if (PyUnicode_READ_CHAR(name, 0) == '_') {
+                Py_DECREF(name);
+                continue;
+            }
         }
         value = PyObject_GetAttr(v, name);
         if (value == NULL)