Skip to content

Commit d5a9707

Browse files
authored
gh-103824: fix use-after-free error in Parser/tokenizer.c (#103993)
1 parent 99aab61 commit d5a9707

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Lib/test/test_tokenize.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
1212
INVALID_UNDERSCORE_LITERALS)
1313
from test.support import os_helper
14-
from test.support.script_helper import run_test_script, make_script
14+
from test.support.script_helper import run_test_script, make_script, run_python_until_end
1515
import os
1616
import token
1717

@@ -1470,6 +1470,19 @@ def test_comment_at_the_end_of_the_source_without_newline(self):
14701470
self.assertEqual(tok_name[tokens[i + 1].exact_type], tok_name[expected_tokens[i]])
14711471
self.assertEqual(tok_name[tokens[-1].exact_type], tok_name[token.ENDMARKER])
14721472

1473+
def test_invalid_character_in_fstring_middle(self):
1474+
# See gh-103824
1475+
script = b'''F"""
1476+
\xe5"""'''
1477+
1478+
with os_helper.temp_dir() as temp_dir:
1479+
filename = os.path.join(temp_dir, "script.py")
1480+
with open(filename, 'wb') as file:
1481+
file.write(script)
1482+
rs, _ = run_python_until_end(filename)
1483+
self.assertIn(b"SyntaxError", rs.err)
1484+
1485+
14731486
class UntokenizeTest(TestCase):
14741487

14751488
def test_bad_input_order(self):

Parser/tokenizer.c

+4
Original file line numberDiff line numberDiff line change
@@ -2552,6 +2552,10 @@ tok_get_fstring_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct
25522552
while (end_quote_size != current_tok->f_string_quote_size) {
25532553
int c = tok_nextc(tok);
25542554
if (c == EOF || (current_tok->f_string_quote_size == 1 && c == '\n')) {
2555+
if (tok->decoding_erred) {
2556+
return MAKE_TOKEN(ERRORTOKEN);
2557+
}
2558+
25552559
assert(tok->multi_line_start != NULL);
25562560
// shift the tok_state's location into
25572561
// the start of string, and report the error

0 commit comments

Comments
 (0)