Skip to content

[3.13] gh-144759: Fix undefined behavior from NULL pointer arithmetic in lexer (GH-144788)#145355

Merged
pablogsal merged 1 commit intopython:3.13from
StanFromIreland:backport-e6110ef-3.13
Feb 28, 2026
Merged

[3.13] gh-144759: Fix undefined behavior from NULL pointer arithmetic in lexer (GH-144788)#145355
pablogsal merged 1 commit intopython:3.13from
StanFromIreland:backport-e6110ef-3.13

Conversation

@StanFromIreland
Copy link
Member

@StanFromIreland StanFromIreland commented Feb 28, 2026

Guard against NULL pointer arithmetic in _PyLexer_remember_fstring_buffers and _PyLexer_restore_fstring_buffers. When start or multi_line_start are NULL (uninitialized in tok_mode_stack[0]), performing NULL - tok->buf is undefined behavior. Add explicit NULL checks to store -1 as sentinel and restore NULL accordingly.

Add test_lexer_buffer_realloc_with_null_start to test_repl.py that exercises the code path where the lexer buffer is reallocated while tok_mode_stack[0] has NULL start/multi_line_start pointers. This triggers _PyLexer_remember_fstring_buffers and verifies the NULL checks prevent undefined behavior.
(cherry picked from commit e6110ef)

…hmetic in lexer (pythonGH-144788)

Guard against NULL pointer arithmetic in `_PyLexer_remember_fstring_buffers`
and `_PyLexer_restore_fstring_buffers`. When `start` or `multi_line_start`
are NULL (uninitialized in tok_mode_stack[0]), performing `NULL - tok->buf`
is undefined behavior. Add explicit NULL checks to store -1 as sentinel
and restore NULL accordingly.

Add test_lexer_buffer_realloc_with_null_start to test_repl.py that
exercises the code path where the lexer buffer is reallocated while
tok_mode_stack[0] has NULL start/multi_line_start pointers. This
triggers _PyLexer_remember_fstring_buffers and verifies the NULL
checks prevent undefined behavior.
(cherry picked from commit e6110ef)

Co-authored-by: Ramin Farajpour Cami <ramin.blackhat@gmail.com>
@pablogsal pablogsal merged commit 1a2b0fb into python:3.13 Feb 28, 2026
45 checks passed
@pablogsal
Copy link
Member

Thanks a lot @StanFromIreland

@StanFromIreland StanFromIreland deleted the backport-e6110ef-3.13 branch February 28, 2026 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants