Title: gh-151763: Fix OOM-0034 tokenizer offset error handling by zainnadeem786 · Pull Request #151798 · python/cpython · GitHub
Open Graph Title: gh-151763: Fix OOM-0034 tokenizer offset error handling by zainnadeem786 · Pull Request #151798 · python/cpython
X Title: gh-151763: Fix OOM-0034 tokenizer offset error handling by zainnadeem786 · Pull Request #151798 · python/cpython
Description: 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:
a NULL pointer dereference (access violation), or
a tokenizer result being returned while a MemoryError was still pending.
Issue
_PyPegen_byte_offset_to_character_offset_line() in Parser/pegen.c calls PyUnicode_AsUTF8() and immediately dereferences the returned pointer.
Under memory-allocation failure, PyUnicode_AsUTF8() can return NULL with a pending MemoryError. The existing code did not check for this condition before accessing the returned buffer.
Example crash path:
tokenizeriter_next()
└── _get_col_offsets()
└── _PyPegen_byte_offset_to_character_offset_line()
└── PyUnicode_AsUTF8()
└── returns NULL under OOM
└── NULL dereference
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:
import _testcapi
import _tokenize
source = "if True:\n \u00e9 = 1\n"
it = _tokenize.TokenizerIter(
iter(source.splitlines(True)).__next__,
extra_tokens=False,
)
for _ in range(5):
next(it)
_testcapi.set_nomemory(2, 3)
next(it)
Observed result before the fix:
Windows fatal exception: access violation
RC 3221225477
Further investigation showed that a helper-only NULL check was not sufficient.
_get_col_offsets() ignored failure returns from offset-conversion helpers, allowing tokenizeriter_next() to continue execution with a pending MemoryError.
This could also result in:
SystemError:
Open Graph Description: 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: a NULL pointer dereference (access viol...
X Description: 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: a NULL pointer dereference (access viol...
Opengraph URL: https://github.com/python/cpython/pull/151798
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:17021053-7aae-8547-3367-4b35c1477149 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | AE16:1F69C6:162A5D:1E0516:6A532FE5 |
| html-safe-nonce | d6bbb8a889c4ef63ded6be62fd6686b0489f3a114e79409e055f5eea64f43d16 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRTE2OjFGNjlDNjoxNjJBNUQ6MUUwNTE2OjZBNTMyRkU1IiwidmlzaXRvcl9pZCI6IjM0NTYzMDU3OTcxNTEyNzI5MzMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 12b4ffc04531e180a39b39505e6db8d6748d2adbbf22fbc4203cb5e8fa63e84b |
| hovercard-subject-tag | pull_request:3903741777 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/151798/files |
| twitter:image | https://avatars.githubusercontent.com/u/165761530?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/165761530?s=400&v=4 |
| og:image:alt | 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: a NULL pointer dereference (access viol... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width