From b137cbae1d7281999c356413abbbfe6117f7bdd3 Mon Sep 17 00:00:00 2001 From: Xiang Zhang Date: Mon, 29 Oct 2018 23:12:42 +0800 Subject: [PATCH] bpo-35062: Fix parsing _io.IncrementalNewlineDecoder's *translate* argument. _io.IncrementalNewlineDecoder's initializer possibly assigns out-of-range value to the bitwise struct field. --- Lib/test/test_io.py | 10 ++++++++++ .../Library/2018-10-29-23-09-24.bpo-35062.dQS1ng.rst | 2 ++ Modules/_io/textio.c | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2018-10-29-23-09-24.bpo-35062.dQS1ng.rst diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index abd55387bdbf87..d927bb96ceb5b1 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -3748,6 +3748,16 @@ def _check(dec): dec = self.IncrementalNewlineDecoder(None, translate=True) _check(dec) + def test_translate(self): + # issue 35062 + for translate in (-2, -1, 1, 2): + decoder = codecs.getincrementaldecoder("utf-8")() + decoder = self.IncrementalNewlineDecoder(decoder, translate) + self.check_newline_decoding_utf8(decoder) + decoder = codecs.getincrementaldecoder("utf-8")() + decoder = self.IncrementalNewlineDecoder(decoder, translate=0) + self.assertEqual(decoder.decode(b"\r\r\n"), "\r\r\n") + class CIncrementalNewlineDecoderTest(IncrementalNewlineDecoderTest): pass diff --git a/Misc/NEWS.d/next/Library/2018-10-29-23-09-24.bpo-35062.dQS1ng.rst b/Misc/NEWS.d/next/Library/2018-10-29-23-09-24.bpo-35062.dQS1ng.rst new file mode 100644 index 00000000000000..b77ed8685bfcbd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-10-29-23-09-24.bpo-35062.dQS1ng.rst @@ -0,0 +1,2 @@ +Fix incorrect parsing of :class:`_io.IncrementalNewlineDecoder`'s +*translate* argument. diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index a466d3a03a5ba3..0b41c377343e28 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -261,7 +261,7 @@ _io_IncrementalNewlineDecoder___init___impl(nldecoder_object *self, } Py_INCREF(self->errors); - self->translate = translate; + self->translate = translate ? 1 : 0; self->seennl = 0; self->pendingcr = 0;