8000 Add private _PyUnicode_AsUTF8NoNUL() function (GH-111957) · python/cpython@771bd3c · GitHub
[go: up one dir, main page]

Skip to content

Commit 771bd3c

Browse files
Add private _PyUnicode_AsUTF8NoNUL() function (GH-111957)
Like PyUnicode_AsUTF8(), but check for embedded null characters.
1 parent 3932b0f commit 771bd3c

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

Include/internal/pycore_unicodeobject.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,10 @@ struct _Py_unicode_state {
434434
extern void _PyUnicode_InternInPlace(PyInterpreterState *interp, PyObject **p);
435435
extern void _PyUnicode_ClearInterned(PyInterpreterState *interp);
436436

437+
// Like PyUnicode_AsUTF8(), but check for embedded null characters.
438+
// Export for '_sqlite3' shared extension.
439+
PyAPI_FUNC(const char *) _PyUnicode_AsUTF8NoNUL(PyObject *);
440+
437441

438442
#ifdef __cplusplus
439443
}

Modules/_io/textio.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,15 +1020,10 @@ io_check_errors(PyObject *errors)
10201020
return 0;
10211021
}
10221022

1023-
Py_ssize_t name_length;
1024-
const char *name = PyUnicode_AsUTF8AndSize(errors, &name_length);
1023+
const char *name = _PyUnicode_AsUTF8NoNUL(errors);
10251024
if (name == NULL) {
10261025
return -1;
10271026
}
1028-
if (strlen(name) != (size_t)name_length) {
1029-
PyErr_SetString(PyExc_ValueError, "embedded null character in errors");
1030-
return -1;
1031-
}
10321027
PyObject *handler = PyCodec_LookupError(name);
10331028
if (handler != NULL) {
10341029
Py_DECREF(handler);

Modules/_sqlite/connection.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,10 @@ isolation_level_converter(PyObject *str_or_none, const char **result)
7676
*result = NULL;
7777
}
7878
else if (PyUnicode_Check(str_or_none)) {
79-
Py_ssize_t sz;
80-
const char *str = PyUnicode_AsUTF8AndSize(str_or_none, &sz);
79+
const char *str = _PyUnicode_AsUTF8NoNUL(str_or_none);
8180
if (str == NULL) {
8281
return 0;
8382
}
84-
if (strlen(str) != (size_t)sz) {
85-
PyErr_SetString(PyExc_ValueError, "embedded null character");
86-
return 0;
87-
}
88-
8983
const char *level = get_isolation_level(str);
9084
if (level == NULL) {
9185
return 0;

Objects/unicodeobject.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3847,6 +3847,18 @@ PyUnicode_AsUTF8(PyObject *unicode)
38473847
return PyUnicode_AsUTF8AndSize(unicode, NULL);
38483848
}
38493849

3850+
const char *
3851+
_PyUnicode_AsUTF8NoNUL(PyObject *unicode)
3852+
{
3853+
Py_ssize_t size;
3854+
const char *s = PyUnicode_AsUTF8AndSize(unicode, &size);
3855+
if (s && strlen(s) != (size_t)size) {
3856+
PyErr_SetString(PyExc_ValueError, "embedded null character");
3857+
return NULL;
3858+
}
3859+
return s;
3860+
}
3861+
38503862
/*
38513863
PyUnicode_GetSize() has been deprecated since Python 3.3
38523864
because it returned length of Py_UNICODE.

0 commit comments

Comments
 (0)
0