`stash_foreach` crashes on stash entries with no message or a non-UTF-8 message

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

Maintainer thường phản hồi trong vòng 1 ngày

@shivampwm2020 đang làm issue này rồi.

Từ ngày 16/10/2026.

  • #1321 của @shivampwm2020 — đang mở

Đá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
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
api, backend

Hướng nghiên cứu

Bắt đầu với stash_cb trong src/stash.rs, nơi issue xác định vị trí chuyển đổi thông báo stash thành chuỗi. Dựa vào các bước tái hiện đã được báo cáo cho cả hai trường hợp, kiểm tra cách callback xử lý thông báo NULL và UTF-8 không hợp lệ. Công việc được xem là hoàn tất khi stash_foreach chạy xong mà không bị crash hay panic, đồng thời truyền một chuỗi rỗng khi thiếu thông báo.

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

Mô tả

Calling Repository::stash_foreach with git2 0.21.0 crashes when a stash reflog entry has no message. A non-UTF-8 reflog message causes a panic instead.

This affects applications that enumerate stashes when opening a repository, such as Gitte.

To reproduce this issue, create a repository with a stash, then remove the message from its reflog entry, eg like this:

mkdir stash-repro
cd stash-repro
git init
git config user.name "Test"
git config user.email "[email protected]"

printf 'base\n' > file.txt
git add file.txt
git commit -m "Initial commit"

printf 'changed\n' > file.txt
git stash push -m "Example stash"

cut -f1 .git/logs/refs/stash > .git/logs/refs/stash.tmp
mv .git/logs/refs/stash.tmp .git/logs/refs/stash

Then run this with git2 0.21.0:

fn main() -> Result<(), git2::Error> {
    let mut repo = git2::Repository::open(".")?;

    repo.stash_foreach(|index, message, oid| {
        println!("{index}: {oid} {message}");
        true
    })?;

    Ok(())
}

The process crashes with a segmentation fault. The enumeration should succeed and pass an empty string for the missing message.

The cause is this: in src/stash.rs, stash_cb converts the message with:

CStr::from_ptr(message).to_str().unwrap()

For a reflog entry without a message message is NULL. Passing it to CStr::from_ptr causes undefined behavior.

The same conversion also panics if a message contains invalid UTF-8. This can be reproduced by replacing the message part of the reflog entry with bytes such as b"caf\xe9".

As a workaround, I now use repo.reflog("refs/stash") directly and use entry.message_bytes() with String::from_utf8_lossy. This works with entries without a message and with entries with an invalid UTF-8 message.

Ngôn ngữ chính
Rust
Star
2.1k
Fork
470
Merge trung bình
2 ngày 17 giờ
Pull request đã merge (30 ngày)
18

Chuẩn bị môi trường

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 rust-lang/git2-rs

Tất cả issue của rust-lang/git2-rs

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.