Skip to content

gh-111089: PyUnicode_AsUTF8AndSize() sets size on error #111106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Doc/c-api/unicode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -971,8 +971,8 @@ These are the UTF-8 codec APIs:
returned buffer always has an extra null byte appended (not included in
*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
*size* is stored.
On error, set an exception, set *size* to ``-1`` (if it's not NULL) and
return ``NULL``.

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
Expand Down
2 changes: 1 addition & 1 deletion Modules/_testcapi/unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ unicode_asutf8andsize(PyObject *self, PyObject *args)
NULLABLE(unicode);
s = PyUnicode_AsUTF8AndSize(unicode, &size);
if (s == NULL) {
assert(size == UNINITIALIZED_SIZE);
assert(size == -1);
return NULL;
}

Expand Down
9 changes: 8 additions & 1 deletion Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3820,17 +3820,24 @@ PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
{
if (!PyUnicode_Check(unicode)) {
PyErr_BadArgument();
if (psize) {
*psize = -1;
}
return NULL;
}

if (PyUnicode_UTF8(unicode) == NULL) {
if (unicode_fill_utf8(unicode) == -1) {
if (psize) {
*psize = -1;
}
return NULL;
}
}

if (psize)
if (psize) {
*psize = PyUnicode_UTF8_LENGTH(unicode);
}
return PyUnicode_UTF8(unicode);
}

Expand Down