Skip to content

Commit ae7734d

Browse files
committed
pythongh-111089: PyUnicode_AsUTF8AndSize() sets size on error
On error, PyUnicode_AsUTF8AndSize() now sets the size argument to -1, to avoid undefined value.
1 parent 59ea0f5 commit ae7734d

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

Diff for: Doc/c-api/unicode.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -971,8 +971,8 @@ These are the UTF-8 codec APIs:
971971
returned buffer always has an extra null byte appended (not included in
972972
*size*), regardless of whether there are any other null code points.
973973
974-
In the case of an error, ``NULL`` is returned with an exception set and no
975-
*size* is stored.
974+
On error, set an exception, set *size* to ``-1`` (if it's not NULL) and
975+
return ``NULL``.
976976
977977
This caches the UTF-8 representation of the string in the Unicode object, and
978978
subsequent calls will return a pointer to the same buffer. The caller is not

Diff for: Modules/_testcapi/unicode.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ unicode_asutf8andsize(PyObject *self, PyObject *args)
634634
NULLABLE(unicode);
635635
s = PyUnicode_AsUTF8AndSize(unicode, &size);
636636
if (s == NULL) {
637-
assert(size == UNINITIALIZED_SIZE);
637+
assert(size == -1);
638638
return NULL;
639639
}
640640

Diff for: Objects/unicodeobject.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -3820,17 +3820,24 @@ PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
38203820
{
38213821
if (!PyUnicode_Check(unicode)) {
38223822
PyErr_BadArgument();
3823+
if (psize) {
3824+
*psize = -1;
3825+
}
38233826
return NULL;
38243827
}
38253828

38263829
if (PyUnicode_UTF8(unicode) == NULL) {
38273830
if (unicode_fill_utf8(unicode) == -1) {
3831+
if (psize) {
3832+
*psize = -1;
3833+
}
38283834
return NULL;
38293835
}
38303836
}
38313837

3832-
if (psize)
3838+
if (psize) {
38333839
*psize = PyUnicode_UTF8_LENGTH(unicode);
3840+
}
38343841
return PyUnicode_UTF8(unicode);
38353842
}
38363843

0 commit comments

Comments
 (0)