diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2023-11-10 19:31:36 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-10 19:31:36 (GMT) |
commit | 771bd3c94a366547e0c3451a72a430e1132c1ac1 (patch) | |
tree | 92d9e843024137d115ff9e1fc22399f4707700ef /Objects | |
parent | 3932b0f7b1566374427daa8bc47203032015e350 (diff) | |
download | cpython-771bd3c94a366547e0c3451a72a430e1132c1ac1.zip cpython-771bd3c94a366547e0c3451a72a430e1132c1ac1.tar.gz cpython-771bd3c94a366547e0c3451a72a430e1132c1ac1.tar.bz2 |
Add private _PyUnicode_AsUTF8NoNUL() function (GH-111957)
Like PyUnicode_AsUTF8(), but check for embedded null characters.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/unicodeobject.c | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 53e1e56..f3f1305 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -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. |