diff options
author | Zackery Spytz <zspytz@gmail.com> | 2022-11-28 10:46:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-28 10:46:40 (GMT) |
commit | 53eef27133c1da395b3b4d7ce0ab1d5b743ffb41 (patch) | |
tree | 4549e5244c14603b79f0da28db8e1d8220796dfb /Modules | |
parent | 492dc02b01828f346dd62412fefc654e781de923 (diff) | |
download | cpython-53eef27133c1da395b3b4d7ce0ab1d5b743ffb41.zip cpython-53eef27133c1da395b3b4d7ce0ab1d5b743ffb41.tar.gz cpython-53eef27133c1da395b3b4d7ce0ab1d5b743ffb41.tar.bz2 |
bpo-31718: Fix io.IncrementalNewlineDecoder SystemErrors and segfaults (#18640)
Co-authored-by: Oren Milman <orenmn@gmail.com>
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_io/textio.c | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index ff903e9..3091f6e 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -231,15 +231,16 @@ _io_IncrementalNewlineDecoder___init___impl(nldecoder_object *self, PyObject *errors) /*[clinic end generated code: output=fbd04d443e764ec2 input=89db6b19c6b126bf]*/ { - self->decoder = Py_NewRef(decoder); if (errors == NULL) { - self->errors = Py_NewRef(&_Py_ID(strict)); + errors = Py_NewRef(&_Py_ID(strict)); } else { - self->errors = Py_NewRef(errors); + errors = Py_NewRef(errors); } + Py_XSETREF(self->errors, errors); + Py_XSETREF(self->decoder, Py_NewRef(decoder)); self->translate = translate ? 1 : 0; self->seennl = 0; self->pendingcr = 0; @@ -274,6 +275,13 @@ check_decoded(PyObject *decoded) return 0; } +#define CHECK_INITIALIZED_DECODER(self) \ + if (self->errors == NULL) { \ + PyErr_SetString(PyExc_ValueError, \ + "IncrementalNewlineDecoder.__init__() not called"); \ + return NULL; \ + } + #define SEEN_CR 1 #define SEEN_LF 2 #define SEEN_CRLF 4 @@ -287,11 +295,7 @@ _PyIncrementalNewlineDecoder_decode(PyObject *myself, Py_ssize_t output_len; nldecoder_object *self = (nldecoder_object *) myself; - if (self->decoder == NULL) { - PyErr_SetString(PyExc_ValueError, - "IncrementalNewlineDecoder.__init__ not called"); - return NULL; - } + CHECK_INITIALIZED_DECODER(self); /* decode input (with the eventual \r from a previous pass) */ if (self->decoder != Py_None) { @@ -502,6 +506,8 @@ _io_IncrementalNewlineDecoder_getstate_impl(nldecoder_object *self) PyObject *buffer; unsigned long long flag; + CHECK_INITIALIZED_DECODER(self); + if (self->decoder != Py_None) { PyObject *state = PyObject_CallMethodNoArgs(self->decoder, &_Py_ID(getstate)); @@ -546,6 +552,8 @@ _io_IncrementalNewlineDecoder_setstate(nldecoder_object *self, PyObject *buffer; unsigned long long flag; + CHECK_INITIALIZED_DECODER(self); + if (!PyTuple_Check(state)) { PyErr_SetString(PyExc_TypeError, "state argument must be a tuple"); return NULL; @@ -576,6 +584,8 @@ static PyObject * _io_IncrementalNewlineDecoder_reset_impl(nldecoder_object *self) /*[clinic end generated code: output=32fa40c7462aa8ff input=728678ddaea776df]*/ { + CHECK_INITIALIZED_DECODER(self); + self->seennl = 0; self->pendingcr = 0; if (self->decoder != Py_None) @@ -587,6 +597,8 @@ _io_IncrementalNewlineDecoder_reset_impl(nldecoder_object *self) static PyObject * incrementalnewlinedecoder_newlines_get(nldecoder_object *self, void *context) { + CHECK_INITIALIZED_DECODER(self); + switch (self->seennl) { case SEEN_CR: return PyUnicode_FromString("\r"); |