Skip to content

Commit 35804e4

Browse files
Issue #19279: UTF-7 decoder no more produces illegal strings.
1 parent 507c591 commit 35804e4

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

Lib/test/test_codecs.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,36 @@ def test_partial(self):
820820
]
821821
)
822822

823+
def test_errors(self):
824+
tests = [
825+
(b'a\xffb', 'a\ufffdb'),
826+
(b'a+IK', 'a\ufffd'),
827+
(b'a+IK-b', 'a\ufffdb'),
828+
(b'a+IK,b', 'a\ufffdb'),
829+
(b'a+IKx', 'a\u20ac\ufffd'),
830+
(b'a+IKx-b', 'a\u20ac\ufffdb'),
831+
(b'a+IKwgr', 'a\u20ac\ufffd'),
832+
(b'a+IKwgr-b', 'a\u20ac\ufffdb'),
833+
(b'a+IKwgr,', 'a\u20ac\ufffd'),
834+
(b'a+IKwgr,-b', 'a\u20ac\ufffd-b'),
835+
(b'a+IKwgrB', 'a\u20ac\u20ac\ufffd'),
836+
(b'a+IKwgrB-b', 'a\u20ac\u20ac\ufffdb'),
837+
(b'a+/,+IKw-b', 'a\ufffd\u20acb'),
838+
(b'a+//,+IKw-b', 'a\ufffd\u20acb'),
839+
(b'a+///,+IKw-b', 'a\uffff\ufffd\u20acb'),
840+
(b'a+////,+IKw-b', 'a\uffff\ufffd\u20acb'),
841+
]
842+
for raw, expected in tests:
843+
with self.subTest(raw=raw):
844+
self.assertRaises(UnicodeDecodeError, codecs.utf_7_decode,
845+
raw, 'strict', True)
846+
self.assertEqual(raw.decode('utf-7', 'replace'), expected)
847+
848+
def test_nonbmp(self):
849+
self.assertEqual('\U000104A0'.encode(self.encoding), b'+2AHcoA-')
850+
self.assertEqual('\ud801\udca0'.encode(self.encoding), b'+2AHcoA-')
851+
self.assertEqual(b'+2AHcoA-'.decode(self.encoding), '\U000104A0')
852+
823853
class UTF16ExTest(unittest.TestCase):
824854

825855
def test_errors(self):

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ What's New in Python 3.3.3 release candidate 1?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #19279: UTF-7 decoder no more produces illegal strings.
16+
1517
- Fix macro expansion of _PyErr_OCCURRED(), and make sure to use it in at
1618
least one place so as to avoid regressions.
1719

Objects/unicodeobject.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4359,6 +4359,7 @@ PyUnicode_DecodeUTF7Stateful(const char *s,
43594359
Py_UCS4 outCh = (Py_UCS4)(base64buffer >> (base64bits-16));
43604360
base64bits -= 16;
43614361
base64buffer &= (1 << base64bits) - 1; /* clear high bits */
4362+
assert(outCh <= 0xffff);
43624363
if (surrogate) {
43634364
/* expecting a second surrogate */
43644365
if (Py_UNICODE_IS_LOW_SURROGATE(outCh)) {
@@ -4426,6 +4427,7 @@ PyUnicode_DecodeUTF7Stateful(const char *s,
44264427
inShift = 1;
44274428
shiftOutStart = outpos;
44284429
base64bits = 0;
4430+
base64buffer = 0;
44294431
}
44304432
}
44314433
else if (DECODE_DIRECT(ch)) { /* character decodes as itself */

0 commit comments

Comments
 (0)