t-string tokeniser reports `MemoryError` on invalid input
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
In short
I found a bug in the t-string tokeniser by fuzzing it, specifically in set_ftstring_expr (from Parser/lexer/lexer.c).
The following is a reproducer which consistently reports a MemoryError, which isn't the expected error (I would expect TokenError).
import tokenize
import io
list(tokenize.tokenize(io.BytesIO(b't"{!\n!x').readline))
results in the following:
Traceback (most recent call last):
File "<python-input-3>", line 1, in <module>
list(tokenize.tokenize(io.BytesIO(b't"{!\n!x').readline))
~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/bits/python315/Lib/tokenize.py", line 499, in tokenize
yield from _generate_tokens_from_c_tokenizer(rl_gen.__next__, encoding, extra_tokens=True)
File "/home/bits/python315/Lib/tokenize.py", line 634, in _generate_tokens_from_c_tokenizer
for info in it:
^^
MemoryError
Longer version
set_ftstring_expr in Parser/lexer/lexer.c, extracts the source text of the expression inside {...} so it can be attached as metadata to the token.
The function tracks two fields on the tokenizer mode struct:
last_expr_sizeis set when{is seen, with valuestrlen(tok->cur)(bytes fromtok->curto end of buffer)last_expr_endis set when!,}, or:is seen with valuestrlen(tok->start)(bytes from the delimiter to end of buffer)
The intended expression length is last_expr_size - last_expr_end (number of characters between the two positions).
However, this does not work when there are two ! across two lines, for example:
t"{expr!conv1
n!conv2
The sequence of events (LLM-analysed):
-
{on line 1:_PyLexer_update_ftstring_expr(tok, '{')setslast_expr_size = strlen(tok->cur)(bytes remaining on line 1 after{) andlast_expr_end = -1. -
First
!on line 1:_PyLexer_update_ftstring_expr(tok, '!')setslast_expr_end = strlen(tok->start)(a smaller value from the same line 1 buffer).set_ftstring_exprruns, computeslast_expr_size - last_expr_end > 0, stores result intoken->metadata. Crucially,last_expr_endis now ≥ 0. -
Newline:
_PyLexer_update_ftstring_expr(tok, 0)would normally append the next line's content and growlast_expr_size, keeping the measurements in sync. But thecase 0branch has a guard: it skips the append whenlast_expr_end >= 0. Because the first!already setlast_expr_end, the append is skipped andlast_expr_sizeis locked at its small line-1 value. -
Second
!on line 2:_PyLexer_update_ftstring_expr(tok, '!')setslast_expr_end = strlen(tok->start)measured in the new line 2 buffer. If line 2 has more content after!than line 1 had after{, this newlast_expr_end > last_expr_size. A newtokenstruct is active (the previous one was emitted), sotoken->metadata == NULLandset_ftstring_exprruns the full computation -- producing a negativePy_ssize_t.
That negative value is then used in three places:
PyMem_Malloc((last_expr_size - last_expr_end + 1) * sizeof(char))cast tosize_t,-N+1becomes huge.PyUnicode_DecodeUTF8(buf, last_expr_size - last_expr_end, NULL)The length argument isPy_ssize_tbutunicodeobject.cimmediately checksif (size > PY_SSIZE_T_MAX)after casting -- a negative value cast tosize_tis huge and trips the overflow guard, raisingPyErr_NoMemory.- Loop bounds (
for i < ...; while i < ...) -- a negative bound means the loops never execute, so the comment-stripping pass is silently skipped for inputs that reach thehash_detectedbranch.
Proposed fix
Compute expr_len once at the top of set_ftstring_expr and return -1 immediately if it is negative. Returning -1 signals a tokenizer error, which surfaces to Python callers as TokenError -- the correct outcome for malformed source. All five downstream uses of the subtraction are replaced with expr_len.
The fix (with a regression test) is already implemented on my branch.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-149445
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 Parser/lexer/lexer.c 中的 set_ftstring_expr 开始,跟踪其结果如何到达 tokenize.py 路径。使用提供的 tokenize 复现程序,并检查链接实现中现有的回归测试;当格式错误的输入引发 TokenError 而不是 MemoryError 时,即表示完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- c, python
- 领域
- backend, compilers
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 25/100