From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Tue, 25 Feb 2020 03:43:46 +0000 (-0800) Subject: bpo-30566: Fix IndexError when using punycode codec (GH-18632) X-Git-Tag: v3.7.7rc1~19 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=55be9a6c09d4415f50b14212ce22eccefa83ca64;p=thirdparty%2FPython%2Fcpython.git bpo-30566: Fix IndexError when using punycode codec (GH-18632) Trying to decode an invalid string with the punycode codec shoud raise UnicodeError. (cherry picked from commit ba22e8f174309979d90047c5dc64fcb63bc2c32e) Co-authored-by: Berker Peksag --- diff --git a/Lib/encodings/punycode.py b/Lib/encodings/punycode.py index 66c51013ea43..1c5726447077 100644 --- a/Lib/encodings/punycode.py +++ b/Lib/encodings/punycode.py @@ -143,7 +143,7 @@ def decode_generalized_number(extended, extpos, bias, errors): digit = char - 22 # 0x30-26 elif errors == "strict": raise UnicodeError("Invalid extended code point '%s'" - % extended[extpos]) + % extended[extpos-1]) else: return extpos, None t = T(j, bias) diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index cd2761353490..5a450ebd9dc2 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -1406,6 +1406,18 @@ class PunycodeTest(unittest.TestCase): puny = puny.decode("ascii").encode("ascii") self.assertEqual(uni, puny.decode("punycode")) + def test_decode_invalid(self): + testcases = [ + (b"xn--w&", "strict", UnicodeError()), + (b"xn--w&", "ignore", "xn-"), + ] + for puny, errors, expected in testcases: + with self.subTest(puny=puny, errors=errors): + if isinstance(expected, Exception): + self.assertRaises(UnicodeError, puny.decode, "punycode", errors) + else: + self.assertEqual(puny.decode("punycode", errors), expected) + class UnicodeInternalTest(unittest.TestCase): @unittest.skipUnless(SIZEOF_WCHAR_T == 4, 'specific to 32-bit wchar_t') diff --git a/Misc/NEWS.d/next/Library/2020-02-24-03-45-28.bpo-30566.qROxty.rst b/Misc/NEWS.d/next/Library/2020-02-24-03-45-28.bpo-30566.qROxty.rst new file mode 100644 index 000000000000..c78063303009 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-02-24-03-45-28.bpo-30566.qROxty.rst @@ -0,0 +1,2 @@ +Fix :exc:`IndexError` when trying to decode an invalid string with punycode +codec.