gh-151763: Fix OOM-0034 tokenizer offset error handling#151798
Open
zainnadeem786 wants to merge 1 commit into
Open
gh-151763: Fix OOM-0034 tokenizer offset error handling#151798zainnadeem786 wants to merge 1 commit into
zainnadeem786 wants to merge 1 commit into
Conversation
Author
|
The failing TSan free-threading job appears to fail in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses OOM-0034 from gh-151763.
It fixes an out-of-memory (OOM) failure path in the tokenizer offset conversion logic that could lead to:
MemoryErrorwas still pending.Issue
_PyPegen_byte_offset_to_character_offset_line()inParser/pegen.ccallsPyUnicode_AsUTF8()and immediately dereferences the returned pointer.Under memory-allocation failure,
PyUnicode_AsUTF8()can returnNULLwith a pendingMemoryError. The existing code did not check for this condition before accessing the returned buffer.Example crash path:
The issue was reproducible with
_testcapi.set_nomemory()using non-ASCII source lines that require byte-offset to character-offset conversion.Reproduction
Using a CPython debug build and OOM injection:
Observed result before the fix:
Further investigation showed that a helper-only NULL check was not sufficient.
_get_col_offsets()ignored failure returns from offset-conversion helpers, allowingtokenizeriter_next()to continue execution with a pendingMemoryError.This could also result in:
Root Cause
Two independent problems existed:
_PyPegen_byte_offset_to_character_offset_line()did not check whetherPyUnicode_AsUTF8()failed._get_col_offsets()ignored failures from:_PyPegen_byte_offset_to_character_offset_line()_PyPegen_byte_offset_to_character_offset_raw()As a result, tokenizer execution could continue after offset conversion had already failed.
Fix
This PR:
PyUnicode_AsUTF8().-1when UTF-8 conversion fails._get_col_offsets()fromvoidtoint.MemoryError.Regression Tests
Adds regression coverage in
Lib/test/test_tokenize.py.The test:
_testcapi.set_nomemory()in a subprocess.MemoryErrorinstead of crashes or invalid tokenizer results.Validation
Built and tested using a CPython debug build:
Executed:
Result:
Also verified:
No whitespace errors were reported.
Impact
This change converts tokenizer OOM crash paths into correct exception propagation and ensures that allocation failures during column-offset conversion are handled safely and consistently.
Addresses OOM-0034 from gh-151763.