]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-35062: Fix parsing _io.IncrementalNewlineDecoder's *translate* argument. (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 31 Oct 2018 12:36:20 +0000 (05:36 -0700)
committerGitHub <noreply@github.com>
Wed, 31 Oct 2018 12:36:20 +0000 (05:36 -0700)
_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 <angwerzx@126.com>
Lib/test/test_io.py
Misc/NEWS.d/next/Library/2018-10-29-23-09-24.bpo-35062.dQS1ng.rst [new file with mode: 0644]
Modules/_io/textio.c

index c68b2fea858b718c928b983f59813f86184b0c04..b41141e28adbad31ef0a1b118546f0c93d063966 100644 (file)
@@ -3748,6 +3748,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 (file)
index 0000000..b77ed86
--- /dev/null
@@ -0,0 +1,2 @@
+Fix incorrect parsing of :class:`_io.IncrementalNewlineDecoder`'s
+*translate* argument.
index d6b1e94378832541db9741b6b31c78294196b4a0..73d1b2e8a23baf0cdc6bfc1d4d14b92a079a962b 100644 (file)
@@ -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;