mirror of
https://github.com/python/cpython.git
synced 2025-08-08 10:58:51 +00:00
[3.9] gh-133767: Fix use-after-free in the unicode-escape decoder with an error handler (GH-129648) (GH-133944) (#134346)
* [3.9] gh-133767: Fix use-after-free in the unicode-escape decoder with an error handler (GH-129648) (GH-133944) If the error handler is used, a new bytes object is created to set as the object attribute of UnicodeDecodeError, and that bytes object then replaces the original data. A pointer to the decoded data will became invalid after destroying that temporary bytes object. So we need other way to return the first invalid escape from _PyUnicode_DecodeUnicodeEscapeInternal(). _PyBytes_DecodeEscape() does not have such issue, because it does not use the error handlers registry, but it should be changed for compatibility with _PyUnicode_DecodeUnicodeEscapeInternal(). (cherry picked from commit9f69a58623
) (cherry picked from commit6279eb8c07
) (cherry picked from commita75953b347
) (cherry picked from commit0c33e5baed
) (cherry picked from commit8b528cacbb
) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
d4df3c55e4
commit
8d35fd1b34
8 changed files with 163 additions and 40 deletions
|
@ -1178,20 +1178,32 @@ class EscapeDecodeTest(unittest.TestCase):
|
|||
check(br"[\501]", b"[A]")
|
||||
check(br"[\x41]", b"[A]")
|
||||
check(br"[\x410]", b"[A0]")
|
||||
|
||||
def test_warnings(self):
|
||||
decode = codecs.escape_decode
|
||||
check = coding_checker(self, decode)
|
||||
for i in range(97, 123):
|
||||
b = bytes([i])
|
||||
if b not in b'abfnrtvx':
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
with self.assertWarnsRegex(DeprecationWarning,
|
||||
r"invalid escape sequence '\\%c'" % i):
|
||||
check(b"\\" + b, b"\\" + b)
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
with self.assertWarnsRegex(DeprecationWarning,
|
||||
r"invalid escape sequence '\\%c'" % (i-32)):
|
||||
check(b"\\" + b.upper(), b"\\" + b.upper())
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
with self.assertWarnsRegex(DeprecationWarning,
|
||||
r"invalid escape sequence '\\8'"):
|
||||
check(br"\8", b"\\8")
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
check(br"\9", b"\\9")
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
with self.assertWarnsRegex(DeprecationWarning,
|
||||
r"invalid escape sequence '\\\xfa'") as cm:
|
||||
check(b"\\\xfa", b"\\\xfa")
|
||||
|
||||
with self.assertWarnsRegex(DeprecationWarning,
|
||||
r"invalid escape sequence '\\z'"):
|
||||
self.assertEqual(decode(br'\x\z', 'ignore'), (b'\\z', 4))
|
||||
|
||||
def test_errors(self):
|
||||
decode = codecs.escape_decode
|
||||
self.assertRaises(ValueError, decode, br"\x")
|
||||
|
@ -2393,20 +2405,31 @@ class UnicodeEscapeTest(ReadTest, unittest.TestCase):
|
|||
check(br"[\x410]", "[A0]")
|
||||
check(br"\u20ac", "\u20ac")
|
||||
check(br"\U0001d120", "\U0001d120")
|
||||
|
||||
def test_decode_warnings(self):
|
||||
decode = codecs.unicode_escape_decode
|
||||
check = coding_checker(self, decode)
|
||||
for i in range(97, 123):
|
||||
b = bytes([i])
|
||||
if b not in b'abfnrtuvx':
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
with self.assertWarnsRegex(DeprecationWarning,
|
||||
r"invalid escape sequence '\\%c'" % i):
|
||||
check(b"\\" + b, "\\" + chr(i))
|
||||
if b.upper() not in b'UN':
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
with self.assertWarnsRegex(DeprecationWarning,
|
||||
r"invalid escape sequence '\\%c'" % (i-32)):
|
||||
check(b"\\" + b.upper(), "\\" + chr(i-32))
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
with self.assertWarnsRegex(DeprecationWarning,
|
||||
r"invalid escape sequence '\\8'"):
|
||||
check(br"\8", "\\8")
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
check(br"\9", "\\9")
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
with self.assertWarnsRegex(DeprecationWarning,
|
||||
r"invalid escape sequence '\\\xfa'") as cm:
|
||||
check(b"\\\xfa", "\\\xfa")
|
||||
with self.assertWarnsRegex(DeprecationWarning,
|
||||
r"invalid escape sequence '\\z'"):
|
||||
self.assertEqual(decode(br'\x\z', 'ignore'), ('\\z', 4))
|
||||
|
||||
def test_decode_errors(self):
|
||||
decode = codecs.unicode_escape_decode
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue