From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Wed, 31 Oct 2018 12:37:06 +0000 (-0700) Subject: bpo-35062: Fix parsing _io.IncrementalNewlineDecoder's *translate* argument. (GH... X-Git-Tag: v2.7.16rc1~95 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=05acd44ad6b61adb24571eb0203de7b25c7e869b;p=thirdparty%2FPython%2Fcpython.git bpo-35062: Fix parsing _io.IncrementalNewlineDecoder's *translate* argument. (GH-10217) _io.IncrementalNewlineDecoder's initializer possibly assigns out-of-range value to the bitwise struct field. (cherry picked from commit b08746bfdf64e55ce33516f2065fa2aa4f51be95) Co-authored-by: Xiang Zhang --- diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 26c5dfe9261b..8152e6174d35 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -2922,6 +2922,16 @@ class IncrementalNewlineDecoderTest(unittest.TestCase): 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 000000000000..b77ed8685bfc --- /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 1979539cc054..825e84937c15 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -221,7 +221,7 @@ incrementalnewlinedecoder_init(nldecoder_object *self, self->errors = errors; } - self->translate = translate; + self->translate = translate ? 1 : 0; self->seennl = 0; self->pendingcr = 0;