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

Skip to content

Commit e1335c7

Browse files
author
Victor Stinner
committed
Fix usage og PyUnicode_READY()
1 parent e06e145 commit e1335c7

3 files changed

Lines changed: 27 additions & 12 deletions

File tree

Modules/_io/stringio.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ write_str(stringio *self, PyObject *obj)
131131
return -1;
132132

133133
assert(PyUnicode_Check(decoded));
134+
if (PyUnicode_READY(decoded)) {
135+
Py_DECREF(decoded);
136+
return -1;
137+
}
134138
len = PyUnicode_GET_LENGTH(decoded);
135139

136140
assert(len >= 0);

Objects/unicodeobject.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,6 +2120,10 @@ PyUnicode_FromFormatV(const char *format, va_list vargs)
21202120
str_obj = PyUnicode_DecodeUTF8(str, strlen(str), "replace");
21212121
if (!str_obj)
21222122
goto fail;
2123+
if (PyUnicode_READY(str_obj)) {
2124+
Py_DECREF(str_obj);
2125+
goto fail;
2126+
}
21232127
argmaxchar = PyUnicode_MAX_CHAR_VALUE(str_obj);
21242128
maxchar = Py_MAX(maxchar, argmaxchar);
21252129
n += PyUnicode_GET_LENGTH(str_obj);
@@ -10062,17 +10066,17 @@ PyUnicode_Append(PyObject **p_left, PyObject *right)
1006210066
goto error;
1006310067
}
1006410068

10069+
if (PyUnicode_READY(left))
10070+
goto error;
10071+
if (PyUnicode_READY(right))
10072+
goto error;
10073+
1006510074
if (PyUnicode_CheckExact(left) && left != unicode_empty
1006610075
&& PyUnicode_CheckExact(right) && right != unicode_empty
1006710076
&& unicode_resizable(left)
1006810077
&& (_PyUnicode_KIND(right) <= _PyUnicode_KIND(left)
1006910078
|| _PyUnicode_WSTR(left) != NULL))
1007010079
{
10071-
if (PyUnicode_READY(left))
10072-
goto error;
10073-
if (PyUnicode_READY(right))
10074-
goto error;
10075-
1007610080
/* Don't resize for ascii += latin1. Convert ascii to latin1 requires
1007710081
to change the structure size, but characters are stored just after
1007810082
the structure, and so it requires to move all charactres which is

Python/getargs.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -834,14 +834,21 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
834834

835835
case 'C': {/* unicode char */
836836
int *p = va_arg(*p_va, int *);
837-
if (PyUnicode_Check(arg) &&
838-
PyUnicode_GET_LENGTH(arg) == 1) {
839-
int kind = PyUnicode_KIND(arg);
840-
void *data = PyUnicode_DATA(arg);
841-
*p = PyUnicode_READ(kind, data, 0);
842-
}
843-
else
837+
int kind;
838+
void *data;
839+
840+
if (!PyUnicode_Check(arg))
841+
return converterr("a unicode character", arg, msgbuf, bufsize);
842+
843+
if (PyUnicode_READY(arg))
844+
RETURN_ERR_OCCURRED;
845+
846+
if (PyUnicode_GET_LENGTH(arg) != 1)
844847
return converterr("a unicode character", arg, msgbuf, bufsize);
848+
849+
kind = PyUnicode_KIND(arg);
850+
data = PyUnicode_DATA(arg);
851+
*p = PyUnicode_READ(kind, data, 0);
845852
break;
846853
}
847854

0 commit comments

Comments
 (0)