Skip to content

cross-platform: gate residual Windows assumptions outside cfg(windows) + stale doc claims - #499

Merged
caomengxuan666 merged 1 commit into
masterfrom
audit/windows-hardcode
Oct 10, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
audit/windows-hardcode

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Non-cfg-layer Windows-hardcode audit

Sister of the cfg-gate audit (#492). Scope: Windows assumptions that live outside cfg(windows) gates — strings, defaults, docs, product behavior. Classification: (a) legitimate Windows-native code behind cfg gates — kept; (b) Windows assumptions wrongly active/visible on non-Windows builds — fixed; (c) stale/dangling docs & copy — fixed. Report-only items were deliberately not touched (see footer).

Base: origin/master @ 61dd62f. Windows behavior byte-identical (unix-arm additions or cfg(windows)-scoped text only).

Fixed in this PR

# Location Class Finding Disposition
1 src/expand/tilde/tilde.rs — home_value() b USERPROFILE fallback (env_vars.get("USERPROFILE") + env::var("USERPROFILE")) ran un-gated; a stray USERPROFILE exported by cross-platform tooling would hijack ~ on unix once HOME is unset. Ledger E9 revision planned this gate ("随 getpwnam 改造一并") but it never landed. cfg(windows) gate; unix arm returns "" — identical to today's unix behavior (HOME unset + no USERPROFILE). The pre-existing windows_home_value_prefers_home_before_userprofile test was already cfg(windows).
2 src/main.rs — print_identity_disclosure() (--help) b Native-persona branch unconditionally printed switch: unset RUBASH_IDENTITY (or set any other value) to return to msys — false on non-Windows builds where current_identity() is hardwired Native (identity.rs:42-50). Platform-split: unix prints persona: native (honest-native; non-Windows builds are always native) and switch: n/a (RUBASH_IDENTITY only selects personas on Windows). Windows msys/native text unchanged.
3 src/main.rs — print_identity_report() (--identity) b Same defect: unix reported RUBASH_IDENTITY=native; honest-native (implies the var was set) and the msys-default switch line. Platform-split: non-Windows build; honest-native + n/a switch line. Windows text unchanged.
4 src/build_info.rs — version_appendix() (Windows --version note) c Note pointed users to docs/platform-support.md — the file does not exist anywhere in the repo; both test assertions anchored the dangling path. Repointed to README.md, Identity and Compatibility (where the persona contract actually lives); src/build_info.rs:107 + tests/cli_tests.rs:4146 assertions updated.
5 README.md — Quick Start / Build from Source c > Full functionality requires Windows today. directly contradicted the Platform status section 130 lines above (Linux native runs verified in WSL, macOS CI-green). Replaced with an accurate cross-platform note.
6 README.zh-CN.md — 从源码构建 c > 完整功能当前需要 Windows。 — same stale claim. Replaced (kept Chinese to match the file).
7 README.md — Identity section, default-persona bullet c Presented the MSYS persona as the unqualified default; non-Windows builds never take it. Added the qualification matching identity.rs module docs (non-Windows builds always report honest-native).

Verified correct — no change needed (a-class spot checks)

Location Check
src/executor/external_inner.rs — materialize_required_windows_env #[cfg(windows)] real body + #[cfg(not(windows))] no-op (ledger E8 fixed). SystemRoot/WINDIR/ComSpec copies at :287 also #[cfg(windows)].
src/executor/path.rs — apply_required_windows_child_environment cfg pair, unix side no-op.
src/executor/path.rs — windows_command_processor() (C:\Windows\System32\cmd.exe fallback) Only reachable under is_windows_batch_file()/is_windows_powershell_script() which embed cfg!(windows) and return false on unix.
src/executor/init.rs HOME-from-USERPROFILE seed (:106), TMPDIR+HOME backslash rewrite (:449), TMPDIR injection (:541) — all #[cfg(windows)] (ledger E4/E19 fixed).
src/executor/path.rs — standard_path() cfg!(windows) arm + unix confstr(_CS_PATH) (ledger E5 fixed).
src/executor/path.rs — has_path_separator / shell_path_to_process cfg-split; unix treats only / (ledger E7 fixed).
src/executor/lookup_paths.rs — /usr/bin/mv, /bin/{cat,ls} suite bridge #[cfg(windows)] (ledger E2 fixed).
src/executor/identity.rs Fully platform-aware: unix uname(2)/MACHTYPE/OSTYPE arms; MSYS persona unreachable off-Windows.
src/locale.rs platform_collate cfg pair; windows-sys import gated.
src/proc_vfs.rs, src/fd/windows_impl.rs, src/console_readline.rs Whole-file/function cfg(windows) gating with unix counterparts.
src/executor/local_helpers.rs — safe_temp_dir_string() Unix arm /tmp (rubash#232).
src/executor/job_builtins.rs — fc default vi; script_driver.rs PS1 default \s-\v\$ GNU-correct defaults on all platforms, not Windows hardcode.
Hardcoded C:\... / System32 literals All in #[cfg(windows)] bodies, doc comments, or #[cfg(test)] fixtures.

Reported — deliberately NOT changed

Location Class Finding Why not fixed here
src/bin/bash.rs + Cargo.toml [[bin]] name="bash" b (release-layer) E20 stands: locate_shell() probes only niu.exe/winuxsh.exe, so on unix the shim always exits 127; worse, unix builds ship a bash binary that can shadow the system bash in PATH. Ledger routes this to the C2 release batch (two-part: platform-split probe names + unix release stops installing the shim). The probe-name half alone would make the shadowing quieter (silent forward instead of loud 127) while the release half is pending — a product/packaging decision for the captain. Recorded as a known gap in docs/host-contract.md:73.
src/executor/support_names.rs:763 — executable_extensions() b (benign today) Un-gated PATHEXT/;-split helper; sole caller (lookup_paths.rs:139) is cfg-gated. Ledger E7 revision recommends renaming to prevent future misuse. Rename churns an executor-facing name; zero behavior gain today.
Display-path .replace('\\', "/") normalizations (external_finish.rs, lookup_paths.rs, init.rs, external_file_builtins.rs, …) b (cosmetic) Ungated; on unix a legal filename containing \ renders lossily in display paths. Display-only, and the sites sit in/next to the executor hot path (#242 conflict zone) — report only per audit constraints.
~ with HOME unset expands to "" (all platforms) semantic GNU keeps the word verbatim (echo ~ prints ~). Cross-platform semantic deviation, not a Windows hardcode; touching it changes suite-visible behavior.
Bare ~ has no unix getpwuid fallback (ledger E9 follow-up) semantic HOME unset + USERPROFILE absent → ~ is empty instead of passwd home. New libc surface, belongs with the tilde semantic fix above.

Verification

  • cargo fmt clean.
  • cargo check --lib (x86_64-pc-windows-msvc): clean.
  • cargo test --lib: 642 passed; 2 failed — builtins::pwd::tests::logical_mode_reports_posix_drive_pwd_by_default and executor::path::tests::virtual_system_root_operand_resolution_keeps_option_b_and_legacy_verbatim; both fail identically on pristine origin/master (WSL-interop environment-dependent assertions, e.g. expects /mnt/c), i.e. pre-existing, no new failures.
  • cargo check --lib --target x86_64-unknown-linux-gnu: clean (unix arm compiles).
  • Linux/macOS cargo test --lib runs: pending CI on this PR (no local linux toolchain for test execution).

Conflict hygiene: no changes in cfg-gate audit territory (#492), lexer word/scanner (#462 neighborhood), or the executor hot path (#242 neighborhood).

Non-cfg-layer Windows-hardcode audit (sister of the cfg-gate audit #492).

b-class fixes (wrong on non-Windows builds):
- tilde.rs home_value(): the USERPROFILE fallback (env_vars lookup +
  process env) ran un-gated, so a stray USERPROFILE exported by
  cross-platform tooling would hijack `~` on unix once HOME is unset.
  Now cfg(windows)-gated; unix keeps the empty default it always had.
- main.rs identity disclosure/report: on non-Windows builds the persona
  is always native, but --help/--identity still advertised "unset
  RUBASH_IDENTITY to return to msys". Platform-split so unix prints an
  n/a switch line instead of an impossible instruction.

c-class fixes (stale/dangling copy):
- build_info.rs --version note pointed to docs/platform-support.md,
  which does not exist; repointed to the README identity section
  (both anchoring assertions updated).
- README.md/README.zh-CN.md Quick Start still claimed "full
  functionality requires Windows today", contradicting the Platform
  status section (Linux native runs, macOS CI-green).
- README.md identity section now states non-Windows builds always
  report the honest-native identity.

Windows behavior is byte-identical (all edits are unix-arm additions or
cfg(windows)-scoped text). Verified: cargo check --lib + cargo test
--lib (642 passed; 2 pre-existing environment-dependent failures also
failing on pristine origin/master) + cargo check --lib
--target x86_64-unknown-linux-gnu.
@caomengxuan666
caomengxuan666 merged commit 5c6a052 into master Oct 10, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the audit/windows-hardcode branch October 10, 2026 08:44
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