]> granicus.if.org Git - python/commitdiff
bpo-35062: Fix parsing _io.IncrementalNewlineDecoder's *translate* argument. (GH...
authorXiang Zhang <angwerzx@126.com>
Wed, 31 Oct 2018 11:49:16 +0000 (19:49 +0800)
committerGitHub <noreply@github.com>
Wed, 31 Oct 2018 11:49:16 +0000 (19:49 +0800)
_io.IncrementalNewlineDecoder's initializer possibly assigns out-of-range
value to the bitwise struct field.

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 abd55387bdbf87ca7f3f33c59497851b5d313252..d927bb96ceb5b10a35452e71a358bbcb28713cdd 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 3a3667b39da86c75447b5db14a60412d7eca909d..be427772817a9e2106094b7063642675bd93ad0c 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;