Skip to content

Commit e3b2b4b

Browse files
bpo-31393: Fix the use of PyUnicode_READY(). (#3451)
1 parent 70c2dd3 commit e3b2b4b

File tree

5 files changed

+30
-14
lines changed

5 files changed

+30
-14
lines changed

Modules/socketmodule.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,10 @@ idna_converter(PyObject *obj, struct maybe_idna *data)
14671467
len = PyByteArray_Size(obj);
14681468
}
14691469
else if (PyUnicode_Check(obj)) {
1470-
if (PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) {
1470+
if (PyUnicode_READY(obj) == -1) {
1471+
return 0;
1472+
}
1473+
if (PyUnicode_IS_COMPACT_ASCII(obj)) {
14711474
data->buf = PyUnicode_DATA(obj);
14721475
len = PyUnicode_GET_LENGTH(obj);
14731476
}

Objects/codeobject.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ all_name_chars(PyObject *o)
2727
};
2828
const unsigned char *s, *e;
2929

30-
if (PyUnicode_READY(o) == -1 || !PyUnicode_IS_ASCII(o))
30+
if (!PyUnicode_IS_ASCII(o))
3131
return 0;
3232

3333
s = PyUnicode_1BYTE_DATA(o);
@@ -63,6 +63,10 @@ intern_string_constants(PyObject *tuple)
6363
for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
6464
PyObject *v = PyTuple_GET_ITEM(tuple, i);
6565
if (PyUnicode_CheckExact(v)) {
66+
if (PyUnicode_READY(v) == -1) {
67+
PyErr_Clear();
68+
continue;
69+
}
6670
if (all_name_chars(v)) {
6771
PyObject *w = v;
6872
PyUnicode_InternInPlace(&v);

Objects/typeobject.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class object "PyObject *" "&PyBaseObject_Type"
3030
#define MCACHE_HASH_METHOD(type, name) \
3131
MCACHE_HASH((type)->tp_version_tag, \
3232
((PyASCIIObject *)(name))->hash)
33-
#define MCACHE_CACHEABLE_NAME(name) \
34-
PyUnicode_CheckExact(name) && \
35-
PyUnicode_READY(name) != -1 && \
33+
#define MCACHE_CACHEABLE_NAME(name) \
34+
PyUnicode_CheckExact(name) && \
35+
PyUnicode_IS_READY(name) && \
3636
PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE
3737

3838
struct method_cache_entry {

Objects/unicodeobject.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4185,10 +4185,13 @@ PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index)
41854185
void *data;
41864186
int kind;
41874187

4188-
if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) {
4188+
if (!PyUnicode_Check(unicode)) {
41894189
PyErr_BadArgument();
41904190
return (Py_UCS4)-1;
41914191
}
4192+
if (PyUnicode_READY(unicode) == -1) {
4193+
return (Py_UCS4)-1;
4194+
}
41924195
if (index < 0 || index >= PyUnicode_GET_LENGTH(unicode)) {
41934196
PyErr_SetString(PyExc_IndexError, "string index out of range");
41944197
return (Py_UCS4)-1;
@@ -11668,10 +11671,13 @@ unicode_getitem(PyObject *self, Py_ssize_t index)
1166811671
enum PyUnicode_Kind kind;
1166911672
Py_UCS4 ch;
1167011673

11671-
if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) {
11674+
if (!PyUnicode_Check(self)) {
1167211675
PyErr_BadArgument();
1167311676
return NULL;
1167411677
}
11678+
if (PyUnicode_READY(self) == -1) {
11679+
return NULL;
11680+
}
1167511681
if (index < 0 || index >= PyUnicode_GET_LENGTH(self)) {
1167611682
PyErr_SetString(PyExc_IndexError, "string index out of range");
1167711683
return NULL;

Python/ceval.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5017,13 +5017,16 @@ import_all_from(PyObject *locals, PyObject *v)
50175017
PyErr_Clear();
50185018
break;
50195019
}
5020-
if (skip_leading_underscores &&
5021-
PyUnicode_Check(name) &&
5022-
PyUnicode_READY(name) != -1 &&
5023-
PyUnicode_READ_CHAR(name, 0) == '_')
5024-
{
5025-
Py_DECREF(name);
5026-
continue;
5020+
if (skip_leading_underscores && PyUnicode_Check(name)) {
5021+
if (PyUnicode_READY(name) == -1) {
5022+
Py_DECREF(name);
5023+
err = -1;
5024+
break;
5025+
}
5026+
if (PyUnicode_READ_CHAR(name, 0) == '_') {
5027+
Py_DECREF(name);
5028+
continue;
5029+
}
50275030
}
50285031
value = PyObject_GetAttr(v, name);
50295032
if (value == NULL)

0 commit comments

Comments
 (0)