Skip to content

gh-155004: Fix iconv error handlers in a non-initial shift state - #155005

Open
serhiy-storchaka wants to merge 3 commits into
python:mainfrom
serhiy-storchaka:gh-155004-iconv-error-shift-state
Open

gh-155004: Fix iconv error handlers in a non-initial shift state#155005
serhiy-storchaka wants to merge 3 commits into
python:mainfrom
serhiy-storchaka:gh-155004-iconv-error-shift-state

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

The bytes an error handler returns are copied to the output as they are, so the output has to be back in the initial shift state first. It was reset only after the replacement, and with a NULL output buffer, so the closing sequence was never written and the replacement was read back as encoded data.

iconv_reset_shift_state() flushes into the output before the replacement is copied, and only when there is something to copy. iconv:ISO-2022-JP now matches iso2022_jp byte for byte with all four error handlers.

Encoding <char>😀<char> and decoding it back was broken for 146 (encoding, handler) pairs of the 1180 encodings iconv -l lists here, and for none now. The test fails without the change.

The iconv codecs are new in 3.16, so there is no NEWS entry.

The replacement bytes are copied to the output as they are, so the output
has to be back in the initial shift state first. Otherwise, in a stateful
encoding such as ISO-2022-CN, they are read back as encoded data.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
GNU libiconv resolves ISO-2022-CN-EXT but rejects U+4E2D, so probing the
character must not fail the test.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
An iconv() which substitutes an unencodable character instead of failing
leaves the shift state, and the substitute is then dropped from the output.
The descriptor and the output disagree after that, so the replacement bytes
are written in a non-initial shift state. Seen with citrus iconv (FreeBSD:
ISO-2022-KR and ISO-2022-CN) and libiconv (macOS: ISO-2022-JP).

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant