iconv codec: encoding names that glibc strips to nothing resolve to the locale encoding
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 62/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- python
调研方向
The bug is in iconv_search_function in Lib/encodings/init.py, which probes each name with _iconv_encode(name, '') and accepts any name glibc does not reject. Reproduce with codecs.lookup('!!!') or 'é'.encode('!!!') on Linux under a UTF-8 locale. Done means those names raise LookupError again while valid encodings such as 'utf-8' and 'latin-1' still resolve. The reporter's proposed check, requiring an ASCII alphanumeric before the first '/' or ',', is a starting point to verify.
由索引模型根据 Issue 内容生成。
描述
Bug report
Bug description
Since the iconv-based codec (gh-152997, GH-153001) landed, codecs.lookup() on Linux/glibc accepts encoding names whose charset part glibc's iconv_open() reduces to nothing, and returns a codec for the current locale's encoding. Before 3.16 these names raise LookupError.
>>> 'é'.encode('!!!')
b'\xc3\xa9' # 3.16.0a2, LC_ALL=C.UTF-8; 3.15: LookupError: unknown encoding: !!!
>>> bytes('é', '한글')
b'\xc3\xa9'
>>> import codecs; codecs.lookup('@').name
'@'
The result depends on the environment: with LC_ALL=C the same call fails with UnicodeEncodeError: '!!!' codec can't encode character '\xe9', because the codec is now ASCII.
| name | 3.15.2 | 3.16.0a2 (Linux, UTF-8 locale) |
|---|---|---|
'!!!', '@', '()', 'é', '한글' |
LookupError |
codec for the locale encoding |
'iconv:한글' |
LookupError |
codec for the locale encoding |
',', '/', '//TRANSLIT', '//abc' |
LookupError |
codec for the locale encoding |
'no-such-codec', '8', 'ü8' |
LookupError |
LookupError |
Cause
iconv_search_function in Lib/encodings/__init__.py rejects only the empty name and then probes the name with _iconv_encode(name, ''). glibc's iconv_open() first removes trailing whitespace, / and , and any //-suffix such as //TRANSLIT (gconv_parse_code() in iconv/gconv_charset.c), then drops every remaining character that is not an ASCII alphanumeric, / or one of _-.,: (strip() in iconv/gconv_charset.h), and treats the resulting empty name as the charset of the current locale (__gconv_open in iconv/gconv_open.c: "If the string is empty define this to mean the charset of the currently selected locale"). So the probe succeeds for any name made only of dropped characters, and also for names whose charset part is empty once the suffix is removed, such as ',', '/', '//TRANSLIT' and '//abc'.
def iconv_search_function(encoding):
...
if not name:
return None
# Test if the encoding is supported by iconv.
try:
_iconv_encode(name, '')
except (LookupError, OSError):
return None
Possible fix
Before probing, reject a name unless the part before its first / or , contains an ASCII alphanumeric. Requiring an alphanumeric anywhere in the name is not enough: '//TRANSLIT' and '//abc' contain one and still resolve to the locale encoding.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
- 主要语言
- Python
- 星标
- 78.2k
- 派生
- 36.6k
- 平均合并
- 19 小时 57 分钟
- 30 天内合并 PR
- 630
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/cpython 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
stdlib type-bug
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 1 天内回复
-
extension-modules stdlib topic-XML type-bug
难度 2/5 1-3 小时 新手友好度 76/100
python/cpython#161807 · 1 条评论 ·
维护者通常 1 天内回复
-
stdlib type-feature
难度 2/5 1-3 小时 新手友好度 78/100
python/cpython#161788 · 7 条评论 ·
维护者通常 1 天内回复
-
extension-modules topic-IO type-bug
难度 2/5 1-3 小时 新手友好度 75/100
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 62/100
cpp-best-practices/project_validator#267 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
Project-N-E-K-O/N.E.K.O#3725 ·
维护者通常 1 天内回复
-
[Bug] Sending a Telegram message chain twice duplicates mentions可能已有人在做 @123XieC 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
AstrBotDevs/AstrBot#11125 ·
维护者通常 1 天内回复
-
backend bug priority: high
难度 2/5 1-3 小时 新手友好度 72/100
nooriegarg/ContribIQ#73 ·
维护者通常 1 天内回复
-
bug p3 security
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复