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
16 changes: 16 additions & 0 deletions Lib/test/test_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,22 @@ def test_multiline_string_parsing(self):
output = kill_python(p)
self.assertEqual(p.returncode, 0)

@cpython_only
def test_lexer_buffer_realloc_with_null_start(self):
# gh-144759: NULL pointer arithmetic in the lexer when start and
# multi_line_start are NULL (uninitialized in tok_mode_stack[0])
# and the lexer buffer is reallocated while parsing long input.
long_value = "a" * 2000
user_input = dedent(f"""\
x = f'{{{long_value!r}}}'
print(x)
""")
p = spawn_repl()
p.stdin.write(user_input)
output = kill_python(p)
self.assertEqual(p.returncode, 0)
self.assertIn(long_value, output)

def test_close_stdin(self):
user_input = dedent('''
import os
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix undefined behavior in the lexer when ``start`` and ``multi_line_start``
pointers are ``NULL`` in ``_PyLexer_remember_fstring_buffers()`` and
``_PyLexer_restore_fstring_buffers()``. The ``NULL`` pointer arithmetic
(``NULL - valid_pointer``) is now guarded with explicit ``NULL`` checks.
8 changes: 4 additions & 4 deletions Parser/lexer/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ _PyLexer_remember_fstring_buffers(struct tok_state *tok)

for (index = tok->tok_mode_stack_index; index >= 0; --index) {
mode = &(tok->tok_mode_stack[index]);
mode->start_offset = mode->start - tok->buf;
mode->multi_line_start_offset = mode->multi_line_start - tok->buf;
mode->start_offset = mode->start == NULL ? -1 : mode->start - tok->buf;
mode->multi_line_start_offset = mode->multi_line_start == NULL ? -1 : mode->multi_line_start - tok->buf;
}
}

Expand All @@ -27,8 +27,8 @@ _PyLexer_restore_fstring_buffers(struct tok_state *tok)

for (index = tok->tok_mode_stack_index; index >= 0; --index) {
mode = &(tok->tok_mode_stack[index]);
mode->start = tok->buf + mode->start_offset;
mode->multi_line_start = tok->buf + mode->multi_line_start_offset;
mode->start = mode->start_offset < 0 ? NULL : tok->buf + mode->start_offset;
mode->multi_line_start = mode->multi_line_start_offset < 0 ? NULL : tok->buf + mode->multi_line_start_offset;
}
}

Expand Down
Loading