]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Add private _PyUnicode_AsUTF8NoNUL() function (GH-111957)
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 10 Nov 2023 19:31:36 +0000 (21:31 +0200)
committerGitHub <noreply@github.com>
Fri, 10 Nov 2023 19:31:36 +0000 (21:31 +0200)
Like PyUnicode_AsUTF8(), but check for embedded null characters.

Include/internal/pycore_unicodeobject.h
Modules/_io/textio.c
Modules/_sqlite/connection.c
Objects/unicodeobject.c

index 360a9e1819f8e85a036a464d637a89c633d1fa42..23e2670d3a36afa50ff3da44dab7ed6ae5eeabb1 100644 (file)
@@ -434,6 +434,10 @@ struct _Py_unicode_state {
 extern void _PyUnicode_InternInPlace(PyInterpreterState *interp, PyObject **p);
 extern void _PyUnicode_ClearInterned(PyInterpreterState *interp);
 
+// Like PyUnicode_AsUTF8(), but check for embedded null characters.
+// Export for '_sqlite3' shared extension.
+PyAPI_FUNC(const char *) _PyUnicode_AsUTF8NoNUL(PyObject *);
+
 
 #ifdef __cplusplus
 }
index 10ef8a803c50fdee8c204c186b36003335f4fda0..e6a971e2250d635baf0949cf74fe18f78518836f 100644 (file)
@@ -1020,15 +1020,10 @@ io_check_errors(PyObject *errors)
         return 0;
     }
 
-    Py_ssize_t name_length;
-    const char *name = PyUnicode_AsUTF8AndSize(errors, &name_length);
+    const char *name = _PyUnicode_AsUTF8NoNUL(errors);
     if (name == NULL) {
         return -1;
     }
-    if (strlen(name) != (size_t)name_length) {
-        PyErr_SetString(PyExc_ValueError, "embedded null character in errors");
-        return -1;
-    }
     PyObject *handler = PyCodec_LookupError(name);
     if (handler != NULL) {
         Py_DECREF(handler);
index 319ed0c29c7a9b617c7d91ee4efc988b635df13e..0a6633972cc5ef777c9ff1acd85c0e11e4a3f2b1 100644 (file)
@@ -76,16 +76,10 @@ isolation_level_converter(PyObject *str_or_none, const char **result)
         *result = NULL;
     }
     else if (PyUnicode_Check(str_or_none)) {
-        Py_ssize_t sz;
-        const char *str = PyUnicode_AsUTF8AndSize(str_or_none, &sz);
+        const char *str = _PyUnicode_AsUTF8NoNUL(str_or_none);
         if (str == NULL) {
             return 0;
         }
-        if (strlen(str) != (size_t)sz) {
-            PyErr_SetString(PyExc_ValueError, "embedded null character");
-            return 0;
-        }
-
         const char *level = get_isolation_level(str);
         if (level == NULL) {
             return 0;
index 53e1e56babf952bf5ed9c13338a83c35f9a00bfc..f3f1305c5caf9208cdcde14c5e4d2a20acf0c1fb 100644 (file)
@@ -3847,6 +3847,18 @@ PyUnicode_AsUTF8(PyObject *unicode)
     return PyUnicode_AsUTF8AndSize(unicode, NULL);
 }
 
+const char *
+_PyUnicode_AsUTF8NoNUL(PyObject *unicode)
+{
+    Py_ssize_t size;
+    const char *s = PyUnicode_AsUTF8AndSize(unicode, &size);
+    if (s && strlen(s) != (size_t)size) {
+        PyErr_SetString(PyExc_ValueError, "embedded null character");
+        return NULL;
+    }
+    return s;
+}
+
 /*
 PyUnicode_GetSize() has been deprecated since Python 3.3
 because it returned length of Py_UNICODE.