Skip to content

fix: entry-encode imported environment values at the import funnel (Refs #2884) - #3002

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/2884-env-import-encoding
Nov 7, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/2884-env-import-encoding

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2884

UNVERIFIED-BY-CAPTAIN — lane self-verification in the lane REPORT; captain re-check pending.

Root cause

Process-environment import was the one value entry point that stored incoming values without entry encoding. The engine's stored spelling is a transport domain (carrier protocol, src/executor/markers.rs: C0/PUA codepoints standing in for GNU's CTLESC/CTLNUL), and every other value channel entry-encodes at its boundary — stdin lines, read input, comsub output and assignment RHS all enter through bytes_to_shell_text / push_escaped_text_with_carriers. Environment import fed process_env_pairs() straight into VarTable::from_values, so bare registry chars in inherited user values were live protocol downstream:

Parent value Before GNU
bare U+E314 declare -p prints v="" (display decoder's guard arm eats it) verbatim
U+E400 + U+E314 collapses to 1 char on expansion (${#v} = 1) kept as 2 chars
raw ESC byte declare -p prints the raw ESC inside plain quotes declare -p → v=$'A\EB'

GNU anchor: variables.c:368 initialize_shell_variables stores environ verbatim — GNU has no transport domain, so verbatim storage is only correct here once the value is in stored spelling.

Fix (one mechanism, at the single import funnel)

Executor::new_inner — the only constructor funnel (the api Session included) — now routes the imported map through a new env_helpers::entry_encode_imported_env before VarTable::from_values, encoding per consuming channel:

  • __RUBASH_* names: the shell's own cross-process transport — interpretation is deliberate, values pass verbatim (the issue's scoping note).
  • BASH_FUNC_*%% entries: consumed by the function-source channel (import_exported_functions_from_env tokenizes the value as source), so they take the source encoding (escape_marker_zone_chars, the bytes_to_script_text parity); raw carrier bytes in a body keep the lexer meaning they would have in a script file.
  • every other (user) value: the data encoding — new substitution_metadata::entry_encode_value_text, the bytes_to_shell_text valid-text arm (push_escaped_text_with_carriers: marker-zone chars E400-escaped, raw carrier bytes as raw-byte marker pairs), with a no-change fast path.

The process_env_snapshot field keeps the raw values — the Drop-time restore writes them back to the process environment verbatim; only the table import is encoded. Respawn-state file entries (rubash-written transport, spliced after import) are untouched.

Pins

tests/issue2884_env_import_entry_encoding_regressions.rs (7 tests; expectations captured byte-for-byte from GNU bash 5.3.20): the three golden cells, lone-payload-char and tab/newline controls, the BASH_FUNC source channel, and a nested-shell round trip. Fail-first on base fb96e741: 3 passed / 4 failed (the 4 failures are exactly the golden cells + round trip; controls pass). Plus a unit test in env_helpers pinning the per-channel encoding contract.

本 PR 删除了什么

Nothing deleted: +113/−5 across three source files (one widened visibility, escape_marker_zone_chars → pub(crate)) plus the new pin file. No ledger change (no new public surface — both new helpers are pub(in crate::executor) / pub(crate)); src/lexer/continuation.rs untouched.

Golden cells from the #2879 sweep env-import battery, expectations
captured byte-for-byte from GNU bash 5.3.20: bare U+E314 eaten by
declare -p, U+E400+U+E314 pair collapsing to one char, raw ESC byte
losing its $'...' quoting — plus payload/tab-newline controls, the
BASH_FUNC source channel, and a nested-shell round trip.
Fail-first on base fb96e74: 3 passed / 4 failed.
…efs #2884)

GNU variables.c:368 initialize_shell_variables stores environ
verbatim because GNU has no transport domain; this engine does, and
environment import was the one value entry that skipped the encoding
every other channel applies (stdin lines, read input, comsub output
all enter through bytes_to_shell_text / push_escaped_text_with_carriers).

Executor::new_inner now routes the imported map through
env_helpers::entry_encode_imported_env before VarTable::from_values,
per consuming channel: __RUBASH_* transport verbatim, BASH_FUNC_*%%
bodies take the source encoding (escape_marker_zone_chars, the
bytes_to_script_text parity), user values take the data encoding
(new substitution_metadata::entry_encode_value_text, the
bytes_to_shell_text parity). The process_env_snapshot field keeps
the raw values for the Drop-time verbatim restore.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 7, 2026 17:30
@caomengxuan666
caomengxuan666 merged commit b4eaed5 into master Nov 7, 2026
11 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