diff panics (`into_string().unwrap()`) on a non-UTF-8 argument ending in `--width=N`
还没有人认领这个 Issue。
评估
调研方向
从 src/params.rs 中第 62-63 行附近的 width 和 tabsize 正则表达式开始,然后检查第 116 行附近的参数处理。使用 issue 中所示的非 UTF-8 --width 参数进行复现,并验证该参数会被当作文件操作数处理,而不是导致 panic 或被接受为选项;同时检查 xyz--width=5 的情况。
由索引模型根据 Issue 内容生成。
描述
Steps to reproduce
$ printf 'a\nb\nc\n' > A; printf 'a\nX\nc\n' > B
$ diffutils diff $'\xff--width=5' A B
thread 'main' panicked at src/params.rs:116:45:
called `Result::unwrap()` on an `Err` value: "\xFF--width=5"
$ echo $?
134
diff aborts (panic, exit 134) when given a non-UTF-8 argument whose lossy form ends in --width=<digits> — e.g. an argument with a leading invalid byte like $'\xff--width=5'.
Expected behavior
Match GNU: the unrecognized argument is a file operand; with three operands diff reports the extra operand and exits 2.
$ /usr/bin/diff $'\xff--width=5' A B
diff: extra operand 'B'
$ echo $?
2
Root cause
The --width regex lacks a start anchor, unlike the sibling --tabsize one:
// src/params.rs:62-63
let tabsize_re = Regex::new(r"^--tabsize=(?<num>\d+)$").unwrap(); // anchored — safe
let width_re = Regex::new(r"--width=(?P<long>\d+)$").unwrap(); // no leading ^
// src/params.rs:115-116
if width_re.is_match(param.to_string_lossy().as_ref()) { // matches lossy form
let param = param.into_string().unwrap(); // line 116: Err on non-UTF-8
to_string_lossy() maps invalid bytes to U+FFFD, so a non-UTF-8 argument whose tail is --width=N still matches; into_string() then fails on the real bytes.
Fix: anchor the regex at the start (^--width=…$, matching tabsize_re), and/or match on the bytes / avoid into_string().unwrap() so a non-UTF-8 argument falls through to the operand path. (The unanchored regex also makes diff xyz--width=5 silently accept a width option instead of treating it as a filename — same root cause, non-panic symptom.)
Found by our static analysis tooling.
- 主要语言
- Rust
- 星标
- 276
- 派生
- 39
- 平均合并
- 3 小时 27 分钟
- 30 天内合并 PR
- 3
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
uutils/diffutils 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 70/100
-
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 2/5 1-3 小时 新手友好度 68/100
-
难度 4/5 3-5 天 新手友好度 64/100
-
难度 3/5 1-2 天 新手友好度 76/100
相似的 Issue
-
risk:low runtime status:in-progress type:test
难度 1/5 1 小时以内 新手友好度 92/100
zeroclaw-labs/zeroclaw#11023 ·
-
good first issue refactor
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 2/5 1-3 小时 新手友好度 86/100
kwakseongjae/auto-hwp#319 ·
-
area:cli bug filter-quality good first issue priority:medium
难度 2/5 1-3 小时 新手友好度 84/100
-
难度 1/5 1 小时以内 新手友好度 72/100
bevyengine/bevy#25861 ·