diff options
author | Victor Stinner <vstinner@python.org> | 2023-11-15 13:55:46 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-15 13:55:46 (GMT) |
commit | 1445d77282f9082f3fecde772ddf6bb2f5a383f1 (patch) | |
tree | 0c94d2b0777b2f9c67c6debc5c3cdc3b63a58fdc /Modules/_io | |
parent | 91a33fde4826967aa929e53a234aff8a419c5166 (diff) | |
download | cpython-1445d77282f9082f3fecde772ddf6bb2f5a383f1.zip cpython-1445d77282f9082f3fecde772ddf6bb2f5a383f1.tar.gz cpython-1445d77282f9082f3fecde772ddf6bb2f5a383f1.tar.bz2 |
[3.12] gh-111942: Fix SystemError in the TextIOWrapper constructor (GH-112061) (GH-112089)
In non-debug more the check for the "errors" argument is skipped,
and then PyUnicode_AsUTF8() can fail, but its result was not checked.
Co-authored-by: Victor Stinner <vstinner@python.org>
(cherry picked from commit 9302f05f9af07332c414b3c19003efd1b1763cf3)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Modules/_io')
-rw-r--r-- | Modules/_io/textio.c | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index d4797ca..efada7f 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -1119,6 +1119,15 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer, else if (io_check_errors(errors)) { return -1; } + Py_ssize_t errors_len; + const char *errors_str = PyUnicode_AsUTF8AndSize(errors, &errors_len); + if (errors_str == NULL) { + return -1; + } + if (strlen(errors_str) != (size_t)errors_len) { + PyErr_SetString(PyExc_ValueError, "embedded null character"); + return -1; + } if (validate_newline(newline) < 0) { return -1; @@ -1191,11 +1200,11 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer, /* Build the decoder object */ _PyIO_State *state = find_io_state_by_def(Py_TYPE(self)); self->state = state; - if (_textiowrapper_set_decoder(self, codec_info, PyUnicode_AsUTF8(errors)) != 0) + if (_textiowrapper_set_decoder(self, codec_info, errors_str) != 0) goto error; /* Build the encoder object */ - if (_textiowrapper_set_encoder(self, codec_info, PyUnicode_AsUTF8(errors)) != 0) + if (_textiowrapper_set_encoder(self, codec_info, errors_str) != 0) goto error; /* Finished sorting out the codec details */ |