There was an error while loading. Please reload this page.
1 parent 6de708f commit d73aca7Copy full SHA for d73aca7
3 files changed
Lib/test/test_compile.py
@@ -1,9 +1,11 @@
1
import math
2
+import os
3
import unittest
4
import sys
5
import _ast
6
+import tempfile
7
import types
-from test import support
8
+from test import support, script_helper
9
10
class TestSpecifics(unittest.TestCase):
11
@@ -492,6 +494,16 @@ def test_bad_single_statement(self):
492
494
self.assertInvalidSingle('f()\nxy # blah\nblah()')
493
495
self.assertInvalidSingle('x = 5 # comment\nx = 6\n')
496
497
+ def test_particularly_evil_undecodable(self):
498
+ # Issue 24022
499
+ src = b'0000\x00\n00000000000\n\x00\n\x9e\n'
500
+ with tempfile.TemporaryDirectory() as tmpd:
501
+ fn = os.path.join(tmpd, "bad.py")
502
+ with open(fn, "wb") as fp:
503
+ fp.write(src)
504
+ res = script_helper.run_python_until_end(fn)[0]
505
+ self.assertIn(b"Non-UTF-8", res.err)
506
+
507
@support.cpython_only
508
def test_compiler_recursion_limit(self):
509
# Expected limit is sys.getrecursionlimit() * the scaling factor
Misc/NEWS
@@ -10,6 +10,8 @@ Release date: tba
Core and Builtins
-----------------
12
13
+- Issue #24022: Fix tokenizer crash when processing undecodable source code.
14
15
- Issue #23309: Avoid a deadlock at shutdown if a daemon thread is aborted
16
while it is holding a lock to a buffered I/O object, and the main thread
17
tries to use the same I/O object (typically stdout or stderr). A fatal
Parser/tokenizer.c
@@ -1301,6 +1301,8 @@ verify_identifier(struct tok_state *tok)
1301
{
1302
PyObject *s;
1303
int result;
1304
+ if (tok->decoding_erred)
1305
+ return 0;
1306
s = PyUnicode_DecodeUTF8(tok->start, tok->cur - tok->start, NULL);
1307
if (s == NULL || PyUnicode_READY(s) == -1) {
1308
if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) {
@@ -1469,11 +1471,8 @@ tok_get(struct tok_state *tok, char **p_start, char **p_end)
1469
1471
c = tok_nextc(tok);
1470
1472
}
1473
tok_backup(tok, c);
- if (nonascii &&
- !verify_identifier(tok)) {
1474
- tok->done = E_IDENTIFIER;
+ if (nonascii && !verify_identifier(tok))
1475
return ERRORTOKEN;
1476
- }
1477
*p_start = tok->start;
1478
*p_end = tok->cur;
1479
return NAME;
0 commit comments