diff panics (`into_string().unwrap()`) on a non-UTF-8 argument ending in `--width=N`

Đang mở Phù hợp với người mới
#247 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
85/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
rust
Lĩnh vực
cli

Hướng nghiên cứu

Bắt đầu trong src/params.rs, ở khu vực các regex của width và tabsize tại dòng 62-63, sau đó kiểm tra phần xử lý đối số quanh dòng 116. Tái hiện với đối số --width không phải UTF-8 được nêu trong issue và xác minh rằng đối số được xử lý như một toán hạng tệp thay vì gây ra panic hoặc được chấp nhận là một tùy chọn; cũng kiểm tra trường hợp xyz--width=5.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Rust
Star
276
Fork
39
Merge trung bình
3 giờ 27 phút
Pull request đã merge (30 ngày)
3

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của uutils/diffutils

Tất cả issue của uutils/diffutils

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.