Skip to content

fix(paths): resolve bare MSYS drive-root operands through the shared argv boundary (#1230) - #1274

Merged
caomengxuan666 merged 2 commits into
mainfrom
fix/1230
Oct 21, 2026
Merged

caomengxuan666 merged 2 commits into
mainfrom
fix/1230

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Fixes #1230 — bare MSYS drive-root operands (/x, /x/) bypassed the
drive-dialect recognizer in every command that resolved its operand
outside the shared native_path boundary: the raw /x decayed to a
cwd-drive-relative path (<cwd>:\x), so results depended on which drive
the caller stood on.

Two bug shapes were found and fixed:

  • Resolution miss (data-level): the operand reached a filesystem call
    (ifstream/CreateFileW/GetNamedSecurityInfoW/exists/…) verbatim,
    so /e silently read, wrote or queried D:\e when cwd sat on D:.
    Confirmed live: strings/xxd/tsort/hmac256/expand/unexpand/
    column/xargs -a/stty -F/sync/getfacl/mktemp -p/env -C all
    operated on the wrong drive when <cwd>:\e existed.
  • Classification miss: the open itself was already normalized, but
    the errno-reason probe (std::filesystem::status, attributes_w,
    is_directory, GetFileAttributesW) ran on the raw operand, producing
    cwd-dependent diagnostics (No such file vs Is a directory vs
    Permission denied for the same operand).

Fixes

  • df: operand loop now routes every positional through
    native_path::make_api_path_operand; stat/volume calls use the
    normalized spelling, diagnostics and the file field keep the operand
    as typed.
  • utils:wildcard: glob_expand folds the pattern through
    normalize_api_operand_w once at entry — every *-expansion
    (/e/* → E:\*) now enumerates the named drive.
  • utils:digest: open_failure_reason probes the resolved spelling —
    one fix covers md5sum/sha1sum/sha224sum/sha384sum/sha256sum/sha512sum/
    b2sum/cksum/sum, dircolors and grep's reason text.
  • mktemp: its private drive-dialect recognizer had the same root-form
    blind spot (size() >= 3 rejected bare /x and /cygdrive/x).
  • Per-command operand boundaries now normalize before the fs touch:
    strings, xxd (in+out), tsort, csplit, expand, unexpand, column,
    hmac256, xargs -a, hostname -F, getfacl, stty -F, env -C,
    chroot, sync, sort (-o, -T, error probe), diff (list_directory),
    grep (operand probes + recursive walk seed).
  • Error-reason probes now probe the resolved spelling: head, tail, sort,
    nl, fmt, fold, comm, join, paste, cut, column, expand, unexpand,
    hmac256, xargs, plus the whole *sum family via the digest fix.

Regression

tests/unit/df: df /x and df /x/ report the named volume from a
foreign-drive cwd (mirrors the #1231 stat dialect tests).

Full-command scan report

Probe matrix: <cmd> /e vs <cmd> E:/ (E: existing dir, C:\e/D:\e
absent) plus a D:\e-file cross-check from cwd D: (silent
wrong-drive detection). Static side: every ctx.positionals/option path
operand audited for whether it reaches an OS call without
native_path:: normalization.

Status Commands
Fixed in this PR df, head, tail, sort, nl, fmt, fold, comm, join, paste, cut, column, expand, unexpand, strings, xxd, tsort, csplit, hmac256, xargs, hostname, getfacl, stty, env, chroot, sync, diff, grep, mktemp + md5sum/sha1sum/sha224sum/sha384sum/sha256sum/sha512sum/b2sum/cksum/sum/dircolors (via digest fix) + all glob-expand users (via wildcard fix)
Already covered (operand flows through native_path/file_io) cat, wc, pr, tac, uniq, shuf, ptx, look, od, base32/64/basenc, split, less, more, ls/dir/vdir, tree, find, stat, file, du, namei, readlink, realpath, pathchk, lsattr, test/[, rm, rmdir, unlink, cp, mv, install, tee, truncate, shred, dd, sed, hexdump, rev, touch, chmod, chown, chgrp, chcon, chattr, mkdir, mkfifo, mknod, ln, cmp, diff3, sdiff, patch, cpio, man, tic, ldd, which, whereis, locate, u2d, d2u, unix2dos, dos2unix, tar-free spawn boundary (timeout/nohup/watch/stdbuf/runcon/nice/time/xargs-run — childproc::normalize_entry already folds /x)
Not path-taking / n.a. arch, basename, dirname, echo, expr, factor, false, true, yes, sleep, seq, printf, printenv, envsubst, numfmt, col, cal, date, free, uptime, users, who, w, whoami, groups, id, logname, pinky, hostid, hostname(def), nproc, uname, tty, stty(def), ps, top, vmstat, lsof, pgrep, pidof, pkill, kill, killall, nice, renice, watch, getconf, getopt, locale, tzset, logger, mkgroup, mkpasswd, runcon, getfacl(def), regtool, infocmp, toe, tput, reset, clear, mpicalc, dircolors(def), cygpath(see note), wpm(own install-path layer)
Related issues filed / noted cygpath -w /e prints E: instead of E:\ (drive-relative vs root spelling) — separate formatting bug, not the dialect blind spot. hexdump/rev report ENOENT-style text for directory operands (resolve correctly; GNU-fidelity gap). updatedb --localpaths, wpm, cpio -F use their own path layers — flagged for follow-up audit, not confirmed affected.

Test plan

  • df /e, df /e/, df /c (foreign cwd) report the named volume
  • Silent-wrong-drive probes: strings/xxd/hmac256/expand/
    xargs -a/stty -F/sync/getfacl/mktemp -p/env -C with a
    D:\e decoy file no longer read/query the decoy
  • grep /e, diff -q /e /e, sort /e, head /e, nl /e,
    md5sum /e report E:\ (Is a directory), not ENOENT
  • winuxcmd-tests df suite (new dialect-root tests)
  • CI

Generated with Devin

…argv boundary (#1230)

A bare drive root ("/e", "/e/") bypassed the drive-dialect recognizer in
every command that touched the filesystem outside the shared native_path
boundary: the raw operand decayed to a cwd-drive-relative path, so
resolution depended on the caller's drive. Two shapes were found in the
full-command scan:

- resolution miss: raw operands reached ifstream/CreateFileW/
  GetNamedSecurityInfoW/probes directly and silently read, wrote or
  queried <cwd>:\e (strings, xxd, tsort, hmac256, expand, unexpand,
  column, csplit, xargs -a, hostname -F, getfacl, stty -F, env -C,
  chroot, sync, sort -o/-T, diff list_directory, grep operand probes,
  mktemp's private dialect fold, df's operand loop)
- classification miss: the open was normalized but the errno-reason
  probe ran on the raw text, producing cwd-dependent diagnostics
  (head, tail, sort, nl, fmt, fold, comm, join, paste, cut and the
  *sum family via portable_digest::open_failure_reason)

Shared fixes: glob_expand folds the pattern once at entry (every
"/e/*" caller), digest's open_failure_reason probes the resolved
spelling, mktemp's recognizer accepts bare /x and /cygdrive/x roots.
df keeps the operand spelling for diagnostics and the "file" field
while filesystem calls use the normalized form.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member Author

Review: df fix + scan verified, one blocking gap found (sort -T)

Verified locally (MSVC Debug, isolated worktree of fix/1230):

  • df fix confirmed. df /e from a foreign-drive cwd reports E:\ (display keeps the as-typed operand, every fs call gets the api spelling — audited all path.api/path.display uses in the loop). New regression tests pass: df.* 26/26, including both drive-root dialect cases.
  • Shared boundary thesis holds. shell_ns::is_drive_form already accepts the bare root (size() == 2), so routing operands through make_api_path_operand/normalize_api_operand is the right fix shape. Spot-checked wildcard/digest/mktemp and ~15 per-command diffs — all normalize at the fs boundary and keep raw text for diagnostics, consistently.
  • Scan report is honest. My independent static sweep (commands touching fs with no boundary reference) surfaced only: cpio/cygpath/updatedb/wpm (flagged in your table), mktemp (fixed here), nohup (spawn boundary), pwd/locate/which/whereis (not operand-takers). "Already covered" spot-checks (cat, dd) confirmed.

Blocking: sort -T is only half-normalized — dialect hint now leaks temp files onto the wrong drive

The PR normalizes the validation probe (validate_temporary_directory_hint) but not the producer/consumer:

  • src/commands/sort.cpp ~line 1798: temp_dir = std::filesystem::path(utf8_to_wstring(cfg.temporary_directory_hint)) — raw hint.
  • src/commands/sort.cpp line 1921: final cleanup std::filesystem::remove(path) with path still carrying the raw \e\... spelling (the reopen loop at ~1880 goes through file_io::open_binary_file, which normalizes, so the merge itself reads the temps back fine).

Live repro (cwd D:\…, no D:\e present, ~100 MB input to force external spill):

winuxcmd sort -s -T /e in.txt -o out.txt   # exit 0
  • Before this PR: the raw probe made validation fail safely with invalid temporary directory.
  • With this PR: validation passes (probe sees E:\), but create_binary_file folds the raw \e\winuxcmd-sort-<pid>-N.tmp spelling onto E:\ (is_drive_form accepts backslashes too) while std::filesystem::remove resolves \e\… against the cwd drive (D:\e — not found, silent no-op). Result: every 8 MB spill chunk leaks to E:\ root. I measured 24 leaked winuxcmd-sort-*.tmp (~190 MB) from two runs, and confirmed sort -s -T 'E:\wc-ttemp' (native spelling) leaves nothing behind — the dialect spelling is the trigger.

This is the same operand at three points with only one normalized, which fails the ticket's "受影响命令逐个修复" bar for sort's -T (the PR table lists -T as fixed).

Requested fix (one line): fold the hint once where it is stored/consumed, e.g. at temp_dir construction:

: std::filesystem::path(utf8_to_wstring(
      native_path::normalize_api_operand(cfg.temporary_directory_hint)));

(or normalize when building cfg.temporary_directory_hint in build_config, so the probe reads the stored value as-is) — then creation, reopen, and cleanup all share one spelling and the leak closes. Happy to re-verify on push.

Not merging yet; everything else in the PR looks merge-ready. (Probe artifacts cleaned up; no repo state touched outside a disposable worktree.)

…anup (#1230)

The drive-root operand fix left sort -T half-normalized: the validation
probe folded "/e/..." onto the named volume while the spill-chunk path
kept the raw spelling. create_binary_file then wrote every 8 MB chunk
onto the hinted drive root, and the final std::filesystem::remove
resolved "\e\..." against the cwd drive - a silent no-op that leaked
each chunk.

Fold the hint through normalize_api_operand at temp_dir construction
and again at the remove call so creation, reopen, and cleanup share
one resolved spelling. New regression drives "-T /d/..." through the
external path and asserts no winuxcmd-sort-*.tmp survives.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666

Copy link
Copy Markdown
Member Author

Rework: sort -T dialect hint now normalized at producer + cleanup (e2504eb)

Blocker addressed. The -T operand now goes through native_path::normalize_api_operand at both raw-fs sites in external_sort, so creation, reopen, and cleanup share one resolved spelling:

  • src/commands/sort.cpp temp_dir construction: utf8_to_wstring(native_path::normalize_api_operand(cfg.temporary_directory_hint)) — the derived chunk paths now carry the native E:\... form.
  • src/commands/sort.cpp final cleanup: std::filesystem::remove re-folds each stored path through normalize_api_operand before deleting (defensive, in case a path ever enters temporary_paths from another source).

Third-spot audit of the function family: external_sort's only other fs touches are file_io::create_binary_file/open_binary_file (both normalize internally via make_api_path_operand), merge_record_chunks is pure in-memory over file_io reads, and the file_out.open sites at ~1722/~1992 already fold through normalize_api_operand. No third raw spelling found.

Live re-verification (MSVC Debug, worktree on D:, cwd on D:)

  • sort -s -T /e big.txt -o bigout.txt (108 MB, 13×8 MB spill chunks): mid-run poll caught winuxcmd-sort-44672-0.tmp on E:\ root (correct volume, as hinted); post-run E:\ root has zero winuxcmd-sort-* residue — the leak is closed.
  • Native control sort -s -T 'E:\wctemp' in.txt -o out2.txt: exit 0, E:\wctemp empty after — no regression.
  • df /e from D: cwd still reports the E:\ row — df fix preserved.

Regression coverage

New test sort.sort_temporary_directory_dialect_hint_cleans_spill_chunks (tests/unit/sort/sort_unit_test.cpp): builds the /d/... dialect spelling of a TempDir, forces the external path with -s, then asserts exit 0, sorted stdout, and zero winuxcmd-sort-* leftovers inside the hinted directory (pre-fix the remove silently no-ops against <cwd>:\d\... and the chunk leaks).

Test results

ctest -R "^(df|sort)\." — 99/99 passed (df 26/26 incl. both drive-root dialect cases, sort 73/73 incl. the new regression).

Commits: e2504eb on fix/1230.

@caomengxuan666
caomengxuan666 merged commit 2d3aafa into main Oct 21, 2026
8 checks passed
@caomengxuan666

Copy link
Copy Markdown
Member Author

终审通过:merge e2504eb → main 2d3aafa7(squash,--admin)

终审清单逐项复核结果(独立复验,未采信返工自测):

1. diff 审读 — 通过,无第四处

external_sort 临时目录族两处 raw-fs 位点均已归一:

  • src/commands/sort.cpp ~1804:temp_dir 构造经 utf8_to_wstring(normalize_api_operand(hint)),派生 chunk 路径携带原生拼写;
  • src/commands/sort.cpp ~1926:std::filesystem::remove 对每条 temporary_paths 再过一次 normalize_api_operand(原生拼写下幂等直通,防御性冗余,正确)。

同函数族其余 fs 触点核对:chunk 创建(~1820)与重开(~1886)走 file_io::create/open_binary_file,内部经 make_api_path_operand 归一;external_sort 的 -o(~1876)同走 file_io;两个裸 ofstream::open 位点(~1722、~1998)已显式包裹;校验探测(~1297)与错误分类探测(~184)已归一。全文件 grep u8path|filesystem::path(|remove|open( 无遗漏位点。对 PR 全部 33 个变更文件的独立扫描:所有裸 .open(/构造式 stream/CreateFileW/GetNamedSecurityInfoW 位点均已包裹或走共享边界。

2. 实测复现 — 通过(MSVC Debug,独立构建 e2504eb)

外盘 cwd(E:\)+ 52.8MB 输入 + sort -s -T /e big.txt -o out1.txt:

  • 运行中轮询:峰值 7 个 winuxcmd-sort-*.tmp 落在 E:\ 根(提示卷、提示目录,单 chunk ~8MB)——外排确实走了 -T 路径;
  • 结束后 E:\ 根 winuxcmd-sort-* 残留 0,未产生 E:\e 伪目录;
  • 输出 120 万行严格升序,退出码 0;
  • 原生拼写对照组 -T E:\...\native-tmp:退出码 0、chunk 落 native-tmp、结束后零残留、输出与 /e 组逐字节一致——不回退;
  • df 票面:df /e、df /e/、df E:\(D: 盘 cwd)均输出表头 + E:\ 行,退出码 0——不回退。

3. 回归 — 通过

ctest -R "^(df|sort)\."(Debug,本机复跑):99/99 通过(df 26 + sort 73,含新增 sort.sort_temporary_directory_dialect_hint_cleans_spill_chunks 与两条 df drive-root 用例)。

4. 无夹带 + I18N — 通过

本地 git diff f49c63f..e2504eb 文件清单与 GitHub PR files 逐项一致(33 文件,无 CHANGELOG/版本号等夹带);diff 新增行中无新用户可见串(仅注释与既有错误串复用),无新 i18n key。

遗留观察(非阻塞,不属本 PR 范围)

同类"raw 拼写直通 fs"残留还有两处旧有位点(本 PR 未触及、未声称修复):

  • src/commands/env.cpp ~168:env --file/-f 的 env 文件读取为裸 std::ifstream(及 ~158 的裸目录探测),-f /e/vars 会静默读到 <cwd>:\e\vars;
  • src/commands/csplit.cpp ~463:输出文件名由 -f/--prefix 前缀裸构造,-f /e/x 会把分块写到错误卷。

建议另开 issue 顺延 #1230 的边界收敛。另:external_sort 中途出错提前返回时已写出的 chunk 不回收(先于本 PR 存在,未变)。

CI:PR 分支全绿(compare / oracle / arm64 / x64);合并后 main 2d3aafa7 终态全绿 —— GNU master differential(run 39591428038)success,CMake Windows Multi-Arch CI(run 39591428125,x64 + arm64 + i18n)success。

Fixes #1230。

caomengxuan666 added a commit that referenced this pull request Oct 22, 2026
…1276) (#1278)

The -f/--prefix operand feeds each derived output filename, which the
bare ofstream resolved literally: a dialect spelling like "/e/xx-" was
opened relative to the cwd drive instead of the named drive. Normalize
the filename once at materialization so the ofstream and cleanup's
remove share one resolved spelling - the same pattern as the sort -T
fix in #1274.

Regression test: "-f /d/..." through the external path asserts the
segments land in the named directory.

Fixes #1276.

Generated with [Devin](https://devin.ai)

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
caomengxuan666 added a commit that referenced this pull request Oct 22, 2026
…ary (#1277)

env --file opened its operand with a raw ifstream, so MSYS drive-dialect
spellings ("/e/...") decayed to a cwd-drive-relative path and missed the
named file - the same blind spot #1274 closed for sort -T. read_env_file
now resolves the operand once through native_path::normalize_api_operand:
the ifstream and the is_directory classification probe run on the native
spelling while diagnostics keep the caller's text.

Regression test: env -f /x/... through the external path loads the file
and prints its assignments (verified failing before the fix with
"cannot read env file '/c/...': No such file or directory").

Fixes #1275.

Generated with [Devin](https://devin.ai)

Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@caomengxuan666
caomengxuan666 deleted the fix/1230 branch October 30, 2026 01:42
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.

df 裸盘符根参数(/d、/e)绕过盘符方言识别,cwd 盘符依赖误解析——疑似共享路径归一化盲区,需全命令扫描

1 participant