Skip to content

fix(iconv): write real bytes, add -o, -c and //IGNORE - #2937

Merged
chaliy merged 3 commits into
mainfrom
claude/project-thread-cs5tvz
Nov 18, 2026
Merged

chaliy merged 3 commits into
mainfrom
claude/project-thread-cs5tvz

Conversation

@chaliy

@chaliy chaliy commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Михайло · project thread

Part 1 of #2935 (code pages follow in a separate PR).

What changed

  • iconv now outputs raw bytes: -t latin1 writes Latin-1 bytes and -t utf-16* writes real UTF-16 code units. Before, output went back through a String.
  • UTF-16 output has a BOM and is little-endian. UTF-16LE and UTF-16BE have no BOM, as in GNU. Before, utf-16le was an alias of the BOM form.
  • Errors use the GNU wording and give the byte position in the input (illegal input sequence at position N, incomplete character or shift sequence at end of buffer). The part converted before the error still goes to stdout, with exit 1.
  • -o FILE is not written when conversion stops on an error. iconv -f X -t Y -o in.csv in.csv with a wrong -f guess leaves the file as it was.
  • -c and //IGNORE drop invalid input bytes, characters the target cannot hold, and a character cut off at the end of the input. They convert the rest and exit 1. With -o, the rest is still written, because dropping was requested. That is how a damaged export gets saved.
  • //TRANSLIT now only applies to characters the target cannot hold, so latin1//TRANSLIT keeps é. It now covers multi-character spellings (ß→ss, €→EUR) and quotes and dashes. Characters it does not know become ?.
  • Also: several input files are concatenated, and the forms -fX, --from-code=X, --to-code, --output and -s are accepted. -s and empty suffixes behave as in glibc.
  • Output growth is charged to the shared execution budget, the same way as od.

Why

Excel and Windows CSV exports are not UTF-8. Text builtins replace invalid UTF-8 with � (L-STREAM-001), so iconv is the step that has to work before awk, sed or cut can read these files. It could not repair double-encoded text, and it damaged UTF-16 output.

Before / After

$ printf 'K\303\203\302\266ln\n' | iconv -f utf-8 -t latin1      # repair double-encoded
before: Köln        after: Köln
$ printf 'ab' | iconv -f utf-8 -t utf-16le | od -An -tx1
before: (lossy, BOM)  after: 61 00 62 00
$ printf 'K\366ln\n' > e.csv; iconv -f utf-8 -t latin1 -o e.csv e.csv; echo $?
before: iconv: unknown option '-o'
after:  iconv: illegal input sequence at position 1 / 1, e.csv unchanged
$ printf 'a\377b\n' | iconv -c -f utf-8 -t utf-8; echo $?
before: iconv: unknown option '-c'   after: ab / 1

A new differential test checks the output bytes and exit codes against the host's glibc iconv for UTF-16 (with BOM, BE, surrogates), Latin-1 in both directions, and a truncated input.

The -c exit status is the one intended difference. POSIX and GNU libiconv exit 1, and glibc 2.39 exits 0. This is recorded as L-ICONV-001.

Risk

  • Low. The change is limited to the iconv builtin.
  • Code that relied on -t latin1 returning UTF-8 text will now get Latin-1 bytes, which is the intended fix.

Checklist

  • Tests added or updated: 40 unit tests (positive, negative, and every byte value through every charset with no panic), plus 2 integration tests in byte_stream_tests
  • Backward compatibility considered: internal, no compatibility needed. Knowledge updated (limitations L-ICONV-001, threat-model row, log)

threat_model_tests::resource_exhaustion::bounded_* fails 6 tests locally on unmodified main too. It depends on the environment and is not related to this change.

iconv built its output as a String, so -t latin1 wrote UTF-8 again and
-t utf-16 went through a lossy decode. Convert char by char from input
bytes to output bytes, report GNU byte positions, keep the converted
prefix on error. UTF-16LE/BE write no BOM. -o writes only after a full
conversion. -c and //IGNORE drop bad input and unconvertible chars and
exit 1. Output growth is charged to the execution budget.

Refs #2935
@chaliy chaliy self-assigned this Nov 18, 2026

@bashkit-pr-reviewer bashkit-pr-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of iconv byte rewrite: conversion core, -o atomicity, and -c/IGNORE handling look solid and well tested, but -o still overwrites on skipped bytes despite the documented guarantee, plus two smaller divergences below.

Comment thread crates/bashkit/src/builtins/iconv.rs Resolved
Comment thread crates/bashkit/src/builtins/iconv.rs Resolved
Comment thread crates/bashkit/src/builtins/iconv.rs Resolved
@bashkit-pr-reviewer

bashkit-pr-reviewer Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Review summary — iconv real-bytes + -o/-c (2937@dda0fd23, rebase follow-up)

Verdict: no new findings. This push looks like a rebase onto main (base 7c0ce82e): the iconv.rs hunks, byte_stream_tests.rs differential block, and L-ICONV-001 row match the previously reviewed 736f428e content, including the -c -o FILE writes-rest wording, the -s-keeps-errors pin, and the empty-suffix acceptance tests.

Prior findings remain resolved; no new review posted. Context re-verified from the snapshot: -o gating (failure vs requested -c drops), -s arm, parse_encoding_spec empty-suffix handling, and the limitations row. No sandbox, panic, or budget issues in this push.

Address review: -c -o FILE intentionally writes the converted rest (the
point of -c); the header and usage now say so and an in-place test pins
it. -s and empty //, suffixes match glibc 2.39; tests pin both.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
bashkit 7266fce Commit Preview URL

Branch Preview URL
Nov 18 2026, 06:09 PM

@chaliy
chaliy merged commit e9b726c into main Nov 18, 2026
67 checks passed
@chaliy
chaliy deleted the claude/project-thread-cs5tvz branch November 18, 2026 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant