bpo-30566: Fix IndexError when using punycode codec (GH-18632)
Trying to decode an invalid string with the punycode codec shoud raise UnicodeError.
This commit is contained in:
parent
8af4712a16
commit
ba22e8f174
|
@ -143,7 +143,7 @@ def decode_generalized_number(extended, extpos, bias, errors):
|
||||||
digit = char - 22 # 0x30-26
|
digit = char - 22 # 0x30-26
|
||||||
elif errors == "strict":
|
elif errors == "strict":
|
||||||
raise UnicodeError("Invalid extended code point '%s'"
|
raise UnicodeError("Invalid extended code point '%s'"
|
||||||
% extended[extpos])
|
% extended[extpos-1])
|
||||||
else:
|
else:
|
||||||
return extpos, None
|
return extpos, None
|
||||||
t = T(j, bias)
|
t = T(j, bias)
|
||||||
|
|
|
@ -1343,6 +1343,18 @@ class PunycodeTest(unittest.TestCase):
|
||||||
puny = puny.decode("ascii").encode("ascii")
|
puny = puny.decode("ascii").encode("ascii")
|
||||||
self.assertEqual(uni, puny.decode("punycode"))
|
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)
|
||||||
|
|
||||||
|
|
||||||
# From http://www.gnu.org/software/libidn/draft-josefsson-idn-test-vectors.html
|
# From http://www.gnu.org/software/libidn/draft-josefsson-idn-test-vectors.html
|
||||||
nameprep_tests = [
|
nameprep_tests = [
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix :exc:`IndexError` when trying to decode an invalid string with punycode
|
||||||
|
codec.
|
Loading…
Reference in New Issue