Skip to content

fix(builtins): read -d keys on the option word's first raw byte - #3955

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/3944-read-d-word-first-byte
Nov 19, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/3944-read-d-word-first-byte

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #3944.

GNU contract

builtins/read.def:385 takes the -d delimiter as the option word's FIRST BYTE (delim = (unsigned char)*list_optarg) and read.def:834 compares it against raw stream bytes — in every locale. -d é keys on 0xC3; -d $'\377' keys on 0x377's byte 0xFF.

Root cause

Rubash widened the word to a delimiter CHARACTER (read_delimiter_char) whose UTF-8 encoding's first byte then drove FdRecordScan's compare. A lone byte word >= 0x80 widens to a Latin-1 char whose encoding starts with 0xC3/0xC2 — a byte GNU never tests for that word. Under a UTF-8 locale every file-backed serve (exec 0<file, -u N, while read; done < file) cut the record VALUE at the right byte but returned rc 1 (delimiter "not found") and left the descriptor offset past the wrong place; only byte 0xC3 worked end to end. The byte-locale faces were correct by accident (the widened char's byte IS the word's byte there).

Fix

  • read_delimiter_first_byte(word) — read.def:385's value from the raw word.
  • Every -d parse site records it (Executor::read_delim_word_byte); a fresh read clears it.
  • read_input_for_command mirrors it onto the fd table for the acquisition and retires it after (a select serve must never key on a stale byte).
  • FdRecordScan::new keys its first-byte compare on the word byte; callers without the word (select, mapfile) keep the derived form.

Verification

  • Pins tests/issue3944_read_d_word_first_byte.rs: fail-first verified — 6/8 red at 1d85d15, green with the fix.
  • Issue seed R138-A: 12/12 under WSL GNU bash 5.3.20 (script files) vs the Linux rubash build.
  • Delimiter regression families (2662/3212/3291/3294/2819/3191) green.
  • Base carries 16 known-red cli_tests, unchanged.

…3944)

GNU builtins/read.def:385 takes the -d delimiter as the option
word's FIRST BYTE (delim = (unsigned char)*list_optarg) and
read.def:834 compares it against raw stream bytes in every locale.
Rubash widened the word to a delimiter CHARACTER instead; a lone
byte word >= 0x80 (-d $'\377') widens to a Latin-1 char whose UTF-8
encoding starts with 0xC3, so under a UTF-8 locale the FdRecordScan
byte compare keyed on a byte GNU never tests: every file-backed
serve (exec 0<file, -u N, while-read <file) cut the record value at
the right byte but reported rc 1 and left the descriptor offset
past the wrong place — only byte 0xC3 worked end to end.

The word's first byte now travels beside the widened char
(Executor::read_delim_word_byte, set by every -d parse site, cleared
on a fresh read), mirrors onto the fd table for the acquisition
(read_input_for_command), and drives FdRecordScan's first_byte; the
text-domain derivation (read_delimiter_first_byte) is recorded for
the follow-up serves. Callers without the option word (select,
mapfile serves) keep the derived form.

Pins: tests/issue3944_read_d_word_first_byte.rs (fail-first
verified: 6/8 red at 1d85d15, green with the fix). Seed R138-A
12/12 under WSL GNU bash 5.3.20 vs the Linux rubash build. Oracle
citations: read.def:385, read.def:834.
@caomengxuan666
caomengxuan666 merged commit 9c520c3 into master Nov 19, 2026
8 of 12 checks passed
caomengxuan666 added a commit that referenced this pull request Nov 19, 2026
…IR (#3960)

Follow-up to #3955/#3954 (the same hazard the #3958 branch scoped for its own pins): parallel census stems sharing the repo cwd collided on the pins' relative file operands. Expectations unchanged.
@caomengxuan666
caomengxuan666 deleted the fix/3944-read-d-word-first-byte branch November 19, 2026 17: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.

[bug] read -d with a single-byte delimiter >= 0x80 fails on file-backed input under a UTF-8 locale

1 participant