Skip to content

fix(executor): keep non-UTF-8 bytes opaque in $(cat) and ${x:off:len} - #781

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/772-comsub-binary-bytes
Oct 17, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/772-comsub-binary-bytes

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes items 1 and 2 of #772. Item 3 (read -r stripping a trailing CR) is a separate read-subsystem gap and is not touched here — see "Out of scope" below.

Summary

Two byte-domain defects where raw bytes were pushed through the string/carrier layer:

  1. $(cat FILE) on non-UTF-8 content → empty value + bogus "No such file".
    The word-level cat FILE… command-substitution shortcut used fs::read_to_string, so any invalid sequence failed the read and the Err arm reported ENOENT. A plain data=$(cat file) silently became "" and printed a message about a missing file that exists.
  2. ${x:off:len} fabricated U+E000 carrier bytes for invalid UTF-8.
    parameter_substring's multibyte branch sliced value.chars(), which cut a U+E000 raw-byte marker pair in half and leaked the introducer/payload chars.

Reproduction (before → after)

Measured against WSL GNU Bash 5.3.0, LF script files, scratch cwd.

probe GNU bash master b5793580 this PR
x=$(cat h) (h = 0xff) ff, no stderr len=0 + cat: 'h': No such file… ff, no stderr
x=$(cat g h) (g=abc) abc\xff (len 4) fails abc\xff
x=$(cat bin) (NUL-free) exact bytes empty + error exact bytes
x=$(cat noeol) noeol noeol noeol
x=$(cat utf8) (guard) exact exact exact
printf '%s' "${x:0:1}" (x=$'\xff') ff ee 80 80 ff
printf '%s' "${x:1}" (empty) ee 84 80 (empty)
printf '%s' "${y:2}" (y=$'a\xffb') 62 ee 84 80 62 62

Mechanism

Both were the same class flagged by the #695/#718/#1201 family: transport text vs payload at the arm boundary.

Item 1 — GNU cat.c reads a file operand as a byte stream (safe_read → write) and command_substitute (subst.c:7143 → read_comsub) captures the child's pipe as bytes. The shortcut now fs::reads and rides the same bytes_to_shell_text carrier contract every other capture uses, dropping NUL first exactly as SubstitutionOutput::readback does (GNU read_comsub discards NUL while reading the pipe).

Item 2 — GNU subst.c:9087-9092 parameter_brace_substring calls mb_substring (subst.c:9022) in a multibyte locale; each step is one ADVANCE_CHAR (include/shmbutil.h:114-147), which advances a valid multibyte sequence by its length and an invalid byte by one (mbrlen == (size_t)-1/-2). The new locale::multibyte_char_offsets performs that walk over the decoded byte view; parameter_substring slices by those offsets and re-encodes with bytes_to_shell_text. This is the same walk ${#v} already used (parameter_char_length, MB_STRLEN subst.c:8308 → utf8.c:167-184), so the two now agree.

Files

  • src/executor/command_substitution.rs — read cat operands as bytes.
  • src/executor/parameter_ops.rs — multibyte substring walks the byte view.
  • src/locale.rs — new multibyte_char_offsets (ADVANCE_CHAR port) + unit test.
  • tests/issue772_comsub_non_utf8_regressions.rs — 10 tests.
  • tests/issue772_substring_raw_byte_regressions.rs — 6 tests.

Verification

  • GNU oracle: a self-contained fixture script run under rubash.exe and wsl bash in separate scratch dirs; diff -r of the raw result files is byte-identical, stdout and stderr both empty, in both the UTF-8 locale and LC_ALL=C. Matrix: 0xcd, 0xff, mixed NUL-free binaries (A ff B fe C cd D 80 E), carrier-range bytes (0x0c/0x11), no-trailing-newline, trailing-newline stripping, UTF-8 and Euro/multibyte guards, negative offsets, concatenated operands, and the redirect (cat < h) / 2>/dev/null alternate paths.
  • Suites: cargo test --lib (690 passed) plus the neighbouring substitution/carrier suites (comsub_nonascii_span, issue71/154/277/305/319/320/335/354/362/368/376/379/384/416_417/434/485×2/513/539/540/548/604/707, posixexp2, stdin_multibyte, parameter_transform_tests, decode_to_visible_text) — all green, 0 failed.
  • cargo fmt --check clean.

Tests use a sandboxed cwd, Rust-written byte fixtures, and assert byte-exact stdout/stderr/rc.

Out of scope

Item 3 (IFS= read -r drops a trailing CR that is content) is reproduced, but it lives in the read subsystem: trim_read_input_literal (src/executor/read_helpers.rs:542-559) permissively pops trailing \r on Windows because the pty/pipe readers hand CRLF records over without the \n, and the code comment there already marks the tightening as wave-2 work. Fixing it needs those readers to report whether the delimiter they consumed was preceded by a \r; I did not touch it.

Mentioned for completeness, not part of this change: the genuine-missing-file fast-path message stays single-quoted (cat: 'missing': …) whereas GNU coreutils quotef leaves a plain name unquoted. Non-UTF-8 content no longer reaches that arm.

Refs #772.

`$(cat FILE)` on non-UTF-8 content returned an empty value and reported a
file that plainly exists as "No such file"; `${x:off:len}` over an invalid
byte emitted the UTF-8 encoding of the U+E000 carrier pair. Both re-encoded
or rejected raw bytes at the string/carrier boundary.

Item 1 (subst.c:7143 command_substitute / cat.c safe_read): the word-level
`cat FILE...` command-substitution shortcut used fs::read_to_string, so any
invalid sequence failed the read and the Err arm reported ENOENT. GNU cat
reads the operand as a byte stream, so read bytes and ride the same
bytes_to_shell_text carrier contract as the other captures, dropping NUL the
way read_comsub does (substitution_metadata SubstitutionOutput::readback).

Item 2 (subst.c:9087-9092 parameter_brace_substring -> subst.c:9022
mb_substring -> include/shmbutil.h:114-147 ADVANCE_CHAR): the multibyte
substring branch sliced value.chars(), cutting a U+E000 marker pair in half
and leaking a carrier char. ADVANCE_CHAR counts one character per valid
multibyte sequence and one per invalid byte, so walk the decoded byte view
with the new locale::multibyte_char_offsets and re-encode the slice.

Verified byte-identical to WSL GNU Bash 5.3.0 over invalid bytes (0xcd,
0xff), NUL-free binary files, a file with no trailing newline, and UTF-8 /
multibyte guards, in both the UTF-8 and C locales.

Refs #772 (item 3, `read -r` stripping a trailing CR, is a separate
read-subsystem CRLF-provenance gap and is not part of this change).
@caomengxuan666
caomengxuan666 merged commit b2dd10e into master Oct 17, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/772-comsub-binary-bytes branch October 18, 2026 09:29
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