Skip to content

bpo-22602: Raise an exception in the UTF-7 decoder for ill-formed sequences starting with "+" #8741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,7 @@ def test_errors(self):
(b'a+////,+IKw-b', 'a\uffff\ufffd\u20acb'),
(b'a+IKw-b\xff', 'a\u20acb\ufffd'),
(b'a+IKw\xffb', 'a\u20ac\ufffdb'),
(b'a+@b', 'a\ufffdb'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test will fail. Is that you want?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The self.assertRaises() on line 1027 checks that a UnicodeDecodeError is raised with the "strict" error handler. Or did you mean something else?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I put the comment in the wrong line. I think that the test will fail on line 1029 self.assertEqual(raw.decode('utf-7', 'replace'), expected)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, you're mistaken.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add also tests for b'a+' and b'a+@'. It may be need to change the code for proper handling these cases.

]
for raw, expected in tests:
with self.subTest(raw=raw):
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,10 @@ def test_codecs_utf7(self):
for c in set_o:
self.assertEqual(c.encode('ascii').decode('utf7'), c)

with self.assertRaisesRegex(UnicodeDecodeError,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add also a test for the non-strict error handler, e.g. "replace". UTF7Test.test_errors in test_codecs.py may be more appropriate place for this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment. I've added the test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test in test_unicode.py is no longer needed, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test in test_codecs.py doesn't check the error message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then all correct.

'ill-formed sequence'):
b'+@'.decode('utf-7')

def test_codecs_utf8(self):
self.assertEqual(''.encode('utf-8'), b'')
self.assertEqual('\u20ac'.encode('utf-8'), b'\xe2\x82\xac')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The UTF-7 decoder now raises :exc:`UnicodeDecodeError` for ill-formed
sequences starting with "+" (as specified in RFC 2152). Patch by Zackery
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use :rfc:`2152`.

Spytz.
5 changes: 5 additions & 0 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4479,6 +4479,11 @@ PyUnicode_DecodeUTF7Stateful(const char *s,
if (_PyUnicodeWriter_WriteCharInline(&writer, '+') < 0)
goto onError;
}
else if (s < e && !IS_BASE64(*s)) {
s++;
errmsg = "ill-formed sequence";
goto utf7Error;
}
else { /* begin base64-encoded section */
inShift = 1;
surrogate = 0;
Expand Down