]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-111089: PyUnicode_AsUTF8AndSize() sets size on error (#111106)
authorVictor Stinner <vstinner@python.org>
Fri, 20 Oct 2023 18:03:11 +0000 (20:03 +0200)
committerGitHub <noreply@github.com>
Fri, 20 Oct 2023 18:03:11 +0000 (20:03 +0200)
On error, PyUnicode_AsUTF8AndSize() now sets the size argument to -1,
to avoid undefined value.

Doc/c-api/unicode.rst
Modules/_testcapi/unicode.c
Objects/unicodeobject.c

index d17e63dc089b98906f9fba8f9c5e80ce3c259d96..5fa37963e07eff7c7c138e31f3bcab6ea422bf56 100644 (file)
@@ -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
index d52d88a65d86fcc93285fd05d95a5298d80f95d3..a10183dddeca98cf76fd24535091ed4472e8b059 100644 (file)
@@ -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;
     }
 
index 07d1b6e726b159ac3b2d9483de8fc0576351b1fe..80b19567c63d2086c9271521791edc45b95db0ab 100644 (file)
@@ -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);
 }