Skip to content

fix(env): skip invalid-identifier env names like GNU bash 5.3 (niubash #102) - #106

Merged
caomengxuan666 merged 2 commits into
masterfrom
feat/invalid-env-names
Sep 15, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
feat/invalid-env-names

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes unixwin/niubash#102

问题

Windows 环境里有 CommonProgramFiles(x86) 这类名字不是合法 shell 标识符的环境变量。GNU bash 5.3 把它们绑定进独立的 invisible invalid_env 表(variables.c:3307,由 maybe_make_export_env variables.c:5064 合入导出环境):会透传给子进程,但对 set / export -p / declare -p / ${!prefix@} 不可见。

rubash 之前把它们当普通导出变量导入,导致:

  • export -p 输出不加引号的 declare -x CommonProgramFiles(x86)=...
  • Hermes 等工具的 export -p > snapshot; source snapshot 用法直接 syntax error near unexpected token '('
  • set、${!X*} 名单、compgen -v 同样泄漏非法名
  • declare -p 'X(BR)' 因名字被拒后落进无参全量列举分支,dump 整个变量表

修复(对照 GNU bash 5.3.0(1) oracle 逐项验证)

行为 修复后 GNU 5.3.0
export -p / set 列举 跳过非法名 一致
子进程环境 非法名仍透传 一致
declare -p INVALID not found,rc=1 逐字节一致
declare INVALID / export INVALID=x not a valid identifier,rc=1 逐字节一致
${!prefix*} / ${!prefix@} 名单 过滤非法名 一致
BASH_FUNC_*%% 导入为函数后从变量表移除,函数可调用、不再进 export -p 一致
export -p > snap; source snap SOURCE_OK 一致

改动点

  • executor/init.rs + shell/variables.rs:非法标识符名不导入变量表(子进程通过父环境照常继承);BASH_FUNC_*%% 成功导入函数后从 env_vars 移除
  • builtins/declare.rs / declare/print.rs / setattr/print.rs:列举过滤 + 报错文案对齐(-p 走 not found,非 -p 走 not a valid identifier)
  • builtins/set.rs + set/unset.rs:共享标识符校验,过滤 set 列举
  • builtins/complete.rs + executor/expand_braced_special.rs + executor/arrays/executor.rs:${!prefix@} / ${!prefix*} 名单与 compgen -v 候选过滤

验证

  • cargo test --lib --locked:397 passed, 0 failed;cargo fmt --check 干净
  • 端到端用临时驱动(Executor 真实执行路径)对 WSL /usr/local/bin/bash 5.3.0(1) 做 7 组差分,全部一致;含回归单测

🤖 Generated with WorkBuddy

…#102)

Windows environments carry entries such as CommonProgramFiles(x86) whose
names are not valid shell identifiers. GNU bash binds those into the
invisible invalid_env table (variables.c:3307, wired into the export
environment by maybe_make_export_env variables.c:5064): they reach child
processes but are invisible to set / export -p / declare -p / ${!prefix@}.
rubash imported them as ordinary exported variables, so export -p emitted
unquoted 'declare -x CommonProgramFiles(x86)=...' lines and sourcing that
snapshot died with "syntax error near unexpected token '('".

- executor/init.rs + shell/variables.rs: do not import non-identifier env
  names into the variable store (children still inherit them through the
  parent environment); drop BASH_FUNC_*%% entries after function import
- builtins/declare{,/print}.rs + setattr/print.rs: exclude invalid names
  from export -p / set / declare -p listings; declare -p INVALID now
  reports "not found" while declare/export INVALID report "not a valid
  identifier", byte-identical to bash 5.3.0
- builtins/set.rs + set/unset.rs: share the identifier check, filter the
  set listing
- builtins/complete.rs, executor/expand_braced_special.rs,
  executor/arrays/executor.rs: ${!prefix@} / ${!prefix*} name lists and
  compgen -v variable candidates skip invalid names
- declare_tests.rs: regression tests for listing and diagnostics
…ygiene guard

The Check UTF-8 boundary hygiene step (added in fe16578) has been failing
CI since it landed: its own tree contained four unreviewed byte->char
widening sites.

- executor/command_substitution_pipelines.rs: rewrite the heredoc
  delimiter scanner at byte level (byte == b'\'', byte.is_ascii_whitespace(),
  matches!(byte, b';' | b'|' | b'&' | b')')). The previous widened form
  turned a UTF-8 continuation byte 0x85 into U+0085, which satisfies
  char::is_whitespace() and truncated a multibyte delimiter mid-character.
- scripts/check-utf8-boundary-hygiene.sh: allowlist
  parser/token_actions.rs `Some(c as char)` — the byte there can only be
  one of the ASCII structural bytes matched by b'&' | b'|' | b';' |
  b'<' | b'>'.

Verified with a multibyte (CJK) heredoc delimiter through the real
executor; cargo test --lib 397 passed; hygiene check clean.

🤖 Generated with [WorkBuddy](https://workbuddy.cn)
@caomengxuan666
caomengxuan666 merged commit 9729b14 into master Sep 15, 2026
3 checks passed
@caomengxuan666
caomengxuan666 deleted the feat/invalid-env-names branch September 15, 2026 14:07
caomengxuan666 added a commit that referenced this pull request Sep 16, 2026
fix: niubash #106/#107/#108 regressions + restore #103 array fix lost in rescue merge
caomengxuan666 added a commit that referenced this pull request Sep 16, 2026
…as-char sites

- README/README.zh-CN: new "Fixed today" section covering niubash #106 (CRLF
  line-terminator handling), #107 (-c GNU first-non-option-argument), #108
  (type output capture), plus the nameref/history convergence and the
  debug-instrumentation cleanup from the local batch landing.
- check-utf8-boundary-hygiene: extend the execution_misc.rs allowlist entry
  with the two reviewed byte->char widening patterns introduced by the batch:
  `out.push(byte as char)` under the 0x20..=0x7e printable-ASCII arm and the
  octal digit math `b'0' + ((byte >> n) & 0o7)` (always 0x30..0x37) — both can
  never involve a UTF-8 continuation byte.
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.

在应用到hermes时有个引号错误

1 participant