mirror of https://github.com/python/cpython
gh-111089: PyUnicode_AsUTF8AndSize() sets size on error (#111106)
On error, PyUnicode_AsUTF8AndSize() now sets the size argument to -1, to avoid undefined value.
This commit is contained in:
parent
d8f32be5b6
commit
f1e751e933
|
@ -971,8 +971,8 @@ These are the UTF-8 codec APIs:
|
||||||
returned buffer always has an extra null byte appended (not included in
|
returned buffer always has an extra null byte appended (not included in
|
||||||
*size*), regardless of whether there are any other null code points.
|
*size*), regardless of whether there are any other null code points.
|
||||||
|
|
||||||
In the case of an error, ``NULL`` is returned with an exception set and no
|
On error, set an exception, set *size* to ``-1`` (if it's not NULL) and
|
||||||
*size* is stored.
|
return ``NULL``.
|
||||||
|
|
||||||
This caches the UTF-8 representation of the string in the Unicode object, and
|
This caches the UTF-8 representation of the string in the Unicode object, and
|
||||||
subsequent calls will return a pointer to the same buffer. The caller is not
|
subsequent calls will return a pointer to the same buffer. The caller is not
|
||||||
|
|
|
@ -634,7 +634,7 @@ unicode_asutf8andsize(PyObject *self, PyObject *args)
|
||||||
NULLABLE(unicode);
|
NULLABLE(unicode);
|
||||||
s = PyUnicode_AsUTF8AndSize(unicode, &size);
|
s = PyUnicode_AsUTF8AndSize(unicode, &size);
|
||||||
if (s == NULL) {
|
if (s == NULL) {
|
||||||
assert(size == UNINITIALIZED_SIZE);
|
assert(size == -1);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3820,17 +3820,24 @@ PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
|
||||||
{
|
{
|
||||||
if (!PyUnicode_Check(unicode)) {
|
if (!PyUnicode_Check(unicode)) {
|
||||||
PyErr_BadArgument();
|
PyErr_BadArgument();
|
||||||
|
if (psize) {
|
||||||
|
*psize = -1;
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PyUnicode_UTF8(unicode) == NULL) {
|
if (PyUnicode_UTF8(unicode) == NULL) {
|
||||||
if (unicode_fill_utf8(unicode) == -1) {
|
if (unicode_fill_utf8(unicode) == -1) {
|
||||||
|
if (psize) {
|
||||||
|
*psize = -1;
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (psize)
|
if (psize) {
|
||||||
*psize = PyUnicode_UTF8_LENGTH(unicode);
|
*psize = PyUnicode_UTF8_LENGTH(unicode);
|
||||||
|
}
|
||||||
return PyUnicode_UTF8(unicode);
|
return PyUnicode_UTF8(unicode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue