Skip to content

fix(lexer): comsub heredoc gather ends at delimiter + IFS_GLUE record (Refs #4141) - #4165

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/4141-comsub-piped-heredoc-delimiter-glue
Nov 24, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/4141-comsub-piped-heredoc-delimiter-glue

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #4141. UNVERIFIED-BY-CAPTAIN.

Mechanism (diagnosed before touching code)

Shape: echo "$(cat <<E | sort\nbody\nE)" printed E\x1c + body instead of body.

Route map, measured against the cloud GNU 5.3.20 oracle with script-file batteries:

  • The cat heredoc comsub plan models at most the [sort, -u] pipeline arm (heredoc_comsub_produce). A plain sort, a cat second stage, or a third stage declines at produce time and the body falls to the real executor (command_list_substitution_output_typed), which re-tokenizes the extracted body text.
  • In that text the delimiter line carries the reader's IFS_GLUE record byte: the outer reader splices it in front of the closing ) to record that the substitution closed on the delimiter line, and extraction consumes the ) itself, leaving E\x1c. The plan route strips IFS_GLUE wholesale before tokenizing, so it never saw the byte.
  • The real route's gatherer, feed_awaiting_body (src/lexer/mod.rs), compares the raw line against the delimiter. Its in-comsub trailing-residue arm required the rest after the delimiter to trim() to empty — \x1c is not whitespace — so the line failed all four terminator arms and was pushed into the body, leaking into the pipeline's input.

Leak class (oracle-verified): quoted comsub + plan-declined pipeline + E) delimiter line — plain sort, sort with multi-line body, pipe to cat, <<- tab form, body with expansions, quoted delimiter, three-stage pipeline all leaked. Already correct and kept correct: [sort, -u] (plan), non-piped quoted, assignment form, unquoted form, plain heredoc outside a comsub.

GNU basis

gather_here_documents (third_party/bash/make_cmd.c:602) ends the document at the exact delimiter line; the PST_EOFTOKEN arm (make_cmd.c:606-611) ends it at the delimiter followed by the substitution's EOF token. In the extracted body the EOF token is already consumed; the glue record is its only trace.

Fix (single mechanism)

The in-comsub residue arm's invariant was wrong, not incomplete: the residue after the delimiter is reader-side residue (whitespace, or the reader's own glue record), never body text. The arm now removes IFS_GLUE from the residue before the trim check. Delimiter + glue/whitespace terminates; delimiter + real words (E more) still does not. No body bytes change — only the terminator decision. This corrects the predicate for the whole class (rubash#117 sanctioned move 1); it adds no per-symptom guard.

What this PR deletes

Nothing structural: it deletes the false invariant that the post-delimiter residue consists solely of whitespace. No code path, arm, or mechanism is removed; no new mechanism is added.

Net lines

  • src/lexer/mod.rs: +14/−1 (the residue test plus its citation comment)
  • tests: +184 (new pin file)
  • Total: +198/−1; src net +13.

Evidence

  • Fail-first: new pins tests/issue4141_quoted_comsub_piped_heredoc_delimiter_regressions.rs — 4 of 5 red on pristine master d5ae7bfb, every red solely the E\u{1c} stdout leak (all stderr assertions already green pre-fix); the adjacent-routes guard green pre-fix by design. Post-fix 5/5 green in both locales.
  • Oracle batteries (script files, local build vs cloud ~/oracle/bash-5.3.20/bin/bash): bug shape and the adjacency scripts byte-identical on stdout AND stderr; the wide battery byte-identical on stdout.
  • Adjacent suites green: issue4128 (5), issue2026 (5), issue1981 (4), issue2025 (6), issue1151 (1), issue1184 (2), issue4031 (4); cargo test --lib lexer:: 112/112. rustfmt clean on both files.

Honest notes (pre-existing, NOT this issue, untouched)

  • heredoc_regressions has one failure on pristine master too (unterminated_heredoc_with_backslash_newline_warns_at_last_physical_line: probe-script write NotFound in this sandbox) — verified by stash A/B; environment face.
  • Warning-side gaps observed while probing, all pre-existing and outside Quoted comsub with piped heredoc leaks the delimiter line (with 0x1c) into output #4141's scope: the unquoted E) form and the three-stage form emit no delimited-by-end-of-file warning in rubash where GNU emits one; one sequence shape (redirect + ; inside the comsub) reports the warning at a different line than GNU. Output bytes are correct in all three; recorded here for the captain's triage.
  • Full census differential is the PR CI's job (captain compares the name set against baseline-511cca8a-census.txt).

… (Refs #4141)

A quoted comsub whose heredoc feeds a pipeline declined by the cat
plan (plain sort / cat / a third stage models only [sort, -u]) falls
to the real executor, which re-tokenizes the extracted body. In that
text the delimiter line carries the reader's IFS_GLUE record byte in
place of the consumed closing paren, and feed_awaiting_body's
in-comsub trailing-residue arm required the rest after the delimiter
to trim to empty — 0x1c is not whitespace — so the line failed every
terminator arm and was pushed into the body (the delimiter line
leaked into the captured output).

GNU gather_here_documents (make_cmd.c:602) ends the document at the
exact delimiter line; the PST_EOFTOKEN arm (make_cmd.c:606-611) ends
it at the delimiter followed by the substitution's EOF token. In the
extracted body the EOF token is already consumed and the glue record
is its only trace, so the residue test now ignores IFS_GLUE before
the trim check: delimiter + glue/whitespace terminates, delimiter +
real words still does not.
…ifs_glue (Refs #4141)

The heredoc gatherer's in-comsub arm stripped the reader's IFS_GLUE
record from the post-delimiter residue with a bare .replace() keyed
on the registry const. The marker-lint L3 gate rejects needle
operations on registry consts at unreviewed sites: a blind replace
cannot see the escape pairs that make a typed marker char data
(rubash#2885 E7), so marker surgery belongs in
src/executor/markers.rs.

Move the strip into a new pair-aware helper there,
markers::strip_ifs_glue: IFS_GLUE is a prefix carrier
(protect_ifs_chars splices it in front of the character it
protects), so the walk drops every bare glue byte and copies the
protected data char verbatim. A user-typed U+001C never arrives as a
bare char — typed C0 data rides raw-byte marker pairs, whose
codepoints are distinct and pass through untouched — so the strip
cannot fold data into protocol. Output is byte-identical to the
.replace() it supersedes; the lexer's residue test, its #4141
comment, and the guard behavior (delimiter + real words still does
not terminate) are unchanged.

marker-lint: OK (was 1 violation at src/lexer/mod.rs:1213).
Unit pin: strip_ifs_glue_drops_carrier_preserves_data.
@caomengxuan666
caomengxuan666 merged commit 623ead4 into master Nov 24, 2026
10 of 12 checks passed
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