Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Lib/test/test_tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2687,6 +2687,24 @@ def test_tolerant_fstring_closer_at_expression_entry_depth(self):
self._get_tokens(source, extra_tokens=extra_tokens)
self.assertEqual(caught.exception.args, error)

def test_fstring_unmatched_paren_before_indent(self):
# gh-162142: on 3.15.1 this input reached _get_col_offsets with a
# byte offset of -1 for the INDENT token, and the same-line fast
# path passed it to _PyPegen_byte_offset_to_character_offset_line,
# which read one byte before the line buffer. The mismatched
# bracket is now a TokenError before any INDENT is produced, so
# the over-read no longer triggers on main, but keep the input
# covered as a regression test for the guard in _get_col_offsets.
source = 'f"{)\n \u00e9\n'
for extra_tokens in (False, True):
with self.subTest(extra_tokens=extra_tokens):
with self.assertRaises(tokenize.TokenError) as caught:
self._get_tokens(source, extra_tokens=extra_tokens)
self.assertEqual(
caught.exception.args,
("f-string: unmatched ')'", (1, 4)),
)

def test_int(self):

self.check_tokenize('0xff <= 255', """\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix a one-byte heap over-read in the tokenizer: ``_get_col_offsets`` could
pass a byte offset of ``-1`` to
``_PyPegen_byte_offset_to_character_offset_line`` when an ``INDENT`` token
started before the current line, which then read one byte before the line
buffer. The same-line fast path now skips the call for a negative offset.
6 changes: 5 additions & 1 deletion Python/Python-tokenize.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,11 @@ _get_col_offsets(tokenizeriterobject *it, const struct token *token,

if (token_end >= view->end_line_start) {
Py_ssize_t end_byte_offset = token_end - view->end_line_start;
if (lineno == end_lineno) {
// byte_offset is -1 when the token starts before the current line's
// span, as an INDENT token does when the tokenizer still holds
// earlier lines in its buffer. The callee reads data[col_offset],
// so a negative offset would read before the line's buffer.
if (lineno == end_lineno && byte_offset >= 0) {
// Avoid rescanning the prefix of a very long line.
Py_ssize_t token_col_offset = _PyPegen_byte_offset_to_character_offset_line(line, byte_offset, end_byte_offset);
if (token_col_offset < 0) {
Expand Down
Loading