Repository navigation
fix(executor): keep non-UTF-8 bytes opaque in $(cat) and ${x:off:len} - #781
Merged
Merged
Conversation
`$(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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes items 1 and 2 of #772. Item 3 (
read -rstripping 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:
$(cat FILE)on non-UTF-8 content → empty value + bogus "No such file".The word-level
cat FILE…command-substitution shortcut usedfs::read_to_string, so any invalid sequence failed the read and theErrarm reportedENOENT. A plaindata=$(cat file)silently became""and printed a message about a missing file that exists.${x:off:len}fabricated U+E000 carrier bytes for invalid UTF-8.parameter_substring's multibyte branch slicedvalue.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.
b5793580x=$(cat h)(h=0xff)ff, no stderrlen=0+cat: 'h': No such file…ff, no stderrx=$(cat g h)(g=abc)abc\xff(len 4)abc\xffx=$(cat bin)(NUL-free)x=$(cat noeol)noeolnoeolnoeolx=$(cat utf8)(guard)printf '%s' "${x:0:1}"(x=$'\xff')ffee 80 80ffprintf '%s' "${x:1}"ee 84 80printf '%s' "${y:2}"(y=$'a\xffb')62ee 84 80 6262Mechanism
Both were the same class flagged by the #695/#718/#1201 family: transport text vs payload at the arm boundary.
Item 1 — GNU
cat.creads a file operand as a byte stream (safe_read→write) andcommand_substitute(subst.c:7143 →read_comsub) captures the child's pipe as bytes. The shortcut nowfs::reads and rides the samebytes_to_shell_textcarrier contract every other capture uses, dropping NUL first exactly asSubstitutionOutput::readbackdoes (GNUread_comsubdiscards NUL while reading the pipe).Item 2 — GNU subst.c:9087-9092
parameter_brace_substringcallsmb_substring(subst.c:9022) in a multibyte locale; each step is oneADVANCE_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 newlocale::multibyte_char_offsetsperforms that walk over the decoded byte view;parameter_substringslices by those offsets and re-encodes withbytes_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— readcatoperands as bytes.src/executor/parameter_ops.rs— multibyte substring walks the byte view.src/locale.rs— newmultibyte_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
rubash.exeandwsl bashin separate scratch dirs;diff -rof the raw result files is byte-identical, stdout and stderr both empty, in both the UTF-8 locale andLC_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/nullalternate paths.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 --checkclean.Tests use a sandboxed cwd, Rust-written byte fixtures, and assert byte-exact stdout/stderr/rc.
Out of scope
Item 3 (
IFS= read -rdrops 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\ron 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 coreutilsquotefleaves a plain name unquoted. Non-UTF-8 content no longer reaches that arm.Refs #772.