python / python/cpython

Unexpected nested exception with b64decode

オープン
#129,505 コメント 6 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

docs stdlib
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Bug report

Bug description:

The b64decode function of the base64 standard library module can behave in unexpected ways with invalid inputs.

Take this example:

#!/usr/bin/python3
import base64
base64.b64decode('a\xd6==')

According to the docs, I would expect that invalid characters in the input cause a binascii.Error exception. However, this example gives me the following:

Traceback (most recent call last):
  File "/usr/lib/python3.12/base64.py", line 37, in _bytes_from_decode_data
    return s.encode('ascii')
           ^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'ascii' codec can't encode character '\xd6' in position 1: ordinal not in range(128)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/tmp/./test", line 3, in <module>
    base64.b64decode('a\xd6==')
  File "/usr/lib/python3.12/base64.py", line 83, in b64decode
    s = _bytes_from_decode_data(s)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/base64.py", line 39, in _bytes_from_decode_data
    raise ValueError('string argument should contain only ASCII characters')
ValueError: string argument should contain only ASCII characters

So not only do I get an exception that I don't expect according to the docs, it appears there is a bug in the internal exception handling causing an exception (ValueError) during another exception (UnicodeEncodeError).

CPython versions tested on:

3.13

Operating systems tested on:

Linux

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

非ASCII入力の例を再現し、その後 base64.py の b64decode と _bytes_from_decode_data の周辺を読んで、観測された例外と文書化された動作を比較します。無効な入力が、予期しない入れ子になった traceback なしに、文書化された例外の動作に従えば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
backend
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。