Skip to content

fix(uniq): treat +NUM after -- as a file name (Fixes #1878) - #2008

Open
caomengxuan666 wants to merge 1 commit into
mainfrom
fix/1878-uniq-plusnum-after-double-dash
Open

caomengxuan666 wants to merge 1 commit into
mainfrom
fix/1878-uniq-plusnum-after-double-dash

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes #1878

Problem

After --, a leading +NUM operand must be a file name, but WinuxCmd swallowed it as uniq's obsolete +N skip-chars option:

$ uniq -- +3 file
# WinuxCmd before:  rc=0, prints the skip-chars result
# GNU:              uniq: +3: No such file or directory   (rc=1)

-NUM / +NUM before -- must keep their skip-fields / skip-chars meaning.

Oracle (GNU coreutils 9.4, LC_ALL=C, via WSL script file)

$ uniq -- +3 file
uniq: +3: No such file or directory                      rc=1
$ uniq -- +0 file
uniq: +0: No such file or directory                      rc=1
$ uniq -- +3 </dev/null                                  rc=1   (operand never falls back to stdin)
uniq: +3: No such file or directory
$ uniq +3 file                                           rc=0   skip 3 chars   -> "aXX"
$ uniq -3 file                                           rc=0   skip 3 fields  -> "aXX"
$ uniq +2 -- file                                        rc=0   skip 2 chars   -> "aXX"
$ uniq -- -3 file
uniq: -3: No such file or directory                      rc=1
$ uniq -- +3x file                                       rc=1   (not all digits -> file name)
uniq: +3x: No such file or directory
$ uniq -- +3 a b
uniq: extra operand 'b'                                  rc=1   (checked before opening 'a')
Try 'uniq --help' for more information.
$ uniq -- +3            # with a real file named +3 present
A
B                                                        rc=0   (reads the file)

Root cause

uniq.c:585-589 parses the obsolete +NUM / -NUM options only while the token is still an option — i.e. before getopt's --. The shared option parser consumes -- and returns bare ctx.positionals, so the command body had no way to tell uniq +3 file from uniq -- +3 file and treated +3 as skip-chars in both.

A second, related divergence came from the same code: the pre-glob splice re-indexed ctx.positionals, so after a +NUM was consumed the INPUT/OUTPUT operands shifted (and uniq -- +3 a b reported a: No such file instead of the extra-operand diagnostic).

Fix (src/commands/uniq.cpp, minimal)

  • collect_operands() re-walks ctx.raw_args with the policy the parser used (value-taking -f/-s/-w and their long forms; the POSIXLY_CORRECT stop rule) and reports each operand with an after_double_dash flag. Reusing ctx.raw_args is the established pattern for grammar the parsed ctx cannot express (getopt, column, cal, comm).
  • build_config() applies the +NUM → skip-chars rule only to option-position tokens; after -- the operand is kept as a file name.
  • The glob splice now uses the already-filtered operand list instead of positional indices, so consumed +NUM options no longer shift INPUT/OUTPUT.

No new user-visible strings (the uniq: +3: No such file or directory text already exists), so no i18n baseline change.

Tests

Six new unit tests (issue1878_*) in tests/unit/uniq/uniq_unit_test.cpp:

  • issue1878_plus_num_after_double_dash_is_a_filename — rc=1, empty stdout, exact GNU message
  • issue1878_plus_num_after_double_dash_alone_does_not_read_stdin — operand never falls back to stdin
  • issue1878_plus_num_after_double_dash_reads_the_file — a real +3 file is read
  • issue1878_plus_num_before_double_dash_still_skips_chars — +3 file and +2 -- file unchanged
  • issue1878_minus_num_still_skips_fields — -3 file unchanged
  • issue1878_double_dash_splice_keeps_output_operand — +2 -- in out still writes out

Evidence:

$ winuxcmd-tests.exe --gtest_filter=uniq.*
[==========] 38 tests with filter 'uniq.*', 38 passed, 0 failed

(32 pre-existing + 6 new)

End-to-end probe against the patched winuxcmd.exe

Same script file run on both sides (WSL):

invocation GNU 9.4 WinuxCmd (patched)
uniq -- +3 file rc=1 uniq: +3: No such file or directory rc=1, same message
uniq +3 file rc=0 → aXX rc=0 → aXX
uniq -3 file rc=0 → aXX rc=0 → aXX
uniq +2 -- file rc=0 → aXX rc=0 → aXX
uniq -- -3 file rc=1 uniq: -3: No such file or directory rc=1, same message
uniq -- +3 a b rc=1 uniq: extra operand 'b' rc=1, same message
uniq -- +3x file rc=1 uniq: +3x: No such file or directory rc=1, same message
uniq -- +3 < in rc=1, no stdin fallback rc=1, no stdin fallback

Gate / ctest scope

  • rust2/gate/run-gate.sh uniq: not applicable — there is no rust2/gate/cases/uniq.tsv and no Rust winux-cmds port of uniq (rust2/crates/winux-cmds/src/uniq.rs does not exist). The runner reports ERROR: 1 requested command(s) had no case table. uniq is still a C++ command, so this fix is in the command body (no dual track added).
  • cargo test -p winux-cmds: not run — no Rust code was changed.
  • ctest: ran the uniq unit tests directly from the built winuxcmd-tests.exe (--gtest_filter=uniq.*, 38/38) rather than a full unisolated ctest.

Unverified

  • POSIXLY_CORRECT=1 interaction: the scan mirrors the parser's documented stop rule, but the standalone winuxcmd.exe probe cannot exercise env-var semantics under WSL, so it is not evidence-backed here.

Formatting

scripts/format.py reports "No files found that need formatting". The bundled clang-format (22.1.3) flags 75 pre-existing violations on origin/main for uniq.cpp under its newer style; none of the lines added by this PR are among them.

GNU uniq.c:585-589 parses the obsolete +NUM and -NUM options only while
the token is still an option, i.e. before getopt's "--". Once "--" has
ended option parsing, "+N" is an ordinary operand and is opened as a
FILE. Oracle (GNU coreutils 9.4, LC_ALL=C, WSL):

  $ uniq -- +3 file
  uniq: +3: No such file or directory      rc=1
  $ uniq -- +3 </dev/null
  uniq: +3: No such file or directory      rc=1   (no stdin fallback)
  $ uniq +3 file                            -> skip 3 chars, rc=0
  $ uniq -3 file                            -> skip 3 fields, rc=0
  $ uniq +2 -- file                         -> skip 2 chars, rc=0
  $ uniq -- +3 a b
  uniq: extra operand 'b'                  rc=1   (checked before open)

Before this change WinuxCmd swallowed "+3" as skip-chars in every one of
those positions: `uniq -- +3 file` exited 0 (GNU exits 1), and
`uniq -- +3 a b` reported "a: No such file" instead of the extra-operand
diagnostic, because the extra NUL/positional splice also misaligned the
operand indices.

The shared parser consumes "--" and returns bare positionals, so the
command body cannot tell whether a token was an option or an operand.
collect_operands() re-walks ctx.raw_args with the policy the parser
used (the value-taking -f/-s/-w options, the POSIXLY_CORRECT stop rule)
and reports which operands came after "--"; build_config() then applies
the +NUM rule only to option-position tokens. The pre-glob splice now
uses the filtered list instead of indexing ctx.positionals, so a
consumed +NUM no longer shifts the INPUT/OUTPUT operands.

Tests: six issue1878_* unit tests in tests/unit/uniq/uniq_unit_test.cpp
cover -- +NUM as a file (missing and existing), no stdin fallback,
+/-NUM before "--" unchanged, and the output-operand splice.
@caomengxuan666

Copy link
Copy Markdown
Member Author

复核结论:需要返工(只补两个单测,实现没问题)

头 2d7c05f4。这是一张 C++ 修复 PR(uniq 仍是 C++ 命令,无 Rust 版、无 uniq.tsv,gate 不适用——已核对)。#1878 的核心面修对了,作者正文的 10 个用例我已用云端 pristine coreutils 9.4 oracle 逐项重验、全部与正文一致(WSL .deb 证据已由 oracle 证据取代)。但正文里打包的两个次要修复没有 fail-first 单测,需要补。

机制核对(已读 parser 源码)

collect_operands() 重走 ctx.raw_args 标 after_double_dash,与共享 parser 的策略逐项对过:

  • 长选项缩写无脱节:dispatcher 在命令体运行前已把无歧义缩写展开成全名(dispatcher.cppm:1494-1560),raw_args 里没有缩写,scan 的精确名匹配是安全的;
  • 短簇 -f/-s/-w 的 attached/分离取值、-D5 数字串、--group/--all-repeated 的 optional 值不吃下一个 argv,均与 parser 一致;
  • POSIXLY_CORRECT 用存在性判断,与 parser(command_context.cppm:132-143)一致;oracle 实测空值也算(case 16)。
  • extra-operand 检查在 build_config 里、开文件之前,与 GNU 的先检查后打开一致;glob splice 改用已过滤的操作数列表,旧的 ctx.positionals[i] 拼接会在通配符展开后把 +2 重新拼进去。

Oracle 实证(pristine coreutils 9.4,LC_ALL=C,探针在云端 ~/uniq-2008-probe)

作者 10 例全确认:-- +3/-- +0/-- +3 </dev/null/-- -3/-- +3x 均为 rc=1 + 对应 No such file;+3/-3/+2 -- 均为 rc=0 → aXX;-- +3 a b 为 rc=1 extra operand 'b'(a 存在时也一样,证明先检查后打开);真实 +3 文件存在时 -- +3 读出 A/B。

作者自承未验证的 POSIXLY_CORRECT 交互,我也在 oracle 上把 ground truth 补了:scan 的模型在探到的 10 个 PC 形上与 GNU 全一致(+3 file 仍 skip、file +3 则 +3 变输出文件、file -c 则 -c 变输出文件、空值也算)。只有一处前身就有的边角与 GNU 不符:PC=1 uniq +3 -s 2 file,oracle rc=0(glibc 的 RETURN_IN_ORDER 在 case-1 的 +3 之后仍继续解析 -s),而新 scan 与旧代码完全相同地报 extra operand 'file'——根因是 parser 的 #1304 PC 策略(见第一个位置操作数即停),不是本 PR 引入的,记为 parser 策略后续项,不挡本单。另:PC=1 uniq file +3 -s 2 的报错命名,新代码与 GNU 一致(extra operand '-s'),旧代码会错报 '2'——本 PR 顺带修好了这一处。

R1/R2

  • R1 PASS:i18n-drift 绿,无新用户可见串。
  • R2 不适用:无 uniq.tsv、无 Rust 版 uniq.rs(main 上均 404 已核)。

需要补的两个单测(返工清单)

  1. glob splice 面没有真正钉住:新测 issue1878_double_dash_splice_keeps_output_operand 用的是 +2 -- in out,in 无通配符、glob 分支根本不触发——我手算过,旧代码下这个测试也通过。真正触发 splice bug 需要通配符输入。建议加:单个匹配文件,+2 -- *.txt out → 新代码 rc=0 且 out 写出;旧代码 rc=1 extra operand 'out'。
  2. extra-operand 顺序面没有单测:uniq -- +3 a b 只在正文 oracle 表里,6 个新测里没有;旧代码报的是 a: No such file(a 存在时甚至静默成功写 b)。建议加:a 存在,uniq -- +3 a b → rc=1,stderr 含 extra operand 'b'。

其余 6 个新测(-- +3 报错、单独不读 stdin、真实 +3 文件读出、+3/+2 -- 仍 skip、-3 仍 skip fields)都钉住了核心面。

与队长已备 Rust 补丁的关系

#2008 只闭合 C++ 侧的 #1878。#1877 仍 OPEN(rust2/port-uniq,头 eadfed52),其 uniq.rs:621-630 有同样的 bug(+NUM 转换作用于整个 raw_operands 池,不分 -- 前后)。队长备好的补丁 lane-1879-1878-fix.patch 之 commit 0002 正是修这一处,#1877 合入后仍需中继,否则接线 Rust 版 uniq 会把 #1878 回退;中继时重验 git apply --check(#1877 头若动过)。

CI 状态(发稿时)

arm64 两腿、x64 full 腿、rust-tests、i18n-drift、oracle、baseline、compare、baseline-growth 均绿;x64 default 腿(CTest)仍在跑,队长发评论前须重验该腿的 CTest 总数与 uniq 面(基线 4082 + 6 新测)。fmt 无 CI 门;作者称 scripts/format.py 干净、75 处为 main 上 pre-existing,本轮未独立核实。

短板说明:移植侧(patched winuxcmd.exe)未在本轮实机跑——devin-windows 隧道恢复后可补跑正文的端到端对照表;POSIXLY_CORRECT 的 WinuxCmd 侧行为未实机验,只核了 oracle 地面真值与 scan/parser 的策略一致性。

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.

[bug] uniq: +NUM after -- is swallowed as skip-chars (GNU treats it as a filename)

1 participant