iconv codec: encoding names that glibc strips to nothing resolve to the locale encoding

未關閉
#161,924 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 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 內容生成。

描述

3.16 stdlib topic-unicode type-bug
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

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。